2016-02-26 222 views
0

我需要爲須藤通過ssh運行命令: -運行sudo命令通過ssh

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

我要帶密碼的腳本,並把它提供給須藤 - 有點像。

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

我不能寫這樣的腳本,需要幫助。

的bash -version GNU的bash,版本4.1.2(1)-release下(x86_64-紅帽Linux的GNU的) 版權所有(C)2009自由軟件基金會,公司 許可的GPLv3 +:GNU GPL版本3或後來http://gnu.org/licenses/gpl.html

================編輯===================

我試過的建議下面給出,但我得到一個錯誤。

[[email protected] ~]$ 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 
[[email protected] ~] 
[[email protected] ~] 
[[email protected] ~] 
[[email protected] ~]$ ./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 
+1

的[適當的方式來須藤通過ssh]可能的複製(http://stackoverflow.com/questions/10310299/proper-way-to-sudo-over-ssh) – idjaw

回答

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 [email protected]$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 
+0

我收到此錯誤'tcgetattr :不適當的ioctl for device' – user1918858

+0

請發佈您正在嘗試運行的ssh命令。 –

+0

抱歉,延遲迴復....我已經在問題中添加了所需的命令(編輯後) – user1918858