【python】用python指令碼實現ansible的推送、下載、上傳檔案功能

ringoo_ming發表於2017-12-14

該指令碼可以實現:上傳檔案、下載檔案、執行命令


If you want perform command:
    Usage: (for one host) ./paramiko-upload.py -H [hostip] -c [command]
    Example: ./paramiko-upload.py -H 10.100.139.245 -c 'ls -al'


    Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -c [command]
    Example: ./paramiko-upload.py -F 'ip.txt' -c 'ls -al'


If you want send file:


    Usage: (for one host) ./paramiko-upload.py -H [hostip] -s [local_dir] [client_dir] [file]
    Example: ./paramiko-upload.py -H 10.100.139.245 -s '/opt/Felix' '/tmp' ip.txt


    Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -s [local_dir] [client_dir] [file]
    Example: ./paramiko-upload.py -F ip.txt -s '/opt/Felix' '/tmp' ip.txt


If you want get file:
    Usage: (for one host) ./paramiko-upload.py -H [hostip] -g [local_dir] [client_dir] [file]
    Example: ./paramiko-upload.py -H 10.100.139.245 -g '/opt/Felix' '/tmp' ip.txt


ip.txt file content like this:
    1.1.1.1
    2.2.2.2\033[0m
    """
    sys.exit()


指令碼如下:

  1. #!/usr/bin/env python
  2. #定義函式
  3. import paramiko
  4. import os
  5. import datetime
  6. import sys
  7. import re
  8. #定義基礎命令格式
  9. def perform_command(hostname, port, username, password, comm):
  10.     try:
  11.         t=paramiko.Transport((hostname,port))
  12.         t.connect(username=username,password=password)
  13.         try:
  14.             print "\033[33mIP:\033[0m", hostname
  15.             print "\033[33mPerform command:\033[0m", comm
  16.             print '\033[32m###########################################################\033[0m'
  17.             s=paramiko.SSHClient()
  18.             s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19.             s.connect(hostname = hostname, port = port, username = username, password = password)
  20.             stdin,stdout,stderr=s.exec_command(comm)
  21.             print stdout.read()
  22.             print '\033[32m###########################################################\033[0m'
  23.             print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
  24.             s.close()
  25.         except Exception:
  26.             print "\033[31mERROR - Perform error\033[0m"
  27.     except Exception:
  28.         print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname

  29. #定義傳送檔案格式
  30. def send_file(hostname,port,username,password,local_dir,client_dir,file):
  31.     try:
  32.         t=paramiko.Transport((hostname,port))
  33.         t.connect(username=username,password=password)
  34.         try:
  35.             print "\033[33mIP:\033[0m", hostname
  36.             print "\033[33mSend file:\033[0m", file
  37.             print "\033[33mClient_dir:\033[0m", client_dir
  38.             sftp=paramiko.SFTPClient.from_transport(t)
  39.             sftp.put(os.path.join(local_dir,file),os.path.join(client_dir,file))
  40.             t.close()
  41.             print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
  42.         except Exception:
  43.             print "\033[31mERROR - Perform error\033[0m"
  44.     except Exception:
  45.             print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
  46. #定義下載檔案格式
  47. def get_file(hostname,port,username,password,local_dir,client_dir,file):
  48.     try:
  49.         t=paramiko.Transport((hostname,port))
  50.         t.connect(username=username,password=password)
  51.         try:
  52.             print "\033[33mIP:\033[0m", hostname
  53.             print "\033[33mGet file:\033[0m", file
  54.             print "\033[33mClient_dir:\033[0m", client_dir
  55.             sftp=paramiko.SFTPClient.from_transport(t)
  56.             sftp.get(os.path.join(client_dir,file),os.path.join(local_dir,file))
  57.             t.close()
  58.             print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
  59.         except Exception:
  60.             print "\033[31mERROR - Perform error\033[0m"
  61.     except Exception:
  62.             print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
  63. #定義幫助檔案
  64. def help():
  65.     print """
  66. \033[33mHelp:

  67.   Welcome to use  python script, this script can help you run commands, send files and get files.

  68. Use this script, you can running for one host or a host group, and you know get file can't for a host group.

  69. Give you examples:

  70. If you want perform command:
  71.     Usage: (for one host) ./paramiko-upload.py -H [hostip] -c [command]
  72.     Example: ./paramiko-upload.py -H 10.100.139.245 -c 'ls -al'

  73.     Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -c [command]
  74.     Example: ./paramiko-upload.py -F 'ip.txt' -c 'ls -al'

  75. If you want send file:

  76.     Usage: (for one host) ./paramiko-upload.py -H [hostip] -s [local_dir] [client_dir] [file]
  77.     Example: ./paramiko-upload.py -H 10.100.139.245 -s '/opt/Felix' '/tmp' ip.txt

  78.     Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -s [local_dir] [client_dir] [file]
  79.     Example: ./paramiko-upload.py -F ip.txt -s '/opt/Felix' '/tmp' ip.txt

  80. If you want get file:
  81.     Usage: (for one host) ./paramiko-upload.py -H [hostip] -g [local_dir] [client_dir] [file]
  82.     Example: ./paramiko-upload.py -H 10.100.139.245 -g '/opt/Felix' '/tmp' ip.txt

  83. ip.txt file content like this:
  84.     1.1.1.1
  85.     2.2.2.2\033[0m
  86.     """
  87.     sys.exit()

  88. #定義命令詳情

  89. if __name__ == '__main__':
  90. #定義賬號、埠、密碼等資訊
  91.     username='root'
  92.     port=2222
  93.     password='abcdefg'


  94.     if len(sys.argv) > 2:
  95.         if sys.argv[1] == '-H':
  96.             if len(sys.argv) == 5 and sys.argv[3] == '-c':
  97.                 hostname=sys.argv[2]
  98.                 comm = sys.argv[4]
  99.                 perform_command(hostname, port, username, password, comm)
  100.             elif len(sys.argv) == 7 and sys.argv[3] == '-s':
  101.                 hostname=sys.argv[2]
  102.                 local_dir=sys.argv[4]
  103.                 client_dir=sys.argv[5]
  104.                 file=sys.argv[6]
  105.                 send_file(hostname,port,username,password,local_dir,client_dir,file)
  106.             elif len(sys.argv) == 7 and sys.argv[3] == '-g':
  107.                 hostname=sys.argv[2]
  108.     username='root'
  109.     port=2222
  110.     password='abcdefg'


  111.     if len(sys.argv) > 2:
  112.         if sys.argv[1] == '-H':
  113.             if len(sys.argv) == 5 and sys.argv[3] == '-c':
  114.                 hostname=sys.argv[2]
  115.                 comm = sys.argv[4]
  116.                 perform_command(hostname, port, username, password, comm)
  117.             elif len(sys.argv) == 7 and sys.argv[3] == '-s':
  118.                 hostname=sys.argv[2]
  119.                 local_dir=sys.argv[4]
  120.                 client_dir=sys.argv[5]
  121.                 file=sys.argv[6]
  122.                 send_file(hostname,port,username,password,local_dir,client_dir,file)
  123.             elif len(sys.argv) == 7 and sys.argv[3] == '-g':
  124.                 hostname=sys.argv[2]
  125.                 local_dir=sys.argv[4]
  126.                 client_dir=sys.argv[5]
  127.                 file=sys.argv[6]
  128.                 get_file(hostname,port,username,password,local_dir,client_dir,file)
  129.             else:
  130.                 help()
  131.         elif sys.argv[1] == '-F':
  132.             ip_file=sys.argv[2]
  133.             file_stat=os.path.exists(ip_file)
  134.             if file_stat:
  135.                 f=open(ip_file)
  136.                 ips=f.readlines()
  137.                 f.close()
  138.                 for ip in ips:
  139.                     ip = ip.strip('\n')
  140.                     if re.match(r'^#', ip):
  141.                         continue
  142.                     if len(sys.argv) == 5 and sys.argv[3] == '-c':
  143.                         hostname = ip
  144.                         comm = sys.argv[4]
  145.                         perform_command(hostname, port, username, password, comm)
  146.                     elif len(sys.argv) == 7 and sys.argv[3] == '-s':
  147.                         hostname=ip
  148.                         local_dir=sys.argv[4]
  149.                         client_dir=sys.argv[5]
  150.                         file=sys.argv[6]
  151.                         send_file(hostname,port,username,password,local_dir,client_dir,file)
  152.                     else:
  153.                         help()
  154.             else:
  155.                 print "\033[31mERROR - Not found ip_file!\033[0m"
  156.                 help()
  157.         else:
  158.             help()
  159.     else:
  160.         help()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31442725/viewspace-2148711/,如需轉載,請註明出處,否則將追究法律責任。

相關文章