How to pass the password to su/sudo/ssh without ov

2018-12-31 19:07发布

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.

could be an option, but it's not present on my stripped-down system.

20条回答
浅入江南
2楼-- · 2018-12-31 19:25

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.

查看更多
皆成旧梦
3楼-- · 2018-12-31 19:26

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

查看更多
谁念西风独自凉
4楼-- · 2018-12-31 19:26
su -c "Command" < "Password"

Hope it is helpful.

查看更多
只若初见
5楼-- · 2018-12-31 19:27

I've got:

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

Works for me.

查看更多
梦醉为红颜
6楼-- · 2018-12-31 19:27

For sudo you can do this too:

sudo -S <<< "password" command
查看更多
残风、尘缘若梦
7楼-- · 2018-12-31 19:27

Hardcoding a password in an expect script is the same as having a passwordless sudo, actually worse, since sudo at least logs its commands.

查看更多
登录 后发表回答