密码提示在python代码中不可见

时间:2019-06-30 14:39:55

标签: python-3.x

我正在尝试自动建立到远程“ server2”的连接,但是我没有看到密码提示以终止该连接并访问“ server2”。 在连接到server2之前,出于安全原因,我首先需要使用用户名和OPC(一次密码)访问网关,对于这一部分而言,脚本可以正常工作,但与server2的连接的第二部分则无法工作。

我的代码是:

import socket,time
from ssh2.session import Session

# gateway access
host= '155.90.32.122'
user= 'root'
password=input ('give me the OPC: ')
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host,22))

#Access to gateway 
session=Session()
session.handshake(sock)
session.userauth_password(user,password)

channel1= session.open_session()
channel1.shell()

#Access to server2 
channel1.write('ssh server2\n')

#ceasing the password 
channel1.write('rootroot\n')
time.sleep(3)
size,data=channel1.read()
print (data.decode())
channel1.close()
print("exit status : {0}",format(channel1.get_exit_status()))

输出为:

give me the OPC: 0312
Run help to get a list of available commands
egw > ssh server2
Connecting to server2 as user root
exit status : {0} 0
Process finished with exit code 0

,而预期的输出应带有密码提示:

give me the OPC: 0312
Run help to get a list of available commands
egw > ssh server2
Connecting to server2 as user root
Password :

server2>

0 个答案:

没有答案