PYQT Remote Bulk Execute Linux command program __linux

Source: Internet
Author: User
Tags locale ssh

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.