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

yipeiwu_com5年前Python基础

写了个小程序:

功能

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

2.批量执行远程ssh命令

效果如下:

pyqt远程批量执行Linux命令程序

代码如下:

#-*- 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命令程序的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python标准库itertools的使用方法

Python标准库itertools模块介绍 itertools是python内置的模块,使用简单且功能强大,这里尝试汇总整理下,并提供简单应用示例;如果还不能满足你的要求,欢迎加入...

Python实现将多个空格换为一个空格.md的方法

最近在文本预处理时遇到这个问题,解决方法如下: import re str1 = ' rwe fdsa fasf ' str1_after = re.sub(' +', '',...

Python求解任意闭区间的所有素数

题目:请求出任意区间[a,b]的所有素数,简单考虑实用性 这道题看起来应该很easy是吧,但任意区间(这个问题有没get 到) Afanty的分析: 1、首先明白什么叫素数,注意用求余法...

Python 使用 Pillow 模块给图片添加文字水印的方法

Python 使用 Pillow 模块给图片添加文字水印的方法

像微博一类的平台上传图片时,平台都会添加一个水印,宣誓着对图片的所有权,我们自己的博客平台也可以给自己的图片添加上水印。 还是用 Pillow 模块来实现 先来看一个简单的例子 &g...

深入理解Python中装饰器的用法

因为函数或类都是对象,它们也能被四处传递。它们又是可变对象,可以被更改。在函数或类对象创建后但绑定到名字前更改之的行为为装饰(decorator)。 “装饰器”后隐藏了两种意思——一是函...