Modify expect script with control statement


 
Thread Tools Search this Thread
Operating Systems Linux Modify expect script with control statement
# 1  
Old 12-28-2009
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 digit response (0) if telnet is able to connect to machine. If it isn't, i want this script to spit out something else, like a 1 or 2.

I also don't want this script to output anything else but the success or failure response. Meaning, i don't want to see "spawn telnet " command being outputted to the screen.

I apologize if this is asking too much but i desperately need this. Thank you very much guys.

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

set timeout [lindex $argv 0] 
set machine [lindex $argv 1]

spawn telnet $machine

expect {
  "ogin" interact
  timeout  exit 
  }

# 2  
Old 12-29-2009
How about this:

Code:
if [ $? = 0 ];
    then
    echo "Telnet to $machine is possible"
else
    echo "Telnet to $machine failed"
fi

You can replace the echo with some logging construct

---------- Post updated at 10:25 AM ---------- Previous update was at 10:22 AM ----------

Here's a tutorial on how to translate the shell code to what expect wants:

http://floppsie.comp.glam.ac.uk/Glam...ripting/5.html
# 3  
Old 01-06-2010
The logic provided by Mark is the correct one, but you need expect version. For the silent / quiet version of your script, you need "log_user 0" at the top of your script. Change the 0 back to 1 when you need it to become chatty again.
For the script, you can use the following syntax :
Code:
if { $result == "0" } {
    set variable "string"
} elseif { $resul == "1" } {
    set variable "string"
} else {
    puts "Unsupported action or behavior : $result"
    exit 1
}

# 4  
Old 01-06-2010
Thanks, sysgate.

I don't use expect, so rather than mung it up, I gave him the method and a link to the constructs of expect.
Login or Register to Ask a Question

Previous Thread | Next Thread

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

Modify awk statement

how do i modify the following: echo "jaba law welcome no jaba law sorry now jaba law" | awk '{s+=gsub(/jaba law/,"jaba law")} END {print s}' so that it shows me the actual phrase it found matching the strings i specified? something like: jaba law jaba law jaba law (4 Replies)
Discussion started by: SkySmart
4 Replies

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

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

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

7. 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 "add" and extract column from there.Now i need to do additional part in... (0 Replies)
Discussion started by: alisha
0 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

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... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question