1

当我在 Eclipse 中运行以下代码时,getPass 返回一个以回车符结尾的字符串。

但是,当我在命令提示符下运行相同的代码时,它运行没有任何问题。

import paramiko
import getpass

userPwd = getpass.getpass()
print ('You have entered ',userPwd)

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('ese.rnd.sanl.se', username='abc', 
    password=userPwd)
stdin,stdout,stderr=ssh.exec_command("ls")
data1=stdout.read().splitlines();

for line in data1:
    print (line)

输出(日食):

Warning: Password input may be echoed.
Password: Mypassword@123
('You have entered ', 'Mypassword@123\r')

输出(命令提示符):

Warning: Password input may be echoed.
Password: Mypassword@123
('You have entered ', 'Mypassword@123')

任何人都可以解释这种歧义吗?

谢谢!!

4

1 回答 1

0

我怀疑 Eclipse 设置为使用 Windows 换行符,即“\r\n”。您需要将其设置为使用 UNIX 新行 ('\n')

基本上,这意味着 python 方法 'getpass()' 正在切割缓冲区中的最后一个字符。由于您要给出两个字符,因此缺少 \r。

在这里查看如何修改此选项。

于 2016-02-10T22:39:20.400 回答