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

时间:2021-09-28 09:29:07

写了个小程序:

功能

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

2.批量执行远程ssh命令

效果如下:

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

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#-*- 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命令程序的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq1124794084/article/details/53982337