Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Ping Result Storage

Posted by dudeman (dudeman), 20 May 2008
Greetings,

I am not sure if this is possible or not,
Below is a curent snippet of a script I am writing to change passwords on switches on our network.
What I am trying to do is ping the switch and
if it is alive telnet to it and change the password
if it is not alive skip it.

How would I store the ping result in a variable and feed that to an
'if' statement?

Note: Variables are passed to the script from user input.
###########Script Start ###############
#!/usr/bin/expect

# Start of loop to ping/telnet to switches
for {set i $k} {$i <= $j} {incr i} {

set 192.168.100.$i [lindex $argv $i]
spawn ping 192.168.100.$i -c 2
# Here is where I want to add
if { "$live" } {
spawn telnet 192.168.100.$i
expect -re "login   :"
send "$un\n"
expect -re "password:"
send "$currpw\n"
send "pw\n"
send "$un\n"
send "$currpw\n"
send "$npw1\n"
send "$npw2\n"
send "exit\n"
interact
expect 0%
} else {
send "moving to the next one\n"
}
}


Posted by priyadharshini (priyadharshini), 21 May 2008
set pid [spawn ping 192.168.100.$i $port]
set id $spawn_id
expect -i $id "*0%packet loss* {spawn telnet 192.168.100.$i
expect -re "login   :"
send "$un\n"
expect -re "password:"
send "$currpw\n"
send "pw\n"
send "$un\n"
send "$currpw\n"
send "$npw1\n"
send "$npw2\n"
send "exit\n"
interact
expect 0%
}"*100% packet loss*"{ ... }

I guess it'll work.

Posted by dudeman (dudeman), 21 May 2008
I'll give it a try...

Thanks.

Posted by dudeman (dudeman), 27 May 2008
I think I figured it out for anyone who is interested, I ran this test several times on reachable and unreachable switches
Note: $ip, $i, $un, $npw1, $npw2, $currpw are passed to the script from user input from the keyboard.
Also this script is being run on CentOS

########## beginning of partial script ###############
# for loop to run through the subnet.
for {set i  $k} {$i <= $j} {incr i} {

set 192.168.$ip.$i [lindex $argv $i]

# Pings host twice
spawn ping 192.168.$ip.$i -c 2

# +2 equals failure sets variable 'to' = 0 and if "0%" it is alive sets 'to' = 1
# 'to =timeout'
expect "+2" { set to 0 } "0%" {set to 1}

# just a check to output the result of the ping test
puts "$to"

# if structure to determine whether or not to telnet to the switch
# and change the password

if { $to == 1} {
spawn telnet 192.168.$ip.$i

expect -re "login   :"
send "$un\n"
expect -re "password:"
send "$currpw\n"
send "pw\n"
send "$un\n"
send "$currpw\n"
send "$npw1\n"
send "$npw2\n"
send "exit\n"
interact
} else {
continue # keeps the loop going
}
}
################## end of script ##################



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho