python paramiko實現多線程遠程執行命令、多線程遠程上傳文件、多線程遠程下載文件

python paramiko實現多線程遠程執行命令、多線程上傳文件、多線程下載文件

  最近內部服務器有在測試,需要更新一些和修改些文件,因爲服務器比較少,不想使用以前的一些技術,於是就自己寫一個python這個腳本去實現,基本功能可以實現,也沒有做優化,如果哪些朋友需要的話,自己優化,不懂的地方可以一起交流,直接上代碼

# coding=utf8
import paramiko,datetime,os,threading
runing = True
class run_cmd(threading.Thread):
      def __init__(self,hostname=None,password=None,username=None,port=None,echo_cmd=None):
          threading.Thread.__init__(self)
          self.hostname=hostname
          self.password=password
          self.username=username
          self.port=port
          self.echo_cmd=echo_cmd
          self.thread_stop=False
      def run(self):
          paramiko.util.log_to_file('paramiko.log')
          s=paramiko.SSHClient()
          s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
          s.connect(hostname = self.hostname,username=self.username, password=self.password)
          stdin,stdout,stderr=s.exec_command(self.echo_cmd)
          print stdout.read()
          s.close()
      def stop(self):
           self.thread_stop=True
class upload_thread(threading.Thread):
    def __init__(self,hostname=None,password=None,username=None,port=None,local_dir=None,remote_dir=None):
        threading.Thread.__init__(self)
        self.hostname=hostname
        self.port=port
        self.username=username
        self.password=password
        self.local_dir=local_dir
        self.remote_dir=remote_dir
        self.thread_stop=False
    def run(self):
        try:
                   t=paramiko.Transport((self.hostname,self.port))
                   t.connect(username=self.username,password=self.password)
                   sftp=paramiko.SFTPClient.from_transport(t)
                   print 'upload file start %s ' % datetime.datetime.now()
                   for root,dirs,files in os.walk(self.local_dir):
                        for filespath in files:
                            local_file = os.path.join(root,filespath)
                            a = local_file.replace(self.local_dir,remote_dir)
                            remote_file = os.path.join(self.remote_dir,a)
                            try:
                               sftp.put(local_file,remote_file)
                            except Exception,e:
                               sftp.mkdir(os.path.split(remote_file)[0])
                               sftp.put(local_file,remote_file)
                            print "upload %s to remote %s" % (local_file,remote_file)
                        for name in dirs:
                           local_path = os.path.join(root,name)
                           a = local_path.replace(self.local_dir,remote_dir)
                           remote_path = os.path.join(self.remote_dir,a)
               try:
                              sftp.mkdir(remote_path)
                              print "mkdir path %s" % remote_path
                           except Exception,e:
                              print e
                   print 'upload file success %s ' % datetime.datetime.now()
                   t.close()
                except Exception,e:
                   print e
    def stop(self):
        self.thread_stop=True
class get_thread(threading.Thread):
    def __init__(self,hostname=None,password=None,username=None,port=None,local_dir=None,remote_dir=None):
        threading.Thread.__init__(self)
        self.hostname=hostname
        self.port=port
        self.username=username
        self.password=password
        self.local_dir=local_dir
        self.remote_dir=remote_dir
        self.thread_stop=False
    def run(self):
        try:
            t=paramiko.Transport((self.hostname,self.port))
            t.connect(username=self.username,password=self.password)
            sftp=paramiko.SFTPClient.from_transport(t)
            print 'get file start %s ' % datetime.datetime.now()
            for root,dirs,files in os.walk(self.remote_dir):
                for name in dirs:
                    remote_path = os.path.join(root,name)
                    a = remote_path.replace(self.remote_dir,local_dir)
                    local_path = os.path.join(self.local_dir,a)
                    try:
                        sftp.mkdir(local_path)
                        print "mkdir path %s" % local_path
                    except Exception,e:
                        print e
                for filespath in files:
                    remote_file = os.path.join(root,filespath)
                    a = remote_file.replace(self.remote_dir,self.local_dir)
                    local_file = os.path.join(self.local_dir,a)
                    try:
                        sftp.get(remote_file,local_file)
                    except Exception,e:
                        sftp.mkdir(os.path.split(local_file)[0])
                        sftp.get(remote_file,local_file)
                    print "get %s to remote %s" % (remote_file,local_file)
            print 'get file success %s ' % datetime.datetime.now()
            t.close()
        except Exception,e:
            print e
    def stop(self):
        self.thread_stop=True
               
while runing:
    print  "1 執行cmd命令"
    print  "2 上傳文件"
    print  "3 下載文件"
    print  "* quit"
    ten = int(raw_input('Enter a number:'))
    if type(ten) is not int:
       break
    else:
         if ten == 1:
            while runing:
               print "1 手動輸入命令"
               print "*(任意輸入) 返回上級目錄"
               cmd_number = int(raw_input('Enter a number(命令):'))
               if cmd_number == 1:
                  username='root'
                  password='redhat'
                  port=22
                  echo_cmd=raw_input('Enter echo cmd:')
                  ip=raw_input('enter hostname:')
                  host=ip.split(' ')
                  for hostname in host:
                    cmd_thread=run_cmd(hostname,password,username,port,echo_cmd)
                    print hostname
                    cmd_thread.start()
                    cmd_thread.stop()
                    if (cmd_thread.isAlive()):
                         cmd_thread.join()
               else:
                  break
         elif ten == 2:
            while runing:
              print "1 上傳文件"
              print "*(任意輸入) 返回上級目錄"
              file_put = int(raw_input('Enter a number(上傳文件):'))
              if file_put == 1:
                  local_dir = raw_input('enter 源路徑 :')
                  remote_dir = raw_input('enter 目錄路徑:')
                  host = []
                  ip=raw_input('enter hostname:')
                  host=ip.split(' ')
                  username='root'
                  password='redhat'
                  port=22
                  for hostname in host:
                    print hostname
            uploadthread=upload_thread(hostname,password,username,port,local_dir,remote_dir)
            uploadthread.start()
            uploadthread.stop()
                       
            if (uploadthread.isAlive()):
                         uploadthread.join()
                                
              else:
                  break
         elif ten == 3:
             while runing:
              print "1 下載文件"
              print "*(任意輸入) 返回上級目錄"
              file_get = int(raw_input('Enter a number(下載文件):'))
              if file_get == 1:
                  username='root'
                  password='redhat'
                  port=22
                  remote_dir= raw_input('enter 服務器的路徑 :')
                  local_dir= raw_input('enter 本地的路徑:')
                  hostname=raw_input('enter請輸入其中一臺服務器地址即可:')
                  getthread=get_thread(hostname,password,username,port,local_dir,remote_dir)
                  getthread.start()
                  getthread.stop()
                  if (getthread.isAlive()):
                      getthread.join()
              else:
                  break
         else:
              break

   這裏的用戶輸入我作的判斷不是很完美,判斷是否是數字,是否輸入空的,我都沒有做,這些就比較簡單,我就不多說了,希望能幫助到一些朋友

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章