|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识:Linux是一套免费使用和自由传播的类Unix操作系统。
比来在进修python,感到很不错,特殊是完成批量/并发处置。完成起来异常便利,比shell便利许多。刚学python一个月,在这留个记载。上面分享一段代码。多提建议/看法~(QQ群交换:24967504)
需求:批量检查/比较线上办事器的状况情形,如uptime、df-h、MD5sum文件等等。本盘算让履行敕令经由过程传参停止、暗码经由过程交互输出(3次纰谬加入)。前面再完美~~
#-*-coding:utf-8-*-
#!/usr/bin/python
importparamiko
importthreading
defssh2(ip,username,passwd,cmd):
try:
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
formincmd:
stdin,stdout,stderr=ssh.exec_command(m)
stdin.write("Y")
out=stdout.readlines()
foroinout:
printo,
print[OK]%s%(ip),
print=========================================================================
ssh.close()
except:
print[Error]%s%(ip),
print=========================================================================
defmain():
cmd=[uptime]#履行敕令
username="root"
passwd="暗码"
threads=[4]
f=file(list.txt)#ip列表
whileTrue:
ip=f.readline()
iflen(ip)==0:
break
a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
a.start()
f.close()
if__name__==__main__:
main()
履行成果:
[root@bw-vm-soft~]#pythonssh2.py
13:31:28up514days,7:28,1user,loadaverage:10.27,9.44,9.03
[OK]192.168.13.116
=========================================================================
13:31:28up514days,8:04,1user,loadaverage:5.99,6.05,6.46
[OK]192.168.13.117
=========================================================================
针对后面的代码,还存在的成绩停止了改良:
1、去失落明文暗码,经由过程交互输出暗码
2、经由过程传参履行相干敕令,增长灵巧性
3、异常处置
4、其他成绩待发明,接洽QQ群:24967504
详细代码:
#!/usr/bin/envpython
#-*-coding:utf-8-*-
importsys
importparamiko
importthreading
importgetpass
defmain():
try:
cmd=sys.argv[1:]
username="root"
passwd=getpass.getpass(Pleaseinputpassword:)
threads=[4]
f=file(list.txt)
whileTrue:
ip=f.readline()
iflen(ip)==0:
break
a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
a.start()
f.close()
except:
pass
defssh2(ip,username,passwd,cmd):
try:
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
formincmd:
stdin,stdout,stderr=ssh.exec_command(m)
stdin.write("Y")
out=stdout.readlines()
foroinout:
printo,
print[OK]%s%(ip),
print=========================================================================
ssh.close()
except:
print[Error]%s%(ip),
print=========================================================================
if__name__==__main__:
main()
履行成果:
[root@bw-vm-softtest]#pythonssh2.pymd5sum/usr/local/webserver/nginx/conf/nginx.conf
Pleaseinputpassword:
33066988953224e936028b341f4e1337/usr/local/webserver/nginx/conf/nginx.conf
[OK]192.168.13.116
=========================================================================
33066988953224e936028b341f4e1337/usr/local/webserver/nginx/conf/nginx.conf
[OK]192.168.13.117
=========================================================================
若暗码纰谬
[Error]192.168.13.116
=========================================================================
[Error]192.168.13.117
小知识:社区支持的免费Linux发行版一般不会从商业支持中寻求什么好处,但对CentOS来说,这种情况即将改变。 |
|