Using Expect for the first time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Expect for the first time
# 1  
Old 11-02-2018
Using Expect for the first time

Hi All,

Using Linux server 18.04

Trying to understand EXPECT, so I created a script to add in new users.

The code below works without expect, and I can manually change the passwords for the new users.
However, if I use EXPECT (which is installed) to set passwords it creates the users but no passwords.

Both scripts are executable.

Code:
#!/bin/bash
#
# TP = a file with 3 usernames testa, testb & testc
# EXPECT is the expect script
TP=/home/user/test.txt
EXPECT=/home/user/exp.sh
ID=9050

while read PERSON
do
    useradd -m -d /home/$PERSON -g 1005 -u $ID -c "Test User" -s /bin/bash $PERSON
    $EXPECT
    passwd -f $PERSON
    ID=$((ID + 1))
done < "$TP"

Expect Script
Code:
#!/usr/bin/expect -f
#
# this expect script is used to set new users passwords
# the password will be forced to expire immediately after 
#
set timeout -1
spawn $env(SHELL)
expect -exact "Enter new UNIX password: "
send -- "letmein\r"
expect -exact "Retype new UNIX password: "
send -- "letmein\r"
expect eof

Output
Code:
# ./test_users.sh
": no such file or directory
passwd: password expiry information changed.
": no such file or directory
passwd: password expiry information changed.
": no such file or directory
passwd: password expiry information changed.

I take it this is something to do with expect, maybe I have not understood it correctly, maybe I am picking the wrong spawn... I am not sure, as I have never used it before, but what I have read seems I have most of it correct.

Could someone point me in the right direction?
# 2  
Old 11-02-2018
Hi,

I do this using two quickly hacked together scripts as follows - please bear in mind that these scripts are running on Solaris 11 and things are done this way to avoid a problem. But the scripts should be easily adaptable.

Script 1

Code:
for USER in `cat $DATADIR/name_list.txt | awk -F: '{ print $1 }'`
        do
if [ ! -d /export/home/${USER} ]
        then
                echo "The home directory for ${USER} does not exist - create."
                mkdir -p /export/home/${USER}
                if [[ $? -eq "0" ]]
                then
                        useradd -c"XXX Support User" -d/export/home/${USER} ${USER}
                        if [[ $? -eq "0" ]]
                        then
                                echo "Account for user ${USER} Created."
                        else
                                echo "Accounr for user ${USER} could not be created."
                        fi
                else
                        echo "The Account for ${USER} either already exists or could not be setup."
                fi

                echo "Setting the ownership for /export/home/${USER}"
                chown ${USER}:staff /export/home/${USER}
                if [[ $? -eq "0" ]]
                then
                        echo "The ownership of /export/home/${USER} wasx successfully set."
                else
                        echo "There was a problem setting the ownership on /export/home/${USER}"
                fi
                echo "Setting the password for ${USER}"
                echo ${USER} >> $DATADIR/added_users.txt
                ${BINDIR}/set_pw.sh ${USER} Ch4ng3me
        else
                echo "The user ${USER} already exists."
fi
done

Script 2

Code:
#!/usr/bin/expect

set username [lindex $argv 0];
set newpass [lindex $argv 1];


# opem shell
spawn $env(SHELL)
# send passwd command
send "passwd $username\n"
expect "New Password:"
send "$newpass\n"
expect "Re-enter new Password:"
send "$newpass\n"
expect eof"
expect "# "
send "exit\n"

I would suggest that you modify the expect script to suit your environment and call it from your main script passing the variables as I do - it's easier that way I think.

Regards

Gull04
This User Gave Thanks to gull04 For This Post:
# 3  
Old 11-02-2018
Hi Gull04,

in your main script I see that this is calling the expect script, with username & password
Code:
${BINDIR}/set_pw.sh ${USER} Ch4ng3me

And looking at your expect code
it is taking the username and password from the args you passed from your script to the expect script.

so amending my expect script then running my users script... I still get
Code:
 ": no such file or directory

but looking at your expect script I see a random " at the eof, is this meant to be there without a closing "???
# 4  
Old 11-02-2018
Hi,

Having a quick look, if you use the -f switch. Then I think that /usr/bin/expect -f ${expect_commands_file} is what is expected.

If you create an expect script starting #!/usr/bin/expect containing the code - think that you'll get on better - also I'd be tempted to make an explicit call to it and to put some error checking in the control script.

Regards

Gull04
This User Gave Thanks to gull04 For This Post:
# 5  
Old 11-02-2018
Tried changing this, and i now get an error:
Code:
./test_users.sh: /home/user/exp.sh: /usr/bin/expect^M: bad interpreter: No such file or directory

All files are in /home/user/ directory.

I have removed the shebang line on exp.sh and recreated it, but to no avail.

I have also added in echo lines in the main script and these show fine before the expect and after the expect... so it must be this that is wrong
# 6  
Old 11-02-2018
Hi,

I'm afraid I have to go offline shortly for about six hours, but you can try the following and see how it goes.

If you modify my expect script, with your system returns then execute the script against a manually created user account - passing it the username and a password. You will be able to tell if the expect code is correct, you can also check the path to expect with which expect.

Where are you editing the files as ^M suggests notepad or similar.

Regards

Gull04
# 7  
Old 11-02-2018
Hi Gull04,

Being offline is fine... I will e going to site now anyway then not back until monday.

I copied the file from windows to Linux, but then I created a new one in vi on linux server.
I will create your expect file and test that on Monday.

thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

Expect script idles for a long time

the following code works sometimes. other times, it behaves mysteriously. when the script sshs to a box, it is suppose to automatically begin running the command it is told to run. but in this case, after this script logs into a host, it just sits there at the prompt and does not run the... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Expect script to suspend expecting for a time period.

I have a simple Expect script to power a system on and off in an endless loop looking for an ERROR message at which point the script should exit. But I need to skip the first 60 seconds after each power on or off and not exit if there are ERROR messages during that time. I thought I could use... (0 Replies)
Discussion started by: David_Gilhooly
0 Replies

4. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

5. Shell Programming and Scripting

Expect - Comparison of expect value and loop selection

Hello All, I am trying to automate an installation process using expect and sh script. My problem is that during the installation process the expected value can change according to the situation. For Example if this is a first time installation then at step 3 I'll get "Do you want to accept... (0 Replies)
Discussion started by: alokrm
0 Replies

6. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

7. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

8. Shell Programming and Scripting

Expect and auto expect command

I need to run a remote application(GUI) in a client.i.e on running a script in the client machine i should get the GUI application which is running in the server by providing password through the script.Will expect or autoexpect command suit for this scenario? could anyone help me by posting some... (0 Replies)
Discussion started by: arun_v
0 Replies

9. Shell Programming and Scripting

Long time since I used "expect"

Hello all, It's been a few years since I touched 'expect' and just got it on my system. In the past, I could simply do the following: (this is just a simple example) $ expect $ ls -l $ pwd $ exit ...then a default script name would be placed in my working directory. At this point I... (2 Replies)
Discussion started by: scriptosaurus
2 Replies
Login or Register to Ask a Question