【发布时间】: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
【问题讨论】: