我想自动执行我通过 ssh (putty) 执行的相同操作。 使用腻子连接后,我的 .bashrc 被加载(所以我可以使用别名)。如果我尝试在 Python 中执行此操作,别名 sanity 是不可见的:
sanity: command not found
使用 source .bashrc 不是解决方案。
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxxxxxx', username='x', password='x', key_filename=None, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command(
"""
sanity;
""")
stdout.flush()
for line in stdout:
print line
print "END"
print stderr.read()
ssh.close()
请您参考如下方法:
因为您正在通过 ssh 运行命令,所以您没有运行登录 shell,所以 .bashrc 不是源代码。
在这里查看答案:https://superuser.com/questions/306530/run-remote-ssh-command-with-full-login-shell
编辑:
尝试在调用 exec_command 时设置 get_pty=True
否则尝试强制登录 shell
exec_command('bash -l -c "sanity;"')
