[英]how to login to user account from shell script with password in the script?
I am writing a shell script to login to any user account with the password mentioned in the shell script itself. 我正在编写一个shell脚本,使用shell脚本本身提到的密码登录任何用户帐户。 My shell Script is as follows: 我的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脚本登录用户帐户。
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命令的最基本的期望脚本。
#!/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*"
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. 如果您想在不提供密码的情况下登录帐户,则需要使用ssh-keygen
从您的帐户生成ssh密钥,并将公钥复制到您要登录的帐户中。 Additionally, I would always set a passphrase on the generated keys and use ssh-agent
to manage it. 另外,我总是在生成的密钥上设置密码,并使用ssh-agent
来管理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.