Visit Our UNIX and Linux User Community


Expect control statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect control statement
# 1  
Old 11-30-2007
Expect control statement

I am using expect

I dont know tcl but trying to use a control statement to send requests from an input file - dont know what I am doing to be honest as I dont know tcl and dont use expect too much...

Any help?

See below
Basically I am opening a telnet session to a server which works fine and I can send one command. I want to put several commands in to a file- just an example below.. and the error when I try to run this is below also
ie - file is called test.txt and contains
df -k
ps -ef
ls -ltr /etc/

==============================
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "USERCODE:"
# Send the username, and then wait for a password prompt.
send "xxxxxxx\r"
expect "PASSWORD:"
# Send the password, and then wait for a shell prompt.
send "xxxxxxx\r"
expect "<"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"

for i in `cat testfm.txt`; send "echo $i\r" ; done
expect "<"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof


==============================


invalid command name "i"
while executing
"i"
("for" initial command)
invoked from within
"for i in `cat test.txt`"
(file "./.test_script" line 24)
# 2  
Old 11-30-2007
First problem is that your loop is in some kind of bash syntax, and you are in a TCL script.

Try looking here to get an idea on how to write proper FOR loops for expect.

Next problem is that you should be "setting" any TCL variables before you use them... again this is probably because it looks like you are confused between expect and bash...

If all you want to do is execute a series of commands on a telnet session, this is probably the most common example that exists when Googling for it... give 'er a try...

Finally, I leave you with my favorite resource on Expect, here

Enjoy!
# 3  
Old 12-03-2007
Excellent - thanks..

Got some useful tcl information from the below site - exactly what I needed

Tcl Tutorial

Previous Thread | Next Thread
Test Your Knowledge in Computers #496
Difficulty: Easy
Many programming languages assign special meaning to keywords such as for, if, and while (for example) that are used to define various control structures.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

EXPECT Bash Script Error Control

Hello Geeks once more, Thanks for all the help you have been rendering.. I have a script that depends on the output of an expect statement but sometimes the main script misbehaves which I believe is a result of SSH communication error, how can I apply an error control to know whether the... (2 Replies)
Discussion started by: infinitydon
2 Replies

2. Shell Programming and Scripting

If statement in expect script

Hi, Can anyone please help me with the below script, I'm trying to match the prompt output in "if statement" if it matches then send "y" else send "n" and come out. I'm not sure about the expect syntax. pls help me out. #!/usr/bin/expect -f set INPUT set timeout 60 spawn su - xxuser ... (0 Replies)
Discussion started by: Jai ganesh
0 Replies

3. Shell Programming and Scripting

How to make expect to leave control for user?

Hello. I have several domains which are using different LDAP trees. To make life easier i wrote such script !#/bin/bash case $1 in "doman1") login='login1' password='pass1' "doman2") login='login2' password='pass2' "doman3") login='login3' password='pass3' /usr/bin/expect... (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Do expect script have "or" statement?

Do expect script have "or" statement? sound like this: expect { "A" or "B" { send "123" } "C" { send "567" } I know expect can separate that to do this expect { "A" { send "123" } "B" { send "123" } "C" { send "567" } } (8 Replies)
Discussion started by: sk860811
8 Replies

5. Shell Programming and Scripting

How to add if statement in expect script

Hi, I am new to expect script and I am having difficulty in adding an if statement into a expect FTP login script. Here is the code: #!/usr/local/bin/expect -f set FTP_SITE set FTP_USER set FTP_PASS set FTP_FILE spawn ftp match_max 100000 expect -exact "ftp> " send -- "open... (1 Reply)
Discussion started by: jrcai
1 Replies

6. Shell Programming and Scripting

How to add control statement?

Hi everyone, I have a script in which i need to add some codes.Right now my scripts takes table name from i/p file data.txt then matches it with data in another file table.if table exists then it searches for line cotaining &quot;add&quot; and extract column from there.Now i need to do additional part in... (0 Replies)
Discussion started by: alisha
0 Replies

7. Linux

Modify expect script with control statement

Ok, so I have this script that was provided to me by one of the posters on this site. This script seems to be perfect. However, since this is a telnet script, i need to add an if then statement to it but dont know how to do it. What i want to do is to have this script spit out a certain... (3 Replies)
Discussion started by: SkySmart
3 Replies

8. Shell Programming and Scripting

If and else statement with Expect

Hi, I have a script which is logging in to network devices via ssh using expect programming. My problem is that if I do Code: ssh host -l uname It only works if y ssh_config is setup to use Protocol 1,2 and my network device I am trying to connect has ssh 1 but not 2. If the... (0 Replies)
Discussion started by: kminev
0 Replies

9. 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

10. 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

Featured Tech Videos