切换用户后,Shell脚本会自动从SSH部分退出(通过pbrun命令)

时间:2014-10-07 01:00:10

标签: bash shell ssh su

我有以下代码..

ssh some_user@server << EOF
echo 'Successfully connected to the server'
pbrun previlige -u user
ls
pwd
id
...few more commands
EOF
if [ $? -eq 0 ]
then
     echo 'Successful Execution of the last command in ssh'
fi

现在我在上面的代码中遇到的问题是,在用户通过 pbrun 命令切换后,脚本会自动退出(即它永远不会执行 ls pwd 和后续命令)。为了使事情变得复杂,这个问题是间歇性的。有时我会得到所有后续命令的正确输出,有时候我不会。

问题发生时的输出:

Successfully connected to the server 
su from some_user to user at Mon Oct 6 09:47:00  MDT 2014 
Successful Execution of the last command in ssh

在上述情况下,切换用户后,它永远不会显示后续的命令输出

问题未发生时的输出

Successfully connected to the server 
su from some_user to user at Mon Oct 6 09:47:00 MDT 2014 
Logs migrate.properties prereq.sh src_exp.sh src_mig.exp 
/home/venus/ 
uid=* gid=* groups=**** 
Successful Execution of the last command in ssh

这有什么原因/解决方法吗?即使是一个解决方法对我来说也应该没问题。谢谢!

1 个答案:

答案 0 :(得分:1)

传递内部heredoc可确保将后续内容提供给pbrun的stdin,而不是仅在pbrun退出后在外壳中调用,否则会发生这种情况:

ssh some_user@server <<'OUTER_EOF'
echo 'Successfully connected to the server'
pbrun -u user bash <<'INNER_EOF'
# this is inside both ssh and pbrun
ls
pwd
id
INNER_EOF
# this is inside ssh, but not inside pbrun
OUTER_EOF