How to use 'expect' to pass UID & Password to a "for loop" in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use 'expect' to pass UID & Password to a "for loop" in shell script?
# 1  
Old 11-11-2013
How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends,

Need someone's help in helping me with the below requirement for a script:

> For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details <

I know, this is possible through expect programming in a simple shell script, but unfortunately I'm not able to get one to work as required. Below is what I could manage to write without expected results:

Code:
cat server_list  
server1 
server2 
server3 
.. 
.. 
..

Code:
cat for_loop.sh  
for i in `cat server_list` 
do 
multibos -S 
done

Code:
cat expect_script.sh  

spawn /$HOME/for_loop.sh  
expect "*User_ID*" 
send "souvikm\r" 
expect "*Password*" 
send "passw0rd\r" 
expect eof

Code:
$> ./expect_script.sh

Running the above written expect script doesn't solve my purpose - it does nothing Smilie Could someone help me rectify this script or help me with a better idea?


-- Souvik

Last edited by thisissouvik; 11-11-2013 at 09:56 AM.. Reason: Reorganizing!
# 2  
Old 11-11-2013
Here's a very basic way using Expect that should hopefully set you on the path to what you want to accomplish:

Code:
#!/usr/bin/expect -f
#
#

# set credentials - NOT RECOMMENDED METHOD
set u "username"
set p "password"

# get host name from command-line
set h [lindex $argv 0]

# set the command to run
set cmd "date"

# spawn a connection to the host and
# run the given command
spawn ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

I've found reading files using Expect to be a bit flaky, so you could use a while loop to parse your file with the hostnames and feed it to the Expect script like so:

while read hosts;do sendCmd $hosts;done < server_list

Output:

spawn ssh -t username@server1 date
username's Password:
Mon Nov 11 08:52:42 EST 2013
Connection to server1 closed.

75d6b64809eec8e415f7bca6b09b6f40

Last edited by in2nix4life; 11-12-2013 at 01:50 PM..
# 3  
Old 11-12-2013
Hello in2nix,

Your basic expect based script did the ground work for me - could login to any server passing the hostname to parameter "h", so this is how my little modified script looks now:

Code:
#!/usr/bin/expect -f
#
#

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
# set h "my_desired_server"

# set the command to run
set cmd "echo Hello Souvik!  >> /export/home/msouvik/echo.out"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

I could not manage to pass multiple server names to this expect script, the methods I tried --

1. Called this expect script from an external "for" & "while" loop -- got an obvious error of parameter "h" not found:
Code:
for h in `cat my_server_list`
do
<Path of above written expect script>
done

2. Added a "for" loop within the above expect script:
Code:
#!/usr/bin/expect -f
#
#
for h in `cat my_server_list`
do

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
# set h ""

# set the command to run
set cmd "echo Hello Souvik!  >> /export/home/msouvik/echo.out"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

done

That failed since it couldn't understand expect syntaxe(s) within the for loop.

Please further suggest!


-- Souvik
# 4  
Old 11-12-2013
Did you try the while loop that I listed in my last post? I mentioned that reading files inside of Expect is a bit flaky. If you create a while loop to read the file containing your hostnames and then send them as an argument to the Expect script, it will store it in the h variable.

That's what this line does:

Code:
set h [lindex $argv 0]

So to put it all together, let's say the Expect script is named sendCmd:

Code:
while read hosts;do sendCmd $hosts;done < my_server_list

or 

while read hosts
do
    sendCmd $hosts
done < my_server_list

Hope that helps.
# 5  
Old 11-12-2013
I haven't found file I/O to be flaky in expect, but I must admit I only really use expect to test solutions posted in the forum. I find it a bit of a hacking tool and would prefer to use passwordless ssh for my own work.

The below script should open the server_list file, connect to each server run the configured command ("multibos -S" in this case) and log the output to a file name config_details with a "hostname:" prefix.

Also note: I'm using telnet in this script and you may want ssh or some other command.

Uncomment the log_user 0 line to suppress output once you have finished debugging.

Code:
#!/usr/bin/expect -f
set timeout 2
set u "souvikm"
set p "passw0rd"
set command "multibos -S"

set hosts [open server_list r]
set config [open config_details w]

#log_user 0

