通过paramiko加载ssh .bashrc

Bar*_*cki 1 python bash paramiko

我想通过ssh(putty)自动执行相同的操作。使用腻子连接后,我的计算机.bashrc已加载(因此我可以使用别名)。如果我尝试在Python中执行此操作,则别名sanity是不可见的:

sanity: command not found
Run Code Online (Sandbox Code Playgroud)

使用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()
Run Code Online (Sandbox Code Playgroud)

scy*_*ale 5

因为您正在通过ssh运行命令,所以您没有在运行登录外壳,因此.bashrc也没有来源。

在此处查看答案:https : //superuser.com/questions/306530/run-remote-ssh-command-with-full-login-shell

编辑:

get_pty=True在调用exec_command时尝试设置

否则尝试强制登录外壳

exec_command('bash -l -c "sanity;"')
Run Code Online (Sandbox Code Playgroud)