我有以下脚本SSH到具有wheel用户的服务器。它是我想在许多服务器和应用程序中使用的测试脚本。但是,不是在脚本中将密码作为纯文本提供,而是希望将其隐藏或加密,然后在运行脚本时进行解密。
#!/usr/bin/expect -f
set timeout 2
set ip 78.47.1xx.xx
set port 22
set rootpwd adgajfgjagf
set wuser winner
set wpwd winner#@!
spawn ssh -l $wuser $ip -p $port
expect -re "connecting (yes/no)?" {
send -- "yes\r"
}
expect -nocase -re "password:" {
send "$wpwd\r"
}
expect "# " {
send -- "su - root\r"
expect "Password: "
send -- "$rootpwd\r"
interact
} ## Heading ##
为什么不使用ssh密钥登录?
对于您希望能够连接的每台计算机,您必须要求他们的公共ssh-key并将它们添加到autorized_keys。然后当通过ssh连接时,这些机器不需要输入密码。
来源:http://sshkeychain.sourceforge.net/mirrors/SSH-with-Keys-HOWTO/SSH-with-Keys-HOWTO-4.html
在过去,我不得不使用混淆(yencode)和文件权限。