pyqt远程批量执行Linux命令程序的方法
作者:黑面狐 时间:2023-05-08 15:59:06
写了个小程序:
功能
1.测试远程ssh连接是否成功,
2.批量执行远程ssh命令
效果如下:
代码如下:
#-*- 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_())
来源:https://blog.csdn.net/qq1124794084/article/details/53982337
标签:pyqt,远程,Linux,命令
0
投稿
猜你喜欢
PYQT5实现控制台显示功能的方法
2023-08-08 11:11:53
centos7利用yum安装mysql 8.0.12
2024-01-26 08:09:45
完整java开发中JDBC连接数据库代码和步骤
2024-01-21 19:27:23
如何使用SQLServer数据库查询累计值
2009-03-16 14:43:00
Python OpenCV对图像像素进行操作
2021-02-25 13:02:20
python os模块简单应用示例
2021-12-20 13:36:48
解决Mac node版本升级失败的问题
2024-05-13 09:34:52
js Array的用法总结
2024-04-25 13:08:26
基于Keras的格式化输出Loss实现方式
2021-10-20 20:44:00
JavaScript实现鼠标经过表格某行时此行变色
2024-04-16 08:51:18
Python实现读取csv文件并进行排序
2021-06-27 08:37:59
Python开发之基于模板匹配的信用卡数字识别功能
2021-08-09 22:30:27
把vgg-face.mat权重迁移到pytorch模型示例
2021-11-03 16:29:20
python通过ffmgep从视频中抽帧的方法
2023-10-02 12:35:39
laravel yajra插件 datatable的使用详解
2023-11-18 20:05:06
用正则替换所有URL
2009-03-13 13:51:00
Oracle PL/SQL语言入门基础
2010-07-20 13:28:00
Vue项目中props传值时子组件检测不到的问题及解决
2024-06-05 15:31:36
ASP开发中可能遇到的错误信息中文说明大全(整理收集)第1/2页
2010-07-02 09:50:31
Vue实现未登录跳转到登录页的示例代码
2023-07-02 17:02:49