Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 May 2005 15:31:32 -0400
From:      Mike Jeays <Mike.Jeays@rogers.com>
To:        Phusion <phusion2k@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Help with Expect Script
Message-ID:  <1116185492.86936.52.camel@chaucer>
In-Reply-To: <c3ed3fdc05051511094476c47e@mail.gmail.com>
References:  <c3ed3fdc05051511094476c47e@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@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."




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1116185492.86936.52.camel>