2015-09-05 166 views
1

更新:在這一點上我非常沮喪。我試着將期望代碼移動到它自己的文件並從bash腳本中調用它。CLI的期望腳本

... 
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個小時我一直在努力解決這個問題。任何人都可以請借我一隻手嗎?

+0

我真的很沮喪在這一點上。我已經嘗試了一些新的代碼,我創建了一個新的expect文件。 – LockX

回答

0

我在代碼中看到一些問題。起初,你已經嘗試在代碼中,這應該拋出介紹一下read命令錯誤添加#!/usr/bin/expect

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置於兩個單引號應該封閉withing expect -cexpect -f

簡單地說,它不會允許bash的換人。所以,我要用雙引號。 (但是,這我們有逃脫Expect的反斜槓雙引號)。

admin="dinesh" 
admin_pwd="root" 
user="satheesh" 
user_pwd="[email protected]" 
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腳本。