while {[gets $hosts host] >= 0} {
    spawn -noecho telnet $host
    expect "*User_ID*"
    send -- "$u\r"
    expect "?assword*"
    send -- "$p\r"
    send "PS1='PROMPT# '\r"
    expect -re "PROMPT# '(.*)PROMPT# "
    send "$command\r"
    expect -re "\r\n(.*)\r\n(.*)PROMPT# "
    set output $expect_out(1,string)
    puts $config "$host: $output\r"
    send "exit\r"
    expect eof
}
close $hosts
close $config


Last edited by Chubler_XL; 02-25-2017 at 02:26 PM..
# 6  
Old 11-13-2013
Thanks in2nix, ~XL for your valuable inputs Smilie

Tried tweaking in2nix's code to allign to my reqd with the following 'expect' code:

Code:
#!/usr/bin/expect -f
#
#

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
set h [lindex $argv 0]

# set the command to run
set cmd "echo Hello Souvik!  >> /export/home/msouvik/echo.out.$h"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# scp the data onto NIM server
spawn -noecho scp $u@$h:/export/home/msouvik/echo.out .

# done
expect eof

And then applied the below 'while' loop:

Code:
while read h
> do
> expect_script.sh $h
> done < lsnim

Below is the error I get while the script tries to create a /export/home/msouvik/echo.out.$h file on the target servers:

==[ Error ]==
msouvik@my_target_server1 password: msouvik@my_target_server1 password:
scp: /export/home/msouvik/echo.out.my_target_server1: No such file or directory

msouvik@my_target_server2 password: msouvik@my_target_server2 password:
scp: /export/home/msouvik/echo.out.my_target_server2: No such file or directory
==

Find it really weird 'coz the script is not able to create a file named /export/home/msouvik/echo.out.$h on the target servers, however it is able to do so without the $h suffix to the filename.

Any leads pls?


-- Souvik
P.S: I'll try to understand XL's script before I implement it tomorrow Smilie
# 7  
Old 11-14-2013
Friend's,

I expected a response from one of you today, but none Smilie

I could manage to get my requirement by adding echo.out.$h in the scp portion of the script & not in the set cmd section as below:

Code:
cat expect_script.sh
#!/usr/bin/expect -f
#
#

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
set h [lindex $argv 0]

# set the command to run
set cmd "echo Hello Buddy! >> /export/home/msouvik/echo.out"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt for ssh
expect "Password:*"
send -- "$p\r"
send -- "\r"

# scp the data onto NIM server
spawn -noecho scp $u@$h:/export/home/msouvik/echo.out /export/home/msouvik/echo.out.$h

# handle the password prompt for scp
expect "Password:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

However, this modification is creating a new problem - now the portion of code(highlighted in red) isn't working, it simply doesn't write to the file on the destination server. I've pasted the above code as it is from the server.

I'm not able to catch what happened to the working code with this small modification, could one you guys help me here? Excuse me (if) I'm forcing you guys to help Smilie


-- Souvik
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

2. Shell Programming and Scripting

Can someone please show me a very simple "expect" script to change password in Solaris please?

Ladies & Gents, Can one of you gurus please show me a very simple "expect" script to change the password in Solaris in a script, please? Nothing fancy, no error checking, no nothing. Just to change the password of a new user, it's all. Many thanks in advance. U guys have honestly earned my... (1 Reply)
Discussion started by: Hiroshi
1 Replies

3. Shell Programming and Scripting

How to pass password to "su account" in script?

Hi, I need to run a test script to check all test accounts, is it possible to pass the password to su in following command? I've got following error: $ echo "${password}" | su ${test_account} -c "check_account.sh" standard in must be a tty Thank you. - j (3 Replies)
Discussion started by: hce
3 Replies

4. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

5. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

6. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

7. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

8. Shell Programming and Scripting

ssh through "expect" in shell script

Hi, I am trying to use "Expect" in shell script to ssh and do some work in remote server but I am unable to connect. Here is the code I am using. #save as test.sh set ip "10.10.10.10" set username "uname" set password "upass" spawn ssh $username@$ip expect "Password:" send... (8 Replies)
Discussion started by: shekhar2010us
8 Replies

9. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

10. Shell Programming and Scripting

shell script & sqlplus "plzz help"

hi friends, i m tryin to load data from one set of table to other i have sql procedure al ready for it..! i m going to load the procedure only if data in one of my table for example table "landing " have 10 records each attribute of this table is file_name status date ... (2 Replies)
Discussion started by: kulbir
2 Replies
Login or Register to Ask a Question