学习用的工具

学习用的工具_第1张图片

代码:

# -*- coding: utf-8 -*-
from PyQt4.QtCore import pyqtSignature
from PyQt4.QtGui import QDialog
from PyQt4 import QtGui,QtCore
from Ui_SSH import Ui_SSH
import paramiko
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class SSH(QDialog, Ui_SSH):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)
QtCore.QObject.connect(self.chooseButton,QtCore.SIGNAL('clicked()'),self.choosefile)
@pyqtSignature("")
def on_execButton_clicked(self):
for ip in self.loopip():
self.command(ip)

@pyqtSignature("")
def on_uploadButton_clicked(self):
for ip in self.loopip():
print self.filename
self.uploadfile(ip,self.filename)
@pyqtSignature("")
def choosefile(self):
f = QtGui.QFileDialog(self)
self.filename = str(f.getOpenFileName())
return self.filename
def loopip(self):
iplist = str(self.iptextEdit.toPlainText()).split('\n')
return iplist
def command(self,ip):
cmd = str(self.cmdEdit.text())
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(ip,22,'root','yunwei@')
except paramiko.SSHException,e:
print e
stdin,stdout,stderr = ssh.exec_command(cmd)
self.resulttextEdit.append(u'IPinfo: %s'%ip)
for line in stdout.readlines():
self.resulttextEdit.append(u'%s'%line.strip())
self.resulttextEdit.append(u'-'*50)
ssh.close()
def uploadfile(self,ip,localpath):
t = paramiko.Transport((ip,22))
t.connect(username='root',password='yunwei')
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(localpath,'/root/')
t.close()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = SSH()
ui.show()
sys.exit(app.exec_())

你可能感兴趣的:(学习用的工具)