2024-05-12 18:20:59 发布
网友
我想自动化通过ssh(putty)执行的相同操作。 使用putty连接之后,我的.bashrc被加载(这样我就可以使用别名了)。如果我尝试在Python中执行此操作,则别名sanity是不可见的:
.bashrc
sanity
sanity: command not found
使用source .bashrc不是解决办法。在
source .bashrc
从bash手册页:
bash
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
因此,如果要使用别名,必须首先设置expand_aliases选项。在
expand_aliases
因为您是在ssh上运行命令,所以没有运行登录shell,因此.bashrc不是源代码。在
请看这里的答案:https://superuser.com/questions/306530/run-remote-ssh-command-with-full-login-shell
编辑:
调用exec_command时尝试设置get_pty=True
get_pty=True
否则,请尝试强制登录shell
exec_command('bash -l -c "sanity;"')
从
bash
手册页:因此,如果要使用别名,必须首先设置
expand_aliases
选项。在因为您是在ssh上运行命令,所以没有运行登录shell,因此
.bashrc
不是源代码。在请看这里的答案:https://superuser.com/questions/306530/run-remote-ssh-command-with-full-login-shell
编辑:
调用exec_command时尝试设置
get_pty=True
否则,请尝试强制登录shell
相关问题 更多 >
编程相关推荐