Sponsored Content
Top Forums Shell Programming and Scripting Using expect script in a shell script Post 302076238 by System Shock on Saturday 10th of June 2006 08:54:03 AM
Old 06-10-2006
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with Expect and Shell script

This is my shell script which calls an expect file, i am trying to find out server.log file sizes on various servers. But what should be correct way to do that, is there any way i can run a for loop inside the expect file which can take cat <filename> as input. I know for can be used in expect file... (1 Reply)
Discussion started by: tonan
1 Replies

2. Shell Programming and Scripting

Using expect script in a shell script or vice versa

Hi All, Has anyone used expect script in a shell script or vice versa. I have a script which would ssh and scp to other machines. I need to use expect script so that user does not have to enter the password everytime ssh/scp command is executed. The script is as below. I need to pass the... (0 Replies)
Discussion started by: nua7
0 Replies

3. Shell Programming and Scripting

Using Expect results in a Shell script

I am trying to write a shell script that launches an expect script and then use results of the expect script in the rest of the shell script. The expect script is connecting to a remote host and looking up certian user info like UID and home directory. This part is working I then want the... (2 Replies)
Discussion started by: kaltekar
2 Replies

4. Shell Programming and Scripting

how to run shell script inside expect script?

I have the code like this : shell script continues ... .... expect -c" spawn telnet $ip expect "login:" send \"$usrname\r\" expect "Password:" send \"$passwd\r\" expect "*\>" send \"$cmdstr\r\" ... (1 Reply)
Discussion started by: robbiezr
1 Replies

5. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

6. Shell Programming and Scripting

Calling Shell Script from Expect...

Hi there, I need some help regarding the execution of shell script from expect as the method I am trying is giving me error. I wrote an shell program which takes two arguments to telnet to a device and saves the output in a file. Following is the script.... (0 Replies)
Discussion started by: cyberparanoid
0 Replies

7. Shell Programming and Scripting

Help with Expect in Shell Script

Hi All, I have a expect script which is working for single server, but if i am trying to pass value of the variable inside expect, it is not working. Please see below :- expect -c 'spawn ssh username@ip_address ; expect assword ; send "Password1\n" ; send "uptime \n" ; send "free -m \n" ;... (0 Replies)
Discussion started by: Renjesh
0 Replies

8. Shell Programming and Scripting

How to use expect in shell script?

Dear friends, Could you please help me in writing a sample code to connect the remote system (through ssh) using expect in shell script. Thanks in advance.. :-) (1 Reply)
Discussion started by: rajesh.tulluri
1 Replies

9. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

10. Shell Programming and Scripting

How to merge Expect script inside shell script?

Hi I have two scripts one is Expect and other is shell. I want to merge Expect code in to Shell script so that i can run it using only one script. Can somebody help me out ? Order to execute: Run Expect_install.sh first and then when installation completes run runTests.sh shell script. ... (1 Reply)
Discussion started by: ashish_neekhra
1 Replies
EXPECT_EXPECTL(3)							 1							 EXPECT_EXPECTL(3)

expect_expectl - Waits until the output from a process matches one of the patterns, a specified time period has passed, or anEOFis seen

SYNOPSIS
int expect_expectl (resource $expect, array $cases, [array &$match]) DESCRIPTION
Waits until the output from a process matches one of the patterns, a specified time period has passed, or an EOF is seen. If $match is provided, then it is filled with the result of search. The matched string can be found in $match[0]. The match substrings (according to the parentheses) in the original pattern can be found in $match[1], $match[2], and so on, up to $match[9] (the limitation of libexpect). PARAMETERS
o $expect - An Expect stream, previously opened with expect_popen(3). o $cases - An array of expect cases. Each expect case is an indexed array, as described in the following table: Expect Case Array +----------+--------------------------+---+---+---+ |Index Key | | | | | | | | | | | | | Value Type | | | | | | | | | | | | Description | | | | | | | | | | | | Is Mandatory | | | | | | | | | | | | Default Value | | | | | | | | | | +----------+--------------------------+---+---+---+ | 0 | | | | | | | | | | | | | string | | | | | | | | | | | | pattern, that will be | | | | | | matched against the out- | | | | | | put from the stream | | | | | | | | | | | | yes | | | | | | | | | | | | | | | | | | T{ 1 | | | | | | | | | | | | mixed | | | | | | | | | | | | value, that will be | | | | | | returned by this func- | | | | | | tion, if the pattern | | | | | | matches | | | | | | | | | | | | yes | | | | | | | | | | | | | | | | | | T{ 2 | | | | | | | | | | | | integer | | | | | | | | | | | | pattern type, one of: | | | | | | EXP_GLOB, EXP_EXACT or | | | | | | EXP_REGEXP | | | | | | | | | | | | no | | | | | | | | | | | | | | | | | | EXP_GLOB | | | | | | | | | | +----------+--------------------------+---+---+---+ RETURN VALUES
Returns value associated with the pattern that was matched. On failure this function returns: EXP_EOF, EXP_TIMEOUT or EXP_FULLBUFFER CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 0.2.1 | | | | | | | Prior to version 0.2.1, in $match parameter a | | | match string was returned, not an array of match | | | substrings. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 expect_expectl(3) example <?php // Copies file from remote host: ini_set("expect.timeout", 30); $stream = fopen("expect://scp user@remotehost:/var/log/messages /home/user/messages.txt", "r"); $cases = array( // array(pattern, value to return if pattern matched) array("password:", "asked for password"), array("yes/no)?", "asked for yes/no") ); while (true) { switch (expect_expectl($stream, $cases)) { case "asked for password": fwrite($stream, "my password "); break; case "asked for yes/no": fwrite($stream, "yes "); break; case EXP_TIMEOUT: case EXP_EOF: break 2; // break both the switch statement and the while loop default: die "Error has occurred!"; } } fclose($stream); ?> SEE ALSO
expect_popen(3). PHP Documentation Group EXPECT_EXPECTL(3)
All times are GMT -4. The time now is 09:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy