TCL/Expect newbie

Sheepykins

Daemon Poster
Messages
556
Location
Worcestershire, England
Hi Guys,

Having a little trouble with TCL/expect.

What started off as a simple script has rocketed away from me and unfortunately its causing me some grief.

My expect script is in the following format:

#!/usr/bin/expect -f
log_user 0
###########
#Variables#
###########
set time [timestamp -format %H:%M:%S]
set date [timestamp -format %Y-%m-%d]
set ::logfile [open /tmp/output2 a]
set engine "AnalysisEngine.+Running"
set user "cisco"
set pass "password"
set sensor 10.1.1.1

######################
#Simple expect script#
######################

spawn ssh $user@$sensor
expect "*Password: "
send $pass\r
expect "*CiscoIPS# "
send "show version\r"
expect {
-re $engine {puts $::logfile "$time $date test success";}
else {puts $::logfile "$time $date test failed";}
}
send "exit\r"
log_file;

So far so good, expect matches against $engine and pushes my comment into my file with a time/date stamp.
My else statement doesnt work, but for what I want .... thats fine lol.

So now I want it to login and check 3 of my Cisco sensors and it does the above, checks all 3 and pumps 3 lines into the file about each one.

Them problem i'm having majorly is with the second sensor, if it becomes unavailable - my script stops and doesnt test the final sensor.

I have tried the timeout options I know but - due to my woeful inexperience with TCL i'm pretty annoyed.
Wishing I had time to learn it properly not mash something together but ....alas...

I'm hoping there are some veterans here who can show me the path!
 
looks like wiki has a couple options for handling timeouts.

Expect - Wikipedia, the free encyclopedia

Code:
# procedure to attempt connecting; result 0 if OK, 1 otherwise
proc connect {passw} {
  expect {
    "Password:" {
      send "$passw\r"
        expect {
          "sftp*" {
            return 0
          }
        }
    }
  }
  # timed out
  return 1
}

and

Code:
#timeout is a predefined variable in expect which by default is set to 10 sec
#spawn_id is another default variable in expect. 
#It is good practice to close spawn_id handle created by spawn command
set timeout 60 
spawn ssh $user@machine
while {1} {
  expect {
 
    eof                          {break}
    "The authenticity of host"   {send "yes\r"}
    "password:"                  {send "$password\r"}
    "*\]"                        {send "exit\r"}
  }
}
wait
close $spawn_id


i wonder if these port as is to linux using expect. i've been looking for a way to test ftp logins on a linux box.
 
Seems like expect/tcl and bash go quite nicely together, I've managed to get my if/else working and yeah - i've learned alot in the last few days trying to get the timeout option to work.

I'm a code noob though,

The problem i've found is getting expect to continue after the timeout.

My code looks a little different now:
#!/usr/bin/expect -f
log_user 0
###########
#Variables#
###########
set time [timestamp -format %H:%M:%S]
set date [timestamp -format %Y-%m-%d]
set ::logfile [open /tmp/output2 a]
set engine "AnalysisEngine.+Running"
set match [string match $engine "AnalysisEngine.+Running"]
set user "cisco"
set pass "password"
set sensor 10.1.1.1

######################
#Simple expect script#
######################

spawn ssh $user@$sensor
expect {
"*Password: " {send $pass\r}
timeout {puts $::logfile "$time $date test failed" ; exit 1}
}
expect {
"*CiscoIPS# " {send "show version\r"}
if {$match ==1} {puts $::logfile "$time $date test success"
} else {puts $::logfile "$time $date test failed";
}
}
log_file;

The script logs into the sensor fine, if it times out the failed message appears and the script exits.
If the script logs in, it string matches the version output and ==1 or ==0 is true or false and the if/else condition also puts into the logfile which is great.

the problem now is that, the script exits upon a failed login. if i use EXP_CONTINUE, the script encounters a spawn error.

This means doing a chain of logins isnt possible :/
 
Back
Top Bottom