Help with Expect

Mike Jeays Mike.Jeays at rogers.com
Sun May 22 20:19:31 PDT 2005


On Sun, 2005-05-22 at 20:30, Phusion wrote:
> I need some help with an expect script I'm trying to write. Here's
> what I would like to do.
> 
> - Ping the host to see if it's up.
>   a. If the host responds to pings telnet into it.
>   b. If the host doesn't respond to pings write that to a log file and
> close the expect script properly.
> 
> The host does respond to pings. I was thinking if I see a ttl in the
> response packet to assume it's up and telnet into it. Let me know how
> I can do the following with expect. Also, how do I close an expect
> script properly? Thanks.
> _______________________________________________
> 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"
> 
I sent you these examples to a similar request a few days ago, and
didn't get any acknowledgement.  Did you not receive it, or is it not
clear, or do you need more help?

-----------------------------------------------------

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