pyqt远程批量执行Linux命令程序的方法

时间:2021-05-22

写了个小程序:

功能

1.测试远程ssh连接是否成功,

2.批量执行远程ssh命令

效果如下:

代码如下:

#-*- coding:utf-8 -*-import sysfrom PyQt4 import QtCore, QtGui, uicimport localeimport reimport osfrom PyQt4.QtCore import *from PyQt4.QtGui import *import paramikoqtCreatorFile = "test.ui" # Enter file here. Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)a = 0username_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命令程序的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章