Created
December 24, 2015 05:22
-
-
Save conanca/49138503ac93757a0ab7 to your computer and use it in GitHub Desktop.
通过自定义密码表自动登录系统,并传输 rsa 公钥和执行命令
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*-coding:utf-8 -*- | |
#author:shiying;tongning;hxg | |
##通过自定义密码表自动登录系统,并传输 rsa 公钥和执行命令 | |
import paramiko | |
import threading | |
import time | |
import pdb | |
iplist = [] | |
passwd=[自定义的密码列表……] | |
localpath=r"/root/.ssh/id_rsa.pub" | |
remotepath=r"/root/.ssh/id_rsa.pub" | |
file = open("./iplist") | |
for line in file: | |
line=line.strip('\n') | |
iplist.append(line) | |
def writelog(status): | |
rsa_error=open(r"/temp/py/rsa_error.log","a") | |
rsa_error.write(status) | |
rsa_error.close() | |
def transport(ip,pswd): | |
try: | |
t = paramiko.Transport((ip,22)) | |
t.connect(username="root", password=pswd) | |
sftp=paramiko.SFTPClient.from_transport(t) | |
sftp.put(localpath,remotepath) | |
t.close() | |
print status | |
writelog(status) | |
except: | |
print status | |
writelog(status) | |
def create_ssh(): | |
ssh=paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
return ssh | |
def ssh(ip,passwd,cmd): | |
count = 1 | |
for pswd in passwd: | |
#这里设置密码探测次数不能大于 10,为什么?自己试试就知道了 | |
if count % 10 == 1: | |
if count != 1: | |
ssh.close() | |
print 'create_policy '+ str(count) | |
ssh=create_ssh() | |
count += 1 | |
try: | |
transport(ip,pswd) | |
ssh.connect(ip,22,'root',pswd,timeout=5,allow_agent=False,look_for_keys=False) | |
for cmd in cmds: | |
time.sleep(2) | |
stdin, stdout, stderr = ssh.exec_command(cmd) | |
stdin.write("Y") | |
print stdout.readlines() | |
break | |
except: | |
print 'connect_ERROR!' | |
continue | |
ssh.close() | |
if __name__=='__main__': | |
#这里命令写的是全路径,为什么?自己试试就知道了 | |
cmds=["cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys","cp /usr/local/nagios/etc/nrpe.cfg | |
/usr/local/nagios/etc/nrpe.cfg.bak","sed -i 's/allowed_hosts=/&192.168.70.70,/g' | |
/usr/local/nagios/etc/nrpe.cfg",'service nrpe restart'] | |
#cmds=["uptime",'w'] | |
threads=[len(iplist)] | |
print '**** threading start ****' | |
for ip in iplist: | |
thread=threading.Thread(target=ssh,args=(ip,passwd,cmds)) | |
thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment