su myacco-6ren">
gpt4 book ai didi

linux - 如何使用脚本中的密码从 shell 脚本登录用户帐户?

转载 作者:太空狗 更新时间:2023-10-29 11:09:16 25 4
gpt4 key购买 nike

我正在编写一个 shell 脚本,使用 shell 脚本本身中提到的密码登录到任何用户帐户。我的shell脚本如下:

mypass="helloworld"
echo mypass>su myaccount

执行这个 shell 脚本后,我仍然在我当前的用户帐户中,而不是在 myaccount

TL;DR 我想知道如何通过运行 shell 脚本来登录用户帐户。

最佳答案

使用 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*"

关于linux - 如何使用脚本中的密码从 shell 脚本登录用户帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21776740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com