企业在分发MacBook时,IT管理员会将MacOS的登录密码设置为空,为了方便用户使用,但是因为MacOS当前的用户密码是空(blank),在使用脚本做维护时,因为sudo在用户空密码为空的时候,是无法使用sudo来提升权限的,导致脚本运行失败,如果人为去终端上,一个一个修改,必定会花费大量人工时间,为解决这样的一个问题,我们使用了shell expect脚本来解决,目的是给当前用户设置一个密码。
设置密码脚本如下:
#!/bin/sh
CurrentUsername=`/usr/bin/id -un`
echo $CurrentUsername
expect <<EOF
spawn passwd $CurrentUsername
expect {
"Old Password:" {send "\r";exp_continue}
"New Password:" {send "qwer1234\r";exp_continue}
"Retype New Password:" {send "qwer1234\r"}
}
EOF
清除密码脚本如下,即将当前用户密码设置为空:
#!/bin/sh
CurrentUserName=`/usr/bin/id -un`
echo $CurrentUserName
expect <<EOF
spawn passwd $CurrentUserName
expect {
"Old Password:" {send "qwer1234\r";exp_continue}
"New Password:" {send "\r";exp_continue}
"Retype New Password:" {send "\r"}
}
EOF
当IT管理员在分发MacBook时,为方便用户,通常会将MacOS登录密码设为空。然而,这导致了sudo命令无法在脚本中使用。为解决此问题,文章介绍了如何利用Shell Expect脚本来自动化设置和清除当前用户的密码。设置密码脚本通过交互方式输入新密码,而清除密码脚本则将密码重置为空。这种方法有效地避免了手动修改大量设备的密码所耗费的时间。
3419

被折叠的 条评论
为什么被折叠?



