pyqt遠(yuǎn)程批量執(zhí)行Linux命令程序的方法
寫了個(gè)小程序:
功能
1.測試遠(yuǎn)程ssh連接是否成功,
2.批量執(zhí)行遠(yuǎn)程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_())
以上這篇pyqt遠(yuǎn)程批量執(zhí)行Linux命令程序的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python函數(shù)中將變量名轉(zhuǎn)換成字符串實(shí)例
這篇文章主要介紹了python函數(shù)中將變量名轉(zhuǎn)換成字符串實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
剛學(xué)完怎么用Python實(shí)現(xiàn)定時(shí)任務(wù),轉(zhuǎn)頭就跑去撩妹!
朋友問我有沒有定時(shí)任務(wù)的模塊,并且越簡單越好.剛好前今天就研究了一下定時(shí)任務(wù)模塊,于是就告訴他使用方式,令我沒有想到的是,這貨他學(xué)會(huì)了之后,居然買了一個(gè)服務(wù)器給女朋友發(fā)消息,發(fā)消息,發(fā)消息……重要的事情說三遍,需要的朋友可以參考下2021-06-06
使用Python向DataFrame中指定位置添加一列或多列的方法
今天小編就為大家分享一篇使用Python向DataFrame中指定位置添加一列或多列的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
使用Python實(shí)現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-04-04
基于Matplotlib?調(diào)用?pyplot?模塊中?figure()?函數(shù)處理?figure圖形對象
這篇文章主要介紹了基于Matplotlib?調(diào)用?pyplot?模塊中?figure()?函數(shù)處理?figure圖形對象,matplotlib.pyplot模塊能夠快速地生成圖像,但如果使用面向?qū)ο蟮木幊趟枷?,我們就可以更好地控制和自定義圖像,下面就來詳細(xì)介紹其內(nèi)容,需要的朋友可以參考下2022-02-02

