Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

You can Add a comment or ranking to this page

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