【python】用python指令碼實現ansible的推送、下載、上傳檔案功能
該指令碼可以實現:上傳檔案、下載檔案、執行命令
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 教你如何實現c#檔案上傳下載功能C#
- 檔案上傳和下載功能
- 【Python】python-django上傳下載功能PythonDjango
- java實現sftp檔案的上傳下載JavaFTP
- JavaWeb之實現檔案上傳與下載原始碼JavaWeb原始碼
- Feign實現檔案上傳下載
- Jsp+Servlet實現檔案上傳下載(一)--檔案上傳JSServlet
- 用C++實現下載檔案的功能C++
- vue實現Excel檔案的上傳與下載VueExcel
- 前端實現檔案下載和拖拽上傳前端
- SpringMVC實現檔案上傳&下載(2)SpringMVC
- python寫的FTP簡單上傳下載檔案薦PythonFTP
- Python介面自動化——檔案上傳/下載介面Python
- JavaWeb之實現檔案上傳與下載工具JavaWeb
- JavaWeb之實現檔案上傳與下載元件JavaWeb元件
- JavaWeb之實現檔案上傳與下載示例JavaWeb
- Spring Boot + thymeleaf 實現檔案上傳下載Spring Boot
- 使用Vue+go實現前後端檔案的上傳下載,csv檔案上傳下載可直接照搬VueGo後端
- python selenium +autoit實現檔案上傳 --實踐Python
- python+selenium+autoit實現檔案上傳Python
- Flutter 實現檔案下載功能Flutter
- Java實現檔案下載功能Java
- Django 實現下載檔案功能Django
- 檔案上傳下載
- .net web core 如何編碼實現檔案上傳功能Web
- Java實現上傳檔案到Oracle及從Oracle下載檔案JavaOracle
- JavaWeb之實現檔案上傳與下載外掛JavaWeb
- JavaWeb之實現檔案上傳與下載例項JavaWeb
- React中使用fetch實現檔案上傳下載React
- 【node】檔案上傳功能簡易實現
- 自定義檔案上傳功能實現方法
- 檔案的上傳與下載
- 檔案上傳/下載後臺程式碼
- Linux使用Shell指令碼實現ftp的自動上傳下載Linux指令碼FTP
- 基於SpringWeb MultipartFile檔案上傳、下載功能SpringWeb
- 檔案上傳與下載
- JAVA檔案上傳下載Java
- Vertx 檔案上傳下載