大网数据广告活动
服务器运维问答

sudo followed by another command in ssh heredoc not working

· 52CE 我爱测

This is my script: #!/bin/sh echo "I'm local" ssh -t -t remote.server << 'EOF' sudo echo "I'm remote and sudo" echo "I'm remote but not sudo" exit EOF This simply doesn't work, maybe because the line echo "I'm not sudo" is suppli…

问题原文

原帖链接 · akai · CC BY-SA 4.0

This is my script:

#!/bin/sh

echo "I'm local"

ssh -t -t remote.server << 'EOF'
    sudo echo "I'm remote and sudo"
    echo "I'm remote but not sudo"
    exit
EOF

This simply doesn't work, maybe because the line echo "I'm not sudo" is supplied as a password.

How can I make it work, without:

  • feeding the password directly to the script
  • enabling password-less sudo, or
  • making everything sudo?

Or, perhaps my approach is completely wrong or unusual for this purpose?

已采纳解答

原帖链接 · dave · CC BY-SA 4.0

In my opinion the only way is to split into two separate ssh commands:

#!/bin/sh

echo "I'm local"

ssh -t remote.server "sudo echo \"I'm remote and sudo\""
ssh -t remote.server "echo \"I'm remote but not sudo\""

The redirection which you use, will always redirect whole block to the command and will not wait for any subcommand success (or fail).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。