Help with Expect Script

Mike Jeays Mike.Jeays at rogers.com
Sun May 15 12:31:37 PDT 2005


On Sun, 2005-05-15 at 14:09, Phusion wrote:
> I'm new to writing expect scripts and need some help. The script will
> telnet to a host and run some commands. I want the script to ping the
> host to see if it's alive first before it telnets into it. Also, I
> know the host is pingable meaning it responds to pings. If the host
> doesn't respond to a ping I want it to log that to a log file and then
> quit. I already know how to telnet to the host, but not sure about the
> ping part and writing it to a log file. Could you please reply to this
> email with a little example script. Thanks.
> 
> Phusion
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
> 
You can ping a host and test whether it was successful from a shell
script, without needing to use expect.  Hope this is useful, as it
doesn't quite answer your question.  Note the "-c 1" to tell ping to try
just once.

ping -c 1 chaucer
rc1=$?
if [ $rc1 -gt 0 ]
then
  echo "Chaucer is down"
else
  echo "Chaucer is up"
fi

Here is an example of telnet from expect; a very quick and dirty way to
synchronize a clock on a very old machine.

#!/usr/local/bin/expect
set timeout 10
spawn telnet jansen
expect "]"

send "password1\r"
expect "jansen???"

send "su\r"
expect "Password:"

send "rootpassword\r"
expect "#"

exec date >/tmp/datesync.tmp
exec cat /tmp/datesync.tmp
set newtime [exec cat /tmp/datesync.tmp]
send "date -s \"$newtime\"\r"
expect "#"

send "exit\r"
expect "jansen???"

send "exit\r"
expect "host."




More information about the freebsd-questions mailing list