python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)...
生活随笔
收集整理的這篇文章主要介紹了
python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?1?#-*-?coding:?utf-8?-*-
?2?#批量操作linux服務器(執行命令,上傳,下載)
?3?#!/usr/bin/python
?4?import?paramiko
?5?import?datetime
?6?import?os
?7?import?threading
?8?def?ssh2(ip,username,passwd,cmd):
?9?????try:
10?????????paramiko.util.log_to_file('paramiko________.log')
11?????????ssh?=?paramiko.SSHClient()
12?????????ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13?????????ssh.connect(ip,22,username,passwd,timeout=5)
14?????????for?m?in?cmd:
15?????????????stdin,stdout,stderr?=?ssh.exec_command(m)
16?????????????#stdin.write("Y")???#簡單交互,輸入?‘Y’
17?????????????out?=?stdout.readlines()
18?????????????#?outerr?=?stderr.readlines()
19?????????????#屏幕輸出
20?????????????for?o?in?out:
21?????????????????print?o,
22?????????????#?for?i?in?outerr:
23?????????????#?????print?i,
24?????????????????print?'%s\tOK\n'%(ip)
25?????????ssh.close()
26?????except?:
27?????????print?'%s\tError\n'%(ip)
28?def?download(ip,?username,?passwd,?local_dir,?remote_dir):
29????try:
30?????????paramiko.util.log_to_file('paramiko_download.log')
31?????????t?=?paramiko.Transport((ip,22))
32?????????t.connect(username=username,password=passwd)
33?????????sftp?=?paramiko.SFTPClient.from_transport(t)
34?????????files?=?sftp.listdir(remote_dir)
35?
36?
37?????????for?f?in?files:
38?????????????print?''
39?????????????print?'############################'
40?????????????print?'Beginning?to?download?file??from?%s??%s?'?%?(ip,?datetime.datetime.now())
41?????????????print?'Downloading?file:',?os.path.join(remote_dir,?f)
42?????????????sftp.get(os.path.join(remote_dir,?f),?os.path.join(local_dir,?f))#下載
43?????????????print?'Download?file?success?%s?'?%?datetime.datetime.now()
44?????????????print?''
45?????????????print?'############################'
46?????????t.close()
47????except:
48?????????print?"connect?error!"
49?
50?
51?def?upload(ip,?username,?passwd,?local_dir,?remote_dir):
52?????try:
53?????????paramiko.util.log_to_file('paramiko_upload.log')
54?????????t?=?paramiko.Transport((ip,?22))
55?????????t.connect(username=username,?password=passwd)
56?????????sftp?=?paramiko.SFTPClient.from_transport(t)
57?????????#files?=?sftp.listdir(remote_dir)
58?????????files?=?os.listdir(local_dir)
59?????????for?f?in?files:
60?????????????print?''
61?????????????print?'############################'
62?????????????print?'Beginning?to?upload?file??to?%s??%s?'?%?(ip,?datetime.datetime.now())
63?????????????print?'Uploading?file:',?os.path.join(local_dir,?f)
64?????????????sftp.put(os.path.join(local_dir,?f),?os.path.join(remote_dir,?f))#上傳
65?????????????print?'Upload?file?success?%s?'?%?datetime.datetime.now()
66?????????????print?''
67?????????????print?'############################'
68?????????t.close()
69?????except:
70?????????print?"connect?error!"
71?????????print?ip,?"fail!"
72?
73?
74?if?__name__=='__main__':
75?#??cmd?=?['ls?-lh?/export/servers/mysql/log/mysql.log']#你要執行的命令列表
76?????cmds=open("D:\\batch\\cmds.txt")?#從文件讀取命令
77?????cmd=cmds.readlines()
78?????cmds.close()
79?????username?=?"root"??#用戶名
80?????#passwd?=?"xxxx"????#單臺服務器時啟用----------------------
81?????#ip='192.168.12.19'??#單臺服務器時啟用----------------------
82?????local_dir?=?"D:\\batch\\tmp"??#本地服務器目錄
83?????remote_dir?=?"/tmp/"?#遠程服務器目錄
84?????#threads?=?[]???#多線程
85?????print?"Begin......"
86?????hosts=open("D:\\batch\\list.txt")?#本地服務器列表
87?????host=hosts.readlines()
88?????hosts.close()
89?????for?list?in?host:
90?????????line?=?list.split()
91?????????ip?=?line[0].strip()
92?????????pwd?=?line[1].strip()
93?????????#?print?ip,
94?????????#?print?pwd,
95?????????#?b=threading.Thread(target=upload,args=(ip,username,pwd,local_dir,remote_dir))
96?????????#?b.start()
97?????????a=threading.Thread(target=ssh2,args=(ip,username,pwd,cmd))
98?????????a.start()
?2?#批量操作linux服務器(執行命令,上傳,下載)
?3?#!/usr/bin/python
?4?import?paramiko
?5?import?datetime
?6?import?os
?7?import?threading
?8?def?ssh2(ip,username,passwd,cmd):
?9?????try:
10?????????paramiko.util.log_to_file('paramiko________.log')
11?????????ssh?=?paramiko.SSHClient()
12?????????ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13?????????ssh.connect(ip,22,username,passwd,timeout=5)
14?????????for?m?in?cmd:
15?????????????stdin,stdout,stderr?=?ssh.exec_command(m)
16?????????????#stdin.write("Y")???#簡單交互,輸入?‘Y’
17?????????????out?=?stdout.readlines()
18?????????????#?outerr?=?stderr.readlines()
19?????????????#屏幕輸出
20?????????????for?o?in?out:
21?????????????????print?o,
22?????????????#?for?i?in?outerr:
23?????????????#?????print?i,
24?????????????????print?'%s\tOK\n'%(ip)
25?????????ssh.close()
26?????except?:
27?????????print?'%s\tError\n'%(ip)
28?def?download(ip,?username,?passwd,?local_dir,?remote_dir):
29????try:
30?????????paramiko.util.log_to_file('paramiko_download.log')
31?????????t?=?paramiko.Transport((ip,22))
32?????????t.connect(username=username,password=passwd)
33?????????sftp?=?paramiko.SFTPClient.from_transport(t)
34?????????files?=?sftp.listdir(remote_dir)
35?
36?
37?????????for?f?in?files:
38?????????????print?''
39?????????????print?'############################'
40?????????????print?'Beginning?to?download?file??from?%s??%s?'?%?(ip,?datetime.datetime.now())
41?????????????print?'Downloading?file:',?os.path.join(remote_dir,?f)
42?????????????sftp.get(os.path.join(remote_dir,?f),?os.path.join(local_dir,?f))#下載
43?????????????print?'Download?file?success?%s?'?%?datetime.datetime.now()
44?????????????print?''
45?????????????print?'############################'
46?????????t.close()
47????except:
48?????????print?"connect?error!"
49?
50?
51?def?upload(ip,?username,?passwd,?local_dir,?remote_dir):
52?????try:
53?????????paramiko.util.log_to_file('paramiko_upload.log')
54?????????t?=?paramiko.Transport((ip,?22))
55?????????t.connect(username=username,?password=passwd)
56?????????sftp?=?paramiko.SFTPClient.from_transport(t)
57?????????#files?=?sftp.listdir(remote_dir)
58?????????files?=?os.listdir(local_dir)
59?????????for?f?in?files:
60?????????????print?''
61?????????????print?'############################'
62?????????????print?'Beginning?to?upload?file??to?%s??%s?'?%?(ip,?datetime.datetime.now())
63?????????????print?'Uploading?file:',?os.path.join(local_dir,?f)
64?????????????sftp.put(os.path.join(local_dir,?f),?os.path.join(remote_dir,?f))#上傳
65?????????????print?'Upload?file?success?%s?'?%?datetime.datetime.now()
66?????????????print?''
67?????????????print?'############################'
68?????????t.close()
69?????except:
70?????????print?"connect?error!"
71?????????print?ip,?"fail!"
72?
73?
74?if?__name__=='__main__':
75?#??cmd?=?['ls?-lh?/export/servers/mysql/log/mysql.log']#你要執行的命令列表
76?????cmds=open("D:\\batch\\cmds.txt")?#從文件讀取命令
77?????cmd=cmds.readlines()
78?????cmds.close()
79?????username?=?"root"??#用戶名
80?????#passwd?=?"xxxx"????#單臺服務器時啟用----------------------
81?????#ip='192.168.12.19'??#單臺服務器時啟用----------------------
82?????local_dir?=?"D:\\batch\\tmp"??#本地服務器目錄
83?????remote_dir?=?"/tmp/"?#遠程服務器目錄
84?????#threads?=?[]???#多線程
85?????print?"Begin......"
86?????hosts=open("D:\\batch\\list.txt")?#本地服務器列表
87?????host=hosts.readlines()
88?????hosts.close()
89?????for?list?in?host:
90?????????line?=?list.split()
91?????????ip?=?line[0].strip()
92?????????pwd?=?line[1].strip()
93?????????#?print?ip,
94?????????#?print?pwd,
95?????????#?b=threading.Thread(target=upload,args=(ip,username,pwd,local_dir,remote_dir))
96?????????#?b.start()
97?????????a=threading.Thread(target=ssh2,args=(ip,username,pwd,cmd))
98?????????a.start()
轉載于:https://www.cnblogs.com/zhuhongbao/p/3820452.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信/qq消息-定时自动循环发送
- 下一篇: 【转载】实现软件架构质量属性的战术