I have an expect script that is pretty simply. It is designed to turn a list of
ports on and off at random times. I thought events handlers, associated with
the after command made the most sense. that is until I couldn't get them to
work. The script is shown below. What I see in my log file is the calls to the
7 ports in the list to turn the ports on. I also see in the console the 7 timer
events to turn the ports off. The problem is the call to autoPortOff is never
invoked, or at least nothing shows up in the log file showing it was invoked.
Any clues?
Bob Bartis
#!/usr/add-on/exptools/bin/expect -f
# List of ports to control
set portList [list 1 2 3 4 5 6 7]
# IP address of power switch
set ipAddress "135.112.119.66"
#
# Expected prompt from power switch
#
set prompt "NPS>"
#
# Moment in time script was started. Used to calculate relative time
# between log Messages
#
global start
set start [clock seconds]
#
# Define log file name and open for writing. Each log file will be given a
unique name
#
global ofid
set logFile "out-$start.txt"
set ofid [open $logFile w]
spawn telnet $ipAddress
expect "$prompt"
#
#
proc bgerror {msg} {
puts $ofid "Background Error: $msg"
}
#
# returns random number between the min and max range provided
# as input parameters. Min defaults to 2 and Max to 600
#
proc randNum { {min 2} {max 600} } {
set mult 1
# calculate an appropriate multiplier to satisfy the max value.
set len [string length $max]
for {set i 0} {$i < $len} {incr i} {append mult 0}
while {1} {
set ret [expr round([expr rand()] * $mult)]
if { ($ret >= $min) && ($ret <= $max)} {
break
}
}
return $ret
}
#
# Procedure to delay execution of the script for val seconds. Blocking if the
cmd parameter is
# zero length or null. If provided the procedure returns immediately, but
creates an event handler
# that will be invoked sec later. The val of the cmd parameter represents the
cmd and its arguments
# is any.
#
proc afterSec {val {cmd ""} } {
set secMult 1000
after [expr $val * $secMult] "$cmd"
}
#
# Procedure to turn a given port on of off. The procedure enforces a .5sec
delay to ensure the power
# node is not overridden with command requests.
#
proc portOnOff {onOff port} {
global prompt
puts "$onOff $port\r"
send -- "\/off $port\r"
expect "$prompt"
afterSec .5
}
#
# Procedure that turns a given port on and also selects a random time to delay
before turning
# the same port off. The on time is selected by calling the randNum procedure
with no parameters
# thereby using the min/max default values provided by the procedure. The
aftersec call returns
# immediately, but invokes an event handler to turn the port off after the
random delay has expired.
#
proc autoPortOn {port} {
portOnOff "on" $port
set delay [randNum]
log $port $delay
afterSec $delay "autoPortOff $port"
}
#
# Same as portOn, but with the opposite effect
#
proc autoPortOff {port} {
portOnOff "off" $port
set delay [randNum]
log $port $delay
afterSec $delay "autoPortOn $port"
}
#
# Procedure to log the on off actions. Date, elapsed time from start of script,
port
# and delay in seconds are saved in a comma separated format for charting later
#
proc log {port delay} {
global ofid
global start
puts $ofid "[clock format [clock seconds] -format {%D %R}],[expr [clock
seconds] - $start],[set port],[set delay]sec"
flush $ofid
}
##########################################
######### Script ####################
##########################################
foreach p $portList {autoPortOn $p}
while {1} {
after 250
update
}
send -- "\/x\r"
close $ofid
Robert M. Bartis
Lucent Technologies, Inc
Tel: +1 732 949 4565
Mail: <[EMAIL PROTECTED]>
Pgr: <[EMAIL PROTECTED]>
-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt
_______________________________________________
vtcl-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vtcl-user