2

I am writing a script that will allow a Linux admin to quickly change passwords of its users.

#!/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

However, the expect portion, which would automate the inputting of passwords, is not highlighted in my editor and does not work. I keep getting prompts to manually enter text. This is especially surprising since the script is sourcing expect, not bash. I've been trying to fix this for the past 2 hours. Can anyone please lend me a hand?

  • 2
    I don't understand what you're trying to do. It's declared like an expect script, but the body clearly isn't one. I suspect you've gone down the wrong path several steps ago, but I can't really tell from what you've presented where that was. – Michael Homer Sep 6 '15 at 1:40
3

You are mixing expect and bash code. Your script is principally a bash script, so replace the top line by #!/bin/bash. Then replace the lines:

sudo passwd ${!tmp}
expect -exact "[sudo] password for $current_user: "
send "$pass\r"
expect -exact "New password: "
send "$password\r"

by a call to a function mychangepw with the args that expect needs, i.e. the current user, sudo password, user to change, and their password:

mychangepw $current_user "$pass" ${!tmp} "$password"

Add the function to the start of the script and use it to pass the args to expect, with the expect script on stdin upto the line "!":

mychangepw(){
    expect -d - "$@" <<\!
    set current_user [lindex $argv 0]
    set sudopass [lindex $argv 1]
    set user [lindex $argv 2]
    set password [lindex $argv 3]
    spawn sudo passwd $user
    expect -exact "\[sudo\] password for $current_user: "
    send "$sudopass\r"
    expect -exact "New password: "
    send "$password\r"
!
}

Be careful with the line starting "!". It must be the first and only char of the line, with no indent nor additional whitespace nor comments etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.