Subprocess 使用python子流程使用ssh登录到远程网络设备
在上面的代码中,它成功登录到网络设备,但我无法通过输入exit退出显示>的设备程序。我不知道我错过了什么,请帮忙Subprocess 使用python子流程使用ssh登录到远程网络设备,subprocess,Subprocess,在上面的代码中,它成功登录到网络设备,但我无法通过输入exit退出显示>的设备程序。我不知道我错过了什么,请帮忙 def ssh(ip): try: cmd = "plink -ssh {}@{} -pw {} -q + " exit" ".format( u , ip , pw ) process = subprocess.Popen( cmd , shell=True , stdin=subprocess.PIPE ,
def ssh(ip):
try:
cmd = "plink -ssh {}@{} -pw {} -q + " exit" ".format( u , ip , pw )
process = subprocess.Popen( cmd , shell=True , stdin=subprocess.PIPE ,
stdout=subprocess.PIPE,stderr=subprocess.PIPE, encoding='utf-8')
a,b = process.communicate()
print( "a:{},b:{}".format( a , b ) )
succ = ''.join( str( e ) for e in a )
err = ''.join(str(e) for e in b)
succ = succ.rstrip()
process.stdout.close()
print("A-1:{}".format(err))
succout=process.stdout.readlines()
print("Success:{}".format(succ))
if "Access granted:" in succ:
process.communicate(b'\r\n')
process.stdin.write( b"exit\r\n" )
print( "Successfully authenticated" )
output = 'Success' + " " + u
ssh_login_status = output
final_list[ 'SSH_AUTH' ].append( ssh_login_status )
final_list[ 'IP' ].append( ip )
break
else:
output = "Unable to establish SSH connection"
print( "Invalid authentication" )
final_list[ 'SSH_AUTH' ].append( output ) # changed later to error message suggested
final_list[ 'IP' ].append( ip )
except Exception as e:
raise e
except socket.error as e:
output = "Connection Error"
ssh_login_status = output
final_list[ 'SSH_AUTH' ].append( output )
print( output )
process.kill()