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:33

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
查看更多
骚的不知所云
3楼-- · 2018-12-31 19:36

USE:

echo password | sudo command

Example:

echo password | sudo apt-get update; whoami

Hope It Helps..

查看更多
不流泪的眼
4楼-- · 2018-12-31 19:36
ssh -t -t me@myserver.io << EOF
echo SOMEPASSWORD | sudo -S do something
sudo do something else
exit
EOF
查看更多
君临天下
5楼-- · 2018-12-31 19:37

Take a look at expect linux utility.

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

查看更多
旧时光的记忆
6楼-- · 2018-12-31 19:38

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.

查看更多
春风洒进眼中
7楼-- · 2018-12-31 19:39

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/

查看更多
登录 后发表回答