您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在pyqt中遠程批量執行Linux命令,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
功能
1.測試遠程ssh連接是否成功,
2.批量執行遠程ssh命令
效果如下:
代碼如下:
#-*- coding:utf-8 -*- import sys from PyQt4 import QtCore, QtGui, uic import locale import re import os from PyQt4.QtCore import * from PyQt4.QtGui import * import paramiko qtCreatorFile = "test.ui" # Enter file here. Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile) a = 0 username_list = [] ip_list = [] password_list = [] class MyApp(QtGui.QMainWindow, Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) self.add.clicked.connect(self.add_info) self.test.clicked.connect(self.test_link) self.do_2.clicked.connect(self.do_command) def add_info(self): global a ip = self.ip.text() ip_list.append(ip) username = self.username.text() username_list.append(username) password = self.password.text() password_list.append(password) self.table.setHorizontalHeaderLabels(['ip','username','password']) newItem = QTableWidgetItem(ip) self.table.setItem(a, 0, newItem) newItem = QTableWidgetItem(username) self.table.setItem(a, 1, newItem) newItem = QTableWidgetItem(password) self.table.setItem(a, 2, newItem) a += 1 def test_link(self): ip = str(self.ip.text()) username = str(self.username.text()) password = str(self.password.text()) try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, 22, username, password) stdin, stdout, stderr = ssh.exec_command("who") print stdout.read() search = re.search(stdout.read(), username) if search: info = u"連接成功" else: info = u"連接失敗" except: info = u"連接失敗" print info self.state.setText(info) ssh.close() def do_command(self): command = str(self.command.text()) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(len(ip_list)): ip = str(ip_list[i]) username = str(username_list[i]) password = str(password_list[i]) ssh.connect(ip, 22, username, password) stdin, stdout, stderr = ssh.exec_command(command) info = stdout.read() self.result.append(info) ssh.close() if __name__ == "__main__": app = QtGui.QApplication(sys.argv) mycode = locale.getpreferredencoding() code = QTextCodec.codecForName(mycode) QTextCodec.setCodecForLocale(code) QTextCodec.setCodecForTr(code) QTextCodec.setCodecForCStrings(code) window = MyApp() window.show() sys.exit(app.exec_())
以上就是如何在pyqt中遠程批量執行Linux命令,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。