pyqt遠程批量執行Linux命令程式__Linux

來源:互聯網
上載者:User

寫了個小程式:

功能

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_())




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.