该脚本可以实现:上传文件、下载文件、执行命令
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()
脚本如下:
- #!/usr/bin/env python
- #定义函数
- import paramiko
- import os
- import datetime
- import sys
- import re
- #定义基础命令格式
- def perform_command(hostname, port, username, password, comm):
- try:
- t=paramiko.Transport((hostname,port))
- t.connect(username=username,password=password)
- try:
- print "\033[33mIP:\033[0m", hostname
- print "\033[33mPerform command:\033[0m", comm
- print '\033[32m###########################################################\033[0m'
- s=paramiko.SSHClient()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(hostname = hostname, port = port, username = username, password = password)
- stdin,stdout,stderr=s.exec_command(comm)
- print stdout.read()
- print '\033[32m###########################################################\033[0m'
- print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
- s.close()
- except Exception:
- print "\033[31mERROR - Perform error\033[0m"
- except Exception:
- print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
-
- #定义发送文件格式
- def send_file(hostname,port,username,password,local_dir,client_dir,file):
- try:
- t=paramiko.Transport((hostname,port))
- t.connect(username=username,password=password)
- try:
- print "\033[33mIP:\033[0m", hostname
- print "\033[33mSend file:\033[0m", file
- print "\033[33mClient_dir:\033[0m", client_dir
- sftp=paramiko.SFTPClient.from_transport(t)
- sftp.put(os.path.join(local_dir,file),os.path.join(client_dir,file))
- t.close()
- print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
- except Exception:
- print "\033[31mERROR - Perform error\033[0m"
- except Exception:
- print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
- #定义下载文件格式
- def get_file(hostname,port,username,password,local_dir,client_dir,file):
- try:
- t=paramiko.Transport((hostname,port))
- t.connect(username=username,password=password)
- try:
- print "\033[33mIP:\033[0m", hostname
- print "\033[33mGet file:\033[0m", file
- print "\033[33mClient_dir:\033[0m", client_dir
- sftp=paramiko.SFTPClient.from_transport(t)
- sftp.get(os.path.join(client_dir,file),os.path.join(local_dir,file))
- t.close()
- print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
- except Exception:
- print "\033[31mERROR - Perform error\033[0m"
- except Exception:
- print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
- #定义帮助文件
- def help():
- print """
- \033[33mHelp:
-
- Welcome to use python script, this script can help you run commands, send files and get files.
-
- Use this script, you can running for one host or a host group, and you know get file can't for a host group.
-
- Give you examples:
-
- 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()
-
- #定义命令详情
-
- if __name__ == '__main__':
- #定义账号、端口、密码等信息
- username='root'
- port=2222
- password='abcdefg'
-
-
- if len(sys.argv) > 2:
- if sys.argv[1] == '-H':
- if len(sys.argv) == 5 and sys.argv[3] == '-c':
- hostname=sys.argv[2]
- comm = sys.argv[4]
- perform_command(hostname, port, username, password, comm)
- elif len(sys.argv) == 7 and sys.argv[3] == '-s':
- hostname=sys.argv[2]
- local_dir=sys.argv[4]
- client_dir=sys.argv[5]
- file=sys.argv[6]
- send_file(hostname,port,username,password,local_dir,client_dir,file)
- elif len(sys.argv) == 7 and sys.argv[3] == '-g':
- hostname=sys.argv[2]
- username='root'
- port=2222
- password='abcdefg'
-
-
- if len(sys.argv) > 2:
- if sys.argv[1] == '-H':
- if len(sys.argv) == 5 and sys.argv[3] == '-c':
- hostname=sys.argv[2]
- comm = sys.argv[4]
- perform_command(hostname, port, username, password, comm)
- elif len(sys.argv) == 7 and sys.argv[3] == '-s':
- hostname=sys.argv[2]
- local_dir=sys.argv[4]
- client_dir=sys.argv[5]
- file=sys.argv[6]
- send_file(hostname,port,username,password,local_dir,client_dir,file)
- elif len(sys.argv) == 7 and sys.argv[3] == '-g':
- hostname=sys.argv[2]
- local_dir=sys.argv[4]
- client_dir=sys.argv[5]
- file=sys.argv[6]
- get_file(hostname,port,username,password,local_dir,client_dir,file)
- else:
- help()
- elif sys.argv[1] == '-F':
- ip_file=sys.argv[2]
- file_stat=os.path.exists(ip_file)
- if file_stat:
- f=open(ip_file)
- ips=f.readlines()
- f.close()
- for ip in ips:
- ip = ip.strip('\n')
- if re.match(r'^#', ip):
- continue
- if len(sys.argv) == 5 and sys.argv[3] == '-c':
- hostname = ip
- comm = sys.argv[4]
- perform_command(hostname, port, username, password, comm)
- elif len(sys.argv) == 7 and sys.argv[3] == '-s':
- hostname=ip
- local_dir=sys.argv[4]
- client_dir=sys.argv[5]
- file=sys.argv[6]
- send_file(hostname,port,username,password,local_dir,client_dir,file)
- else:
- help()
- else:
- print "\033[31mERROR - Not found ip_file!\033[0m"
- help()
- else:
- help()
- else:
- help()
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31442725/viewspace-2148711/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31442725/viewspace-2148711/