python 3.0 pexpect 模块的问题
Issue with python 3.0 pexpect module
这是我编写的用于自动 ssh 到 linux 的一段基本代码,但每次它进入 cases==0,这意味着它每次都在思考 "newkey"/ (yes/no):
请帮我解决。我卡在基础层了
#!/home/python/Python-3.4.3/python
import subprocess;
import pexpect;
def f1_input():
global server, id, password, commands;
server = input("Enter Server: ");
id = input("Enter User ID: ");
password = input("Enter Password: ");
commands = input("Enter Commands: ");
return server, id, password, commands;
def f2_exec():
child = pexpect.spawn('ssh %s@%s %s'%(id,server,commands));
newkey = 'Are you sure you want to continue connecting (yes/no)? ';
passkey = 'password:';
cases = child.expect([newkey, passkey, pexpect.EOF]);
print("cases=", cases);
if cases==0:
print("cases=", cases);
child.sendline('yes');
child.expect('password:');
child.sendline(password);
child.expect(pexpect.EOF);
print(child.before);
elif cases==1:
print("cases=", cases);
child.sendline(password);
child.expect(pexpect.EOF);
print(child.before);
elif cases==2:
print("cases=", cases);
print("Timeout!!!");
else:
print("cases=", cases);
print("EXIT");
f1_input();
f2_exec();
你的问题出在 ssh 上,应该可以毫无预料地重现。它可能无法写入 ~/.ssh/known_hosts 文件,或选项 StrictHostKeyChecking=no in ~/.ssh/config 或类似文件。
此外,您的解决方案最好使用 sshpass(1) 等程序或 paramiko 等 python 模块解决。
这是我编写的用于自动 ssh 到 linux 的一段基本代码,但每次它进入 cases==0,这意味着它每次都在思考 "newkey"/ (yes/no):
请帮我解决。我卡在基础层了
#!/home/python/Python-3.4.3/python
import subprocess;
import pexpect;
def f1_input():
global server, id, password, commands;
server = input("Enter Server: ");
id = input("Enter User ID: ");
password = input("Enter Password: ");
commands = input("Enter Commands: ");
return server, id, password, commands;
def f2_exec():
child = pexpect.spawn('ssh %s@%s %s'%(id,server,commands));
newkey = 'Are you sure you want to continue connecting (yes/no)? ';
passkey = 'password:';
cases = child.expect([newkey, passkey, pexpect.EOF]);
print("cases=", cases);
if cases==0:
print("cases=", cases);
child.sendline('yes');
child.expect('password:');
child.sendline(password);
child.expect(pexpect.EOF);
print(child.before);
elif cases==1:
print("cases=", cases);
child.sendline(password);
child.expect(pexpect.EOF);
print(child.before);
elif cases==2:
print("cases=", cases);
print("Timeout!!!");
else:
print("cases=", cases);
print("EXIT");
f1_input();
f2_exec();
你的问题出在 ssh 上,应该可以毫无预料地重现。它可能无法写入 ~/.ssh/known_hosts 文件,或选项 StrictHostKeyChecking=no in ~/.ssh/config 或类似文件。
此外,您的解决方案最好使用 sshpass(1) 等程序或 paramiko 等 python 模块解决。