【问题标题】:Run sudo command over ssh通过 ssh 运行 sudo 命令
【发布时间】:2016-02-26 01:50:40
【问题描述】:

我需要通过 ssh 以 sudo 身份运行命令:-

ssh ${SSH_SERVER} -l "sudo <command>" 

我想在脚本中获取密码并将其提供给 sudo - 类似的东西。

export PASSWORD=<From user>
ssh ${SSH_SERVER} -l "sudo <command> | echo $PASSWORD"

我无法编写这样的脚本,需要帮助。

bash -版本 GNU bash,版本 4.1.2(1)-release (x86_64-redhat-linux-gnu) 版权所有 (C) 2009 Free Software Foundation, Inc. 许可 GPLv3+:GNU GPL 版本 3 或更高版本http://gnu.org/licenses/gpl.html

================编辑====================

我尝试了下面给出的建议,但出现错误。

[user@my-host ~]$ cat temp.sh 
#!/bin/bash -xv
function f1 {
### Do NOT indent these lines until end comment ###
cat > /tmp/$HOSTS-pw.sh <<EOS
#!/bin/sh -xv
ssh -q -tt $HOSTS sudo "sudo ls" <<EOC
$SUDOPW
EOC
EOS
### End Comment ###
 
    chmod 700 /tmp/$HOSTS-pw.sh
    /tmp/$HOSTS-pw.sh >/dev/null
    if [ -f /tmp/$HOSTS-pw.sh ]; then rm -f /tmp/$HOSTS-pw.sh; fi
 
}
 
export HOSTS="destination.host"
 
echo "Enter SUDO password:"
read -s SUDOPW
 
f1
 
unset SUDOPW
exit 0
[user@my-host ~]
[user@my-host ~]
[user@my-host ~]
[user@my-host ~]$ ./temp.sh 
#!/bin/bash -xv
 
function f1 {
 
### Do NOT indent these lines until end comment ###
cat > /tmp/$HOSTS-pw.sh <<EOS
#!/bin/sh -xv
ssh -q -tt $HOSTS sudo "sudo ls" <<EOC
$SUDOPW
EOC
EOS
### End Comment ###
 
    chmod 700 /tmp/$HOSTS-pw.sh
    /tmp/$HOSTS-pw.sh >/dev/null
    if [ -f /tmp/$HOSTS-pw.sh ]; then rm -f /tmp/$HOSTS-pw.sh; fi
 
}
 
export HOSTS="destination.host"
+ export HOSTS=destination.host
+ HOSTS=destination.host
 
echo "Enter SUDO password:"
+ echo 'Enter SUDO password:'
Enter SUDO password:
read -s SUDOPW
+ read -s SUDOPW
 
f1
+ f1
+ cat
+ chmod 700 /tmp/destination.host-pw.sh
+ /tmp/destination.host-pw.sh
#!/bin/sh -xv
ssh -q -tt destination.host sudo "sudo ls" <<EOC
My-password
EOC
+ ssh -q -tt destination.host sudo 'sudo ls'
tcgetattr: Inappropriate ioctl for device
+ '[' -f /tmp/destination.host-pw.sh ']'
+ rm -f /tmp/destination.host-pw.sh
 
unset SUDOPW
+ unset SUDOPW
exit 0
+ exit 0

【问题讨论】:

标签: bash shell


【解决方案1】:

这是一个基本的 shell,用于在 ssh 上运行各种 sudo 命令,而无需以明文或 shell 历史记录发送密码。临时文件在您自己的主机上创建和删除。

此外,根据命令和情况,您还可以使用 nopasswd 在 sudoers 文件中添加您的帐户以获取特定命令。我希望这有帮助。如果您提供更多信息,我可能会提供更多帮助。

#!/bin/bash

function() {

    if "something" ;
        then

### Do NOT indent these lines until end comment ###
cat > /tmp/$HOSTS-pw.sh <<EOS
#!/bin/sh
ssh -q -tt user@$HOSTS sudo "your command here" <<EOC
$SUDOPW
EOC
EOS
### End Comment ###

    chmod 700 /tmp/$HOSTS-pw.sh
    /tmp/$HOSTS-pw.sh >/dev/null
    if [ -f /tmp/$HOSTS-pw.sh ]; then rm -f /tmp/$HOSTS-pw.sh; fi

    else
        echo "some thing"

    fi
}

echo "Enter SUDO password:"
read -s SUDOPW

for loop here function; done

unset SUDOPW
exit 0

【讨论】:

  • 我收到此错误tcgetattr: Inappropriate ioctl for device
  • 抱歉延迟回复......我在问题中添加了所需的命令(在 EDIT 行之后)
  • 我立即看到了两件事。 1) 您需要在“ ssh -q -tt $HOSTS sudo "sudo ls" ssh -q -tt user@$HOSTS sudo ls <<EOC
  • @BrianMc,注意:如果你在&lt;&lt; 的末尾添加-,则“you can”缩进heredoc 的正文,例如&lt;&lt;-。如果您需要为嵌套循环或 if 保留脚本缩进,这很有帮助。
  • 我把 sudo sudo ls 改成了 sudo ls。还使用了用户@$HOSTS。但我仍然得到同样的错误。是否有一个设置可以让我检查我的机器,看看我是否可以通过重定向输入密码,因为这也失败并要求我输入密码。 sudo ls &lt;&lt;EOC My-Password!!! &gt; EOC Password:
猜你喜欢
  • 2017-05-15
  • 2015-07-24
  • 1970-01-01
  • 2017-12-04
  • 2010-10-08
  • 2011-01-25
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多