需要在交换设备上尝试许多SSH,直到它使用python2.7 Popen连接
从linux服务器尝试访问远程ssh以使用脚本python使用Popen或subprocess.check_output(如果已连接中断循环)进行切换,并发送用户名和密码(如果未尝试再次发送ssh)
导入getpass
从子流程导入Popen,PIPE
Devi = raw_input('输入要在其上ssh的设备的IP:')
numbs = raw_input('输入尝试ssh的次数:')
usen = raw_input('输入您的用户名:')
password = getpass.getpass(“ Password:”)
同时麻木!= 0:
p = Popen(['ssh',Devi],stdout=PIPE)
while p.poll() is None:
line=p.stdout.readline().strip()
print(line)
search_point1="Connect failed"
search_point2="username:"
for KKK in line:
if KKK in search_point1:
break
elif KKK in search_point2:
Popen(usen,stdin=PIPE)
Popen(password,stdin=PIPE)
break
else:
continue
numbs-=1
尝试使用ssh成功连接到交换机
使用子流程修改的代码:
导入getpass
导入子进程
Devi = raw_input('输入要在其上ssh的设备的IP:')
numbs = raw_input('输入尝试ssh的次数:')
usen = raw_input('输入您的用户名:')
password = getpass.getpass(“ Password:”)
同时麻木!= 0:
kkk = subprocess.check_output(['ssh',Devi])
print(kkk)
p=kkk.readlines()
while p:
search_point1="Connection refused"
search_point2="username:"
for KKK in p:
if KKK in search_point1:
break
elif KKK in search_point2:
print(kkk)
break
else:
continue
numbs-=1