Paramiko:

  paramiko模块,基于SSH用于连接远程服务器并执行相关操作。

  SSHClient:

    用于连接远程服务器并执行基本命令

  SFTPClient:

    用于连接远程服务器并执行上传下载

以下该脚本可以实现:上传文件、下载文件、执行命令

  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 #定义发送文件格式
 31 def send_file(hostname,port,username,password,local_dir,client_dir,file):
 32     try:
 33         t=paramiko.Transport((hostname,port))
 34         t.connect(username=username,password=password)
 35         try:
 36             print "\033[33mIP:\033[0m", hostname
 37             print "\033[33mSend file:\033[0m", file
 38             print "\033[33mClient_dir:\033[0m", client_dir
 39             sftp=paramiko.SFTPClient.from_transport(t)
 40             sftp.put(os.path.join(local_dir,file),os.path.join(client_dir,file))
 41             t.close()
 42             print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
 43         except Exception:
 44             print "\033[31mERROR - Perform error\033[0m"
 45     except Exception:
 46             print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
 47 #定义下载文件格式
 48 def get_file(hostname,port,username,password,local_dir,client_dir,file):
 49     try:
 50         t=paramiko.Transport((hostname,port))
 51         t.connect(username=username,password=password)
 52         try:
 53             print "\033[33mIP:\033[0m", hostname
 54             print "\033[33mGet file:\033[0m", file
 55             print "\033[33mClient_dir:\033[0m", client_dir
 56             sftp=paramiko.SFTPClient.from_transport(t)
 57             sftp.get(os.path.join(client_dir,file),os.path.join(local_dir,file))
 58             t.close()
 59             print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
 60         except Exception:
 61             print "\033[31mERROR - Perform error\033[0m"
 62     except Exception:
 63             print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
 64 #定义帮助文件
 65 def help():
 66     print """
 67 \033[33mHelp:
 68 
 69   Welcome to use  python script, this script can help you run commands, send files and get files.
 70 
 71 Use this script, you can running for one host or a host group, and you know get file can't for a host group.
 72 
 73 Give you examples:
 74 
 75 If you want perform command:
 76     Usage: (for one host) ./paramiko-upload.py -H [hostip] -c [command]
 77     Example: ./paramiko-upload.py -H 10.100.139.245 -c 'ls -al'
 78 
 79     Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -c [command]
 80     Example: ./paramiko-upload.py -F 'ip.txt' -c 'ls -al'
 81 
 82 If you want send file:
 83 
 84     Usage: (for one host) ./paramiko-upload.py -H [hostip] -s [local_dir] [client_dir] [file]
 85     Example: ./paramiko-upload.py -H 10.100.139.245 -s '/opt/Felix' '/tmp' ip.txt
 86 
 87     Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -s [local_dir] [client_dir] [file]
 88     Example: ./paramiko-upload.py -F ip.txt -s '/opt/Felix' '/tmp' ip.txt
 89 
 90 If you want get file:
 91     Usage: (for one host) ./paramiko-upload.py -H [hostip] -g [local_dir] [client_dir] [file]
 92     Example: ./paramiko-upload.py -H 10.100.139.245 -g '/opt/Felix' '/tmp' ip.txt
 93 
 94 ip.txt file content like this:
 95     1.1.1.1
 96     2.2.2.2\033[0m
 97     """
 98     sys.exit()
 99 
100 #定义命令详情
101 
102 if __name__ == '__main__':
103 #定义账号、端口、密码等信息
104     username='root'
105     port=2222
106     password='abcdefg'
107 
108 
109     if len(sys.argv) > 2:
110         if sys.argv[1] == '-H':
111             if len(sys.argv) == 5 and sys.argv[3] == '-c':
112                 hostname=sys.argv[2]
113                 comm = sys.argv[4]
114                 perform_command(hostname, port, username, password, comm)
115             elif len(sys.argv) == 7 and sys.argv[3] == '-s':
116                 hostname=sys.argv[2]
117                 local_dir=sys.argv[4]
118                 client_dir=sys.argv[5]
119                 file=sys.argv[6]
120                 send_file(hostname,port,username,password,local_dir,client_dir,file)
121             elif len(sys.argv) == 7 and sys.argv[3] == '-g':
122                 hostname=sys.argv[2]
123     username='root'
124     port=2222
125     password='abcdefg'
126 
127 
128     if len(sys.argv) > 2:
129         if sys.argv[1] == '-H':
130             if len(sys.argv) == 5 and sys.argv[3] == '-c':
131                 hostname=sys.argv[2]
132                 comm = sys.argv[4]
133                 perform_command(hostname, port, username, password, comm)
134             elif len(sys.argv) == 7 and sys.argv[3] == '-s':
135                 hostname=sys.argv[2]
136                 local_dir=sys.argv[4]
137                 client_dir=sys.argv[5]
138                 file=sys.argv[6]
139                 send_file(hostname,port,username,password,local_dir,client_dir,file)
140             elif len(sys.argv) == 7 and sys.argv[3] == '-g':
141                 hostname=sys.argv[2]
142                 local_dir=sys.argv[4]
143                 client_dir=sys.argv[5]
144                 file=sys.argv[6]
145                 get_file(hostname,port,username,password,local_dir,client_dir,file)
146             else:
147                 help()
148         elif sys.argv[1] == '-F':
149             ip_file=sys.argv[2]
150             file_stat=os.path.exists(ip_file)
151             if file_stat:
152                 f=open(ip_file)
153                 ips=f.readlines()
154                 f.close()
155                 for ip in ips: 
156                     ip = ip.strip('\n')
157                     if re.match(r'^#', ip):
158                         continue
159                     if len(sys.argv) == 5 and sys.argv[3] == '-c':
160                         hostname = ip
161                         comm = sys.argv[4]
162                         perform_command(hostname, port, username, password, comm)
163                     elif len(sys.argv) == 7 and sys.argv[3] == '-s':
164                         hostname=ip
165                         local_dir=sys.argv[4]
166                         client_dir=sys.argv[5]
167                         file=sys.argv[6]
168                         send_file(hostname,port,username,password,local_dir,client_dir,file)
169                     else:
170                         help()
171             else:
172                 print "\033[31mERROR - Not found ip_file!\033[0m"
173                 help()
174         else:
175             help()
176     else:
177         help()

 

posted on 2018-04-12 10:49  charlie_ai  阅读(1894)  评论(0编辑  收藏  举报