How to pass the password to su/sudo/ssh without overriding the TTY?

LinuxSshPasswordsSudoSu

Linux Problem Overview


I'm writing a C Shell program that will be doing su or sudo or ssh. They all want their passwords in console input (the TTY) rather than stdin or the command line.

Does anybody know a solution?

Setting up password-less sudo is not an option.

[tag:expect] could be an option, but it's not present on my stripped-down system.

Linux Solutions


Solution 1 - Linux

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

    -S          The -S (stdin) option causes sudo to read the password from
                the standard input instead of the terminal device.

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp

As for ssh, I have made many attempts to automate/script it's usage with no success. There doesn't seem to be any build-in way to pass the password into the command without prompting. As others have mentioned, the "expect" utility seems like it is aimed at addressing this dilemma but ultimately, setting up the correct private-key authorization is the correct way to go when attempting to automate this.

Solution 2 - Linux

I wrote some Applescript which prompts for a password via a dialog box and then builds a custom bash command, like this:

echo <password> | sudo -S <command>

I'm not sure if this helps.

It'd be nice if sudo accepted a pre-encrypted password, so I could encrypt it within my script and not worry about echoing clear text passwords around. However this works for me and my situation.

Solution 3 - Linux

For ssh you can use sshpass: sshpass -p yourpassphrase ssh user@host.

You just need to download sshpass first :)

$ apt-get install sshpass
$ sshpass -p 'password' ssh username@server

Solution 4 - Linux

For sudo you can do this too:

sudo -S <<< "password" command

Solution 5 - Linux

I've got:

ssh user@host bash -c "echo mypass | sudo -S mycommand"

Works for me.

Solution 6 - Linux

The usual solution to this problem is setuiding a helper app that performs the task requiring superuser access: http://en.wikipedia.org/wiki/Setuid

Sudo is not meant to be used offline.

Later edit: SSH can be used with private-public key authentication. If the private key does not have a passphrase, ssh can be used without prompting for a password.

Solution 7 - Linux

Maybe you can use an expect command?:

expect -c 'spawn ssh root@your-domain.com;expect password;send "your-password\n";interact

That command gives the password automatically.

Solution 8 - Linux

This can be done by setting up public/private keys on the target hosts you will be connecting to. The first step would be to generate an ssh key for the user running the script on the local host, by executing:

ssh-keygen
Enter file in which to save the key (/home/myuser/.ssh/id_rsa): <Hit enter for default>
Overwrite (y/n)? y

Then enter a blank password. After that, copy your ssh key onto the target host which you will be connecting to.

ssh-copy-id <remote_user>@<other_host>
remote_user@other_host's password: <Enter remote user's password here>

After registering the ssh keys, you would be able to perform a silent ssh remote_user@other_host from you local host.

Solution 9 - Linux

Take a look at expect linux utility.

It allows you to send output to stdio based on simple pattern matching on stdin.

Solution 10 - Linux

When there's no better choice (as suggested by others), then man socat can help:

   (sleep 5; echo PASSWORD; sleep 5; echo ls; sleep 1) |
   socat - EXEC:'ssh -l user server',pty,setsid,ctty

          EXEC’utes an ssh session to server. Uses a pty for communication
          between socat and ssh, makes it ssh’s  controlling  tty  (ctty),
          and makes this pty the owner of a new process group (setsid), so
          ssh accepts the password from socat.

All of the pty,setsid,ctty complexity is necessary and, while you might not need to sleep as long, you will need to sleep. The echo=0 option is worth a look too, as is passing the remote command on ssh's command line.

Solution 11 - Linux

ssh -t -t me@myserver.io << EOF
echo SOMEPASSWORD | sudo -S do something
sudo do something else
exit
EOF

Solution 12 - Linux

Set SSH up for Public Key Authentication, with no pasphrase on the Key. Loads of guides on the net. You won't need a password to login then. You can then limit connections for a key based on client hostname. Provides reasonable security and is great for automated logins.

Solution 13 - Linux

a better sshpass alternative is: passh https://github.com/clarkwang/passh

Login to a remote server

 $ passh -p password ssh user@host

