ssh + here document + interactive mode

2019-08-26 02:16发布

Can I run a here document script over ssh on remote machine with interactive mode?

Code example is:

ssh -t xijing@ggzshop.com 'bash -s' <<EOF
sudo ls
......Other big scripts......
EOF

double -t won't work properly as well.

-----------------------------One possible solution:-------------------

After a lot of tries, I come up with following answers:

Script=`cat <<'EOF'
sudo ls
.....Big scripts.....
EOF`

ssh -t user@host ${Script}

which will allow user to type password in.

标签: bash ssh
3条回答
小情绪 Triste *
2楼-- · 2019-08-26 02:36

Solution of Xijing appears to work ok for me. However, I did a couple of cosmetic changes. First, for readability I used "dollar-parentheses" instead of backticks. For another thing I don't offer any explanation: Semicolons were needed to separate multiple commands in Script snippet even though commands are written on separate lines. My test:

Script=$( cat <<'HERE'
hostname;
cat /etc/issue;
sudo id
HERE
)

ssh -t user@host ${Script}

Sudo password will be asked in a normal manner, no need to omit that.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-26 02:42

No, I don't think you can run interactive scripts like that.

To achieve what you want, you could create dedicated users for your common admin tasks that can run admin commands with sudo without password. Next, setup ssh key authentication to login as the dedicated users and perform the necessary tasks.

查看更多
看我几分像从前
4楼-- · 2019-08-26 02:48

It is not necessary to use semicolons to separate multiple commands in Script if there are quotes around it.

Script="$( cat <<'HERE'
hostname;
cat /etc/issue;
sudo id
HERE
)"


- ssh -t user@host ${Script}
+ ssh -t user@host "${Script}"

# alternative (not recommended)
# set IFS variable to null string to avoid deletion of newlines \n in unquoted variable expansion
export IFS=''
ssh -t user@host ${Script}
查看更多
登录 后发表回答