期待 CLI 的脚本
Expect Script for CLI
更新:此时我真的很沮丧。我试过将期望代码移动到它自己的文件并从 bash 脚本中调用它。
...
if [[ "$okay" == "OK" ]]
then
echo "PASSWORD ACCEPTED"
echo "Modifying User Passwords..."
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
tmp=user_$index
echo "Changing Password for " ${!tmp}
tmp2=$(${!tmp})
echo $tmp2
sh ./input.sh ${current_user} ${pass} ${password} ${tmp2}
let COUNTER=COUNTER+1
done
...
input.sh
expect -f
#------------------------------------------------------
set current_user [lindex $argv 0]
set pass [lindex $argv 1]
set password [lindex $argv 2]
set tmp2 [lindex $argv 3]
echo "EXPECT SCRIPT RUNNING"
sudo passwd ${!tmp2}
expect -exact "[sudo] password for $current_user: "
send "$pass\r"
expect -exact "New password: "
send "$password\r"
如果有人能帮助我,我将不胜感激。
我正在编写一个脚本,允许 Linux 管理员快速更改其用户的密码。
#!/usr/bin/expect
# Check password for strength
# ----------------------------------------------
read -p "What's your username?" current_user
read -p "What's the root password?" pass
read -p "How many users?" num
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
read -p "Enter username$index : " user_$index
let COUNTER=COUNTER+1
done
read -p "Enter password : " password
echo
echo "Tesing password strength..."
echo
result="$(cracklib-check <<<"$password")"
okay="$(awk -F': ' '{ print }' <<<"$result")"
if [[ "$okay" == "OK" ]]
then
echo "PASSWORD ACCEPTED"
echo "Modifying User Passwords..."
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
tmp=user_$index
echo "Changing Password for " ${!tmp}
echo ${!tmp}
sudo passwd ${!tmp}
expect -exact "[sudo] password for $current_user: "
send "$pass\r"
expect -exact "New password: "
send "$password\r"
let COUNTER=COUNTER+1
done
#echo "$user:$password" | usr/sbin/chpasswd
else
echo "Your password was rejected - $result"
echo "Try again."
fi
但是,将自动输入密码的期望部分在我的编辑器中没有突出显示并且不起作用。我不断收到手动输入文本的提示。这尤其令人惊讶,因为脚本是 expect,而不是 bash。在过去的 2 个小时里,我一直在尝试解决这个问题。谁能帮我一把?
我发现您的代码中存在一些问题。首先,您尝试在代码中添加 #!/usr/bin/expect
,这应该会引发有关 read
命令的错误,如
wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"
while executing
"read -p "What's your username?" current_user"
原因很简单,因为该脚本将被视为 Expect
脚本并且它不遵循 read
的语法。我想知道它对你有用。 :)
当它被称为 shell 脚本时,它应该包含在 expect -c
中而不是简单地包含在 expect -f
中
当 expect -c
用单引号引起来时,将不允许 bash 替换。所以,我将使用双引号。 (但是,我们必须用反斜杠转义 Expect
的双引号。)
admin="dinesh"
admin_pwd="root"
user="satheesh"
user_pwd="Hello@12E"
OUTPUT=$(expect -c "
# To suppress any other form of output generated by spawned process
log_user 0
spawn sudo passwd $user
expect {
timeout { send_user \"Timeout happened\n\";exit 0}
\"Sorry, try again\" {send_user \"Incorrect admin password\";exit 0}
\"password for $admin: $\" {send \"$admin_pwd\r\";exp_continue}
\"password: $\" {send \"$user_pwd\r\";exp_continue}
\"successfully\" {send_user \"Success\"; exit 1}
}
")
echo "Expect's return value : $?"
echo "-----Expect's response-----"
echo $OUTPUT
Expect
的 return 值将在变量 $?
中可用。这将帮助我们了解密码更新是否成功。变量 OUTPUT
将具有派生进程生成的输出。
使用 #!/bin/bash
,而不是 #!/usr/bin/expect
,因为它实际上是一个 bash 脚本。
更新:此时我真的很沮丧。我试过将期望代码移动到它自己的文件并从 bash 脚本中调用它。
...
if [[ "$okay" == "OK" ]]
then
echo "PASSWORD ACCEPTED"
echo "Modifying User Passwords..."
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
tmp=user_$index
echo "Changing Password for " ${!tmp}
tmp2=$(${!tmp})
echo $tmp2
sh ./input.sh ${current_user} ${pass} ${password} ${tmp2}
let COUNTER=COUNTER+1
done
...
input.sh
expect -f
#------------------------------------------------------
set current_user [lindex $argv 0]
set pass [lindex $argv 1]
set password [lindex $argv 2]
set tmp2 [lindex $argv 3]
echo "EXPECT SCRIPT RUNNING"
sudo passwd ${!tmp2}
expect -exact "[sudo] password for $current_user: "
send "$pass\r"
expect -exact "New password: "
send "$password\r"
如果有人能帮助我,我将不胜感激。
我正在编写一个脚本,允许 Linux 管理员快速更改其用户的密码。
#!/usr/bin/expect
# Check password for strength
# ----------------------------------------------
read -p "What's your username?" current_user
read -p "What's the root password?" pass
read -p "How many users?" num
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
read -p "Enter username$index : " user_$index
let COUNTER=COUNTER+1
done
read -p "Enter password : " password
echo
echo "Tesing password strength..."
echo
result="$(cracklib-check <<<"$password")"
okay="$(awk -F': ' '{ print }' <<<"$result")"
if [[ "$okay" == "OK" ]]
then
echo "PASSWORD ACCEPTED"
echo "Modifying User Passwords..."
COUNTER=0
while [ $COUNTER -lt $num ]; do
let index=COUNTER+1
tmp=user_$index
echo "Changing Password for " ${!tmp}
echo ${!tmp}
sudo passwd ${!tmp}
expect -exact "[sudo] password for $current_user: "
send "$pass\r"
expect -exact "New password: "
send "$password\r"
let COUNTER=COUNTER+1
done
#echo "$user:$password" | usr/sbin/chpasswd
else
echo "Your password was rejected - $result"
echo "Try again."
fi
但是,将自动输入密码的期望部分在我的编辑器中没有突出显示并且不起作用。我不断收到手动输入文本的提示。这尤其令人惊讶,因为脚本是 expect,而不是 bash。在过去的 2 个小时里,我一直在尝试解决这个问题。谁能帮我一把?
我发现您的代码中存在一些问题。首先,您尝试在代码中添加 #!/usr/bin/expect
,这应该会引发有关 read
命令的错误,如
wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"
while executing
"read -p "What's your username?" current_user"
原因很简单,因为该脚本将被视为 Expect
脚本并且它不遵循 read
的语法。我想知道它对你有用。 :)
当它被称为 shell 脚本时,它应该包含在 expect -c
中而不是简单地包含在 expect -f
当 expect -c
用单引号引起来时,将不允许 bash 替换。所以,我将使用双引号。 (但是,我们必须用反斜杠转义 Expect
的双引号。)
admin="dinesh"
admin_pwd="root"
user="satheesh"
user_pwd="Hello@12E"
OUTPUT=$(expect -c "
# To suppress any other form of output generated by spawned process
log_user 0
spawn sudo passwd $user
expect {
timeout { send_user \"Timeout happened\n\";exit 0}
\"Sorry, try again\" {send_user \"Incorrect admin password\";exit 0}
\"password for $admin: $\" {send \"$admin_pwd\r\";exp_continue}
\"password: $\" {send \"$user_pwd\r\";exp_continue}
\"successfully\" {send_user \"Success\"; exit 1}
}
")
echo "Expect's return value : $?"
echo "-----Expect's response-----"
echo $OUTPUT
Expect
的 return 值将在变量 $?
中可用。这将帮助我们了解密码更新是否成功。变量 OUTPUT
将具有派生进程生成的输出。
使用 #!/bin/bash
,而不是 #!/usr/bin/expect
,因为它实际上是一个 bash 脚本。