bash 如何使用脚本中的密码从shell脚本登录到用户帐户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21776740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to login to user account from shell script with password in the script?
提问by Punkster
I am writing a shell script to login to any user account with the password mentioned in the shell script itself. My shell Script is as follows:
我正在编写一个 shell 脚本,以使用 shell 脚本本身中提到的密码登录到任何用户帐户。我的shell脚本如下:
mypass="helloworld"
echo mypass>su myaccount
After executing this shell script i am still in my current user account rather than in myaccount
执行这个 shell 脚本后,我仍然在我当前的用户帐户中,而不是在 myaccount
TL;DR I want to know how to login to a user account via a shell script by just running it.
TL;DR 我想知道如何通过运行 shell 脚本来登录到用户帐户。
回答by Fidel
Sending password through script is possible by using expect script, Below is the very basic expect script for login to su account and executing pwd command.
使用expect脚本可以通过脚本发送密码,下面是登录su帐户和执行pwd命令的非常基本的expect脚本。
#!/usr/bin/expect
spawn su myaccount # Spawning the su process
expect "Password:*" # Wait/expect for the Password string from the spawned process
send "uraccpassword\r" # send the password to spawned process as an input.
expect "*bash*"
send "pwd\r"
expect "*bash*"
回答by Amar
If you want to login to an account without providing a password then you'll need to generate ssh keys from your account using ssh-keygen
and copy the public key into the account you want to login to. Additionally, I would always set a passphrase on the generated keys and use ssh-agent
to manage it.
如果您想在不提供密码的情况下登录帐户,则需要使用您的帐户生成 ssh 密钥ssh-keygen
并将公钥复制到您要登录的帐户中。此外,我总是会在生成的密钥上设置密码并用于ssh-agent
管理它。