Run a command on remote server

 $ passh -p password ssh user@host date

other methods to pass the password > -p The password (Default: `password') > > -p env: Read password from env var > > -p file: Read password from file

here I explained why it is better than sshpass, and other solutions.

Solution 14 - Linux

You can also pass various parameters as follows:

echo password | echo y | sudo -S pacman -Syu

(Although that's a bad idea, it's just an example)

Solution 15 - Linux

echo <password> | su -c <command> <user> 

This is working.

Solution 16 - Linux

I had the same problem. dialog script to create directory on remote pc. dialog with ssh is easy. I use sshpass (previously installed).

   dialog --inputbox "Enter IP" 8 78 2> /tmp/ip

   IP=$(cat /tmp/ip)


   dialog --inputbox "Please enter username" 8 78 2> /tmp/user

   US=$(cat /tmp/user)


   dialog --passwordbox "enter password for \"$US\" 8 78 2> /tmp/pass

   PASSWORD = $(cat /tmp/pass)


   sshpass -p "$PASSWORD" ssh $US@$IP mkdir -p /home/$US/TARGET-FOLDER
  

   rm /tmp/ip

   rm /tmp/user

   rm /tmp/pass

greetings from germany

titus

Solution 17 - Linux

Building on @Jahid's answer, this worked for me on macOS 10.13:

ssh <remote_username>@<remote_server> sudo -S <<< <remote_password> cat /etc/sudoers

Solution 18 - Linux

I once had a use case where I needed to run Sudo and ssh in the same command without stdin specifying all the variables needed.

This is the command I used

echo sudopassword | sudo -S -u username sshpass -p  extsshpassword ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no  username@ipaddress " CMD on external machine"

Breaking that command into pieces!

This will allow you to run commands through your machine using Superuser:

echo password | sudo -S -u username

This will allow you to pass ssh password and execute commands on external machines:

sshpass -p  sshpassword  ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no  username@ipaddress " CMD on external machine"

>>make sure you install the sudo and openssh packages on your machine.

Solution 19 - Linux

One way would be to use read -s option .. this way the password characters are not echoed back to the screen. I wrote a small script for some use cases and you can see it in my blog: http://www.datauniv.com/blogs/2013/02/21/a-quick-little-expect-script/

Solution 20 - Linux

USE:

echo password | sudo command

Example:

echo password | sudo apt-get update; whoami

Hope It Helps..

Solution 21 - Linux

You can provide password as parameter to expect script.

Solution 22 - Linux

su -c "Command" < "Password"

Hope it is helpful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn-alexanderView Question on Stackoverflow
Solution 1 - LinuxJesse WebbView Answer on Stackoverflow
Solution 2 - LinuxmlambieView Answer on Stackoverflow
Solution 3 - LinuxEdwardView Answer on Stackoverflow
Solution 4 - LinuxJahidView Answer on Stackoverflow
Solution 5 - LinuxDavid DawsonView Answer on Stackoverflow
Solution 6 - LinuxdiciuView Answer on Stackoverflow
Solution 7 - LinuxadhownView Answer on Stackoverflow
Solution 8 - LinuxByob View Answer on Stackoverflow
Solution 9 - LinuxMarkoView Answer on Stackoverflow
Solution 10 - LinuxMartin DoreyView Answer on Stackoverflow
Solution 11 - LinuxEd BishopView Answer on Stackoverflow
Solution 12 - LinuxChris EllisView Answer on Stackoverflow
Solution 13 - LinuxBadr ElmersView Answer on Stackoverflow
Solution 14 - LinuxExpectoPatromView Answer on Stackoverflow
Solution 15 - LinuxDevOzView Answer on Stackoverflow
Solution 16 - LinuxtitusView Answer on Stackoverflow
Solution 17 - LinuxJS.View Answer on Stackoverflow
Solution 18 - LinuxAffes SalemView Answer on Stackoverflow
Solution 19 - LinuxAviView Answer on Stackoverflow
Solution 20 - LinuxSarat ChandraView Answer on Stackoverflow
Solution 21 - LinuxMarkoView Answer on Stackoverflow
Solution 22 - LinuxShankarView Answer on Stackoverflow