Wrote a small program:
Function
1. Test whether remote SSH connection is successful,
2. Bulk Execute remote SSH command
The effect is as follows:
The code is as follows:
#-*-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_mainw Indow.__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 (user Name) Password = Self.password.text () password_list.append (password) self.table.setHorizontalHeade Rlabels ([' IP ', ' username ', ' password ']) newItem = Qtablewidgetitem (IP) self.table.setItem (A, 0, newitem) newitem = Qtablewidgetitem (user Name) 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, username, password) stdin, stdout, stderr = Ssh.exec_command (
"Who") print stdout.read () search = Re.search (Stdout.read (), username) if search:
info = u "Connection succeeded" Else:info = u "Connection failed" Except:info = u "Connection Failed" 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 (usernam E_list[i]) password = str (password_list[i]) ssh.connect (IP, username, password) St
DIN, 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.getpreferredenc Oding () code = qtextcodec.codecforname (mycode) Qtextcodec.setcodecforlocale (code) qtextcodec.setcodecfortr (COD
e) qtextcodec.setcodecforcstrings (code) window = MYAPP () window.show () Sys.exit (App.exec_ ())