user1918858
user1918858

Reputation: 1208

Run sudo command over ssh

I need to run a command as sudo over ssh:-

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

I want to take password in the script and provide it to sudo - something like.

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

I am not able to write such a script and need help.

bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

================EDIT===================

I tried the suggestion given below but am getting an error.

[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

Upvotes: 0

Views: 4160

Answers (1)

Brian Mc
Brian Mc

Reputation: 151

Here is a basic shell for running all sorts of sudo commands over ssh without having the password sent over in clear text or in the shell history. The temporary file is created and deleted on your own host.

Also depending on the command and situation, you could also add your account in the sudoers file with nopasswd for a specific command. I hope this helps. I you provide more info I may be able to help more.

#!/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

Upvotes: 1

Related Questions