學習用的工具

代碼:app

# -*- coding: utf-8 -*-from PyQt4.QtCore import pyqtSignaturefrom PyQt4.QtGui import QDialogfrom PyQt4 import QtGui,QtCorefrom Ui_SSH import Ui_SSHimport paramikoimport sysreload(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_())
相關文章
相關標籤/搜索