日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)...

發(fā)布時(shí)間:2023/12/10 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?1?#-*-?coding:?utf-8?-*-
?2?#批量操作linux服務(wù)器(執(zhí)行命令,上傳,下載)
?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']#你要執(zhí)行的命令列表
76?????cmds=open("D:\\batch\\cmds.txt")?#從文件讀取命令
77?????cmd=cmds.readlines()
78?????cmds.close()
79?????username?=?"root"??#用戶名
80?????#passwd?=?"xxxx"????#單臺(tái)服務(wù)器時(shí)啟用----------------------
81?????#ip='192.168.12.19'??#單臺(tái)服務(wù)器時(shí)啟用----------------------
82?????local_dir?=?"D:\\batch\\tmp"??#本地服務(wù)器目錄
83?????remote_dir?=?"/tmp/"?#遠(yuǎn)程服務(wù)器目錄
84?????#threads?=?[]???#多線程
85?????print?"Begin......"
86?????hosts=open("D:\\batch\\list.txt")?#本地服務(wù)器列表
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()

轉(zhuǎn)載于:https://www.cnblogs.com/zhuhongbao/p/3820452.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。