Using expect script in a shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Using expect script in a shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-10-2006
Naresh Kumar Naresh Kumar is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 28 August 2006, 11:58 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Question Using expect script in a shell script

How can i call expect script from a shell script with an arguments and how do i access those arguments in the expect script.

My requirements is as follows. I have a list of ip addresses in my shell variable. I want to telnet to those ip addresses and execute some commands and come out.

How can i write a generic expect script and call this expect scipt with arguments and how can i access this argument in expect script.
Sponsored Links
    #2  
Old 06-10-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
 
Join Date: May 2006
Last Activity: 4 May 2012, 3:37 PM EDT
Location: Tau Ceti V
Posts: 558
Thanks: 0
Thanked 7 Times in 6 Posts
...just like calling any other command/file/script; something like:

for x in $(cat IP.addresses); do
expect_script $x
done

then in expect_script you'd have something like:

spawn telnet [lindex $argv 0] ## where [lindex $argv 0] = $x
do expect commands
exit 0
Sponsored Links
    #3  
Old 06-10-2006
Naresh Kumar Naresh Kumar is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 28 August 2006, 11:58 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you.
please suggest some good web pages for learning expect as i did not get good
information from google.
    #4  
Old 06-10-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
 
Join Date: May 2006
Last Activity: 4 May 2012, 3:37 PM EDT
Location: Tau Ceti V
Posts: 558
Thanks: 0
Thanked 7 Times in 6 Posts
I wish I could give you some links. I bought the O'Reilly book many years ago and that's how I learned it, and still use the book as reference when I need it.

If you are just going to run some regular line commands, here's an example to get you started:

Say you have a file with a list of IP address, and want to telnet to each IP address and add a user named user1 and set the user's passwd to "newpassword".

You could pass the IP addresses to the script with a loop like the one I used as an example, or use an array, etc.. but say you use a loop like the example above as root, then your expect script would look like:


Code:
#!/usr/local/bin/expect --  # or wherever your expect executable is
spawn telnet [lindex $argv 0] # again, equal to $x in the example above
expect ogin:   # tell expect to, well, expect the login prompt
send username\r     # need to use \r as carriage return.
expect ssword:
send your_passwd\r
expect "your_prompt"   # put it in quotation marks in case it has  $ or > etc...
send "useradd -u10001 -g10 -s/usr/bin/ksh -d/export/home/user -m user1\r" # need quotes for commands with more than 1 word in them.
expect "your_prompt"
send "passwd user1\r"   
expect ssword:
send newpasswd\r
expect ssword:
send newpasswd\r
expect "your_prompt"
send exit\
expect closed
exit 0

Personally, all of my boxes have ssh and keys, so I spawn ssh and have no need to include passwds.
Oh, and remember to use the last of the string that you tell expect to expect: if the line is "Enter new passwd:" , while it will run if you tell expect to expect "Enter", it'll run faster if you use the last of the string, as in "ssword:".

As with ksh, csh, perl, etc, there are many ways to accomplish the same task. The above is just a basic example that you can use as a guide if you only need to run simple commands. It gets a little more complicated when you expect either one string or another , when you use editing commands like sed or awk, when you pass variables to the commands you are executing within expect (as in send "echo $PATH", for example), and so on.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with Expect in Shell Script Renjesh Shell Programming and Scripting 0 12-13-2011 02:59 AM
Expect script to run a Shell script on remote server yashwanthsn Programming 2 03-23-2011 04:14 PM
how to run shell script inside expect script? robbiezr Shell Programming and Scripting 1 05-06-2009 12:58 PM
Using expect script in a shell script or vice versa nua7 Shell Programming and Scripting 0 07-18-2008 07:16 AM
Need help with Expect and Shell script tonan Shell Programming and Scripting 1 04-10-2008 10:45 PM



All times are GMT -4. The time now is 12:19 AM.