简体   繁体   English

使用没有 tty 的 heredoc 在 ssh 后获取 bash 提示

[英]Get bash prompt after ssh with heredoc with no tty

I would like to ssh onto a remote machine, automatise some code execution with heredoc and then provide the user with a bash prompt to manually execute further commands as needed.我想通过 ssh 连接到远程机器,使用 heredoc 自动执行一些代码,然后为用户提供 bash 提示以根据需要手动执行进一步的命令。

So far I tried (option 1)到目前为止,我尝试过(选项 1)

ssh user@host << 'EOF'
echo "Hello"
EOF

This prints out "Hello" just fine, but closes the ssh connection and exits the script.这会很好地打印出“Hello”,但会关闭 ssh 连接并退出脚本。 Also I get the infamous "Pseudo-terminal will not be allocated because stdin is not a terminal."我也得到了臭名昭著的“伪终端将不会被分配,因为标准输入不是终端。”

Same result with a single -t switch.单个 -t 开关的结果相同。

Next I tried (option 2)接下来我尝试了(选项2)

ssh -tt user@host << 'EOF'
echo "Hello `uname -n`"
EOF

Which does the job, but produces a set of weird outputs:哪个可以完成工作,但会产生一组奇怪的输出:

Success. Logging you in...
echo "Hello"
echo "Hello"
[remote]$ echo "Hello"
Hello
[remote]$ <

Tried without using heredoc, like尝试不使用heredoc,例如

ssh user@host 'echo "Hello"; /bin/bash' 

but the echo will be executed on a different shell但 echo 将在不同的 shell 上执行

So far, option 2 got me the closest.到目前为止,选项 2 让我最接近。 Any other solutions would be welcome!欢迎任何其他解决方案!

You could try setting some ssh options on the command line.您可以尝试在命令行上设置一些 ssh 选项。 These can also be set in the user's ~/.ssh/config file.这些也可以在用户的​​ ~/.ssh/config 文件中设置。 Replace remote-host with user@host or equivalent ssh config Host pattern.用 user@host 或等效的 ssh 配置主机模式替换远程主机

single line单线

ssh -o RequestTTY='yes' -o RemoteCommand='echo "Hello $(uname -n)"; bash -l' remote-host

multiple lines多行

ssh -o RequestTTY='yes' -o RemoteCommand='
echo "hello from $(uname -n)"
echo "I am $(whoami)"
bash -l
' remote-host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM