期待CLI的脚本

时间:2015-09-05 23:46:30

标签: bash highlight expect

更新:此时我真的很沮丧。我已经尝试将期望代码移动到自己的文件中并从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 $2}' <<<"$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

但是,期望部分会自动输入密码,但在我的编辑器中不会突出显示,也不起作用。我不断收到手动输入文字的提示。这是特别令人惊讶的,因为脚本是预期的,而不是bash。我过去2个小时一直试图解决这个问题。有人可以帮帮忙吗?

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到了一些问题。首先,您已尝试在代码中添加#!/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的返回值将在变量$?中可用。这有助于我们了解密码更新是否成功。变量OUTPUT将具有由生成的进程生成的输出。

使用#!/bin/bash,而不是#!/usr/bin/expect,因为它实际上是一个bash脚本。

相关问题