Unix Shell - BASH vs Expect

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > BASH vs Expect





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author BASH vs Expect
keith.phipps@gmail.com

2006-09-18, 7:50 pm

I've recently been promoted to an admin position at work. They've put
me in charge of the unix boxes. There are way too many to list here,
but there are 4 development labs, 2 support labs, 3 colos (about 6
racks each), and the internal server room.

So, having said that - I must admit I'm script stupid. I've only ever
had to work on one or two boxes at a time so I've never really needed
to script anything. I think that's going to change - starting about 10
minutes ago.

I've been tasked with writing a script (or doing it by hand) ssh'ing to
each server to test the internal IP and the external IP to make sure
it's responding on a certain port.

Now, I got through about 10 today and decided that there has got to be
a way to script this.

I asked one of the code guys at work who said bash would be a "dumb way
to do it" and suggested "expect". So I went to my bookstore today
(three in fact) and they had nothing on expect. I found a book online
and a few online resources - but nothing that I've been able to wrap my
head around.

So my question is two fold:

Should I use bash or expect (or something else - this is a FreeBSD
environment)

and

I just need something that will go out - hit the port - come back and
let me know if it got a response in x ms or timed out or what not. I
don't want to necessarily login to each box and terminate a session.

This way when I get in in the morning I can run it (or run it on cron)
and see what gets my attention first.

I've got a monitoring server (Nagios) that's internal that will hit the
internal IP's - but this script is going to be setup outside the
network to hit our IP's from a customer / field guy standpoint.

Sorry for the rambling - but having been fighting setting up bacula to
all these servers all day and getting Nagios sorted out - it's been a
rather stressful day ;)

-Keith

Bruce Barnett

2006-09-18, 7:50 pm

keith.phipps@gmail.com writes:

> I asked one of the code guys at work who said bash would be a "dumb way
> to do it" and suggested "expect". So I went to my bookstore today
> (three in fact) and they had nothing on expect. I found a book online
> and a few online resources - but nothing that I've been able to wrap my
> head around.


Buy a book? What sort of hacker are you?! :-)

Get the source code and look at the examples in the ./example directory.

Here's a start - ./example/rlogin-cwd

---------------
#!../expect --
# rlogin-cwd - rlogin but with same directory
#
# You can extend this idea to save any arbitrary information across rlogin
# Don Libes - Oct 17, 1991.

set prompt "(%|#|\\$) $" ;# default prompt
catch {set prompt $env(EXPECT_PROMPT)}

eval spawn rlogin $argv
set timeout 60
expect eof exit timeout {send_user "timed out\n"; exit} -re $prompt
send "cd [pwd]\r"
interact




-----


You may want to add something like

expect "password:"
send "asIfIdTellYou\r"



--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
mark_horn@sbcglobal.net

2006-09-19, 1:35 am


keith.phipps@gmail.com wrote:
> I asked one of the code guys at work who said bash would be a "dumb way
> to do it" and suggested "expect". So I went to my bookstore today
> (three in fact) and they had nothing on expect. I found a book online
> and a few online resources - but nothing that I've been able to wrap my
> head around.
>
> So my question is two fold:
>
> Should I use bash or expect (or something else - this is a FreeBSD
> environment)
>
> and
>
> I just need something that will go out - hit the port - come back and
> let me know if it got a response in x ms or timed out or what not. I
> don't want to necessarily login to each box and terminate a session.


So long as everything is text-based wrt to the "expected" ACKs from the
remotes (and what you need to send), expect is definitely the elegant
solution... check out the home page:

expect.nist.gov/

good luck (no worries),

mj horn

Why Tea

2006-09-19, 1:35 am

>
> So long as everything is text-based wrt to the "expected" ACKs from the
> remotes (and what you need to send), expect is definitely the elegant


>From my own experience, this is definitely true.


But since you are using SSH, the example of "rlogin" script will not
work for you. I believe the SSH server somehow "detects" it's dealing
with an automatic script and hence the login procedure always fails -
someone can probably explain the details of how SSH server works during
login. The way to get around that is to use ssh_agent and ssh_add.

mr.bmonroe@gmail.com

2006-09-19, 7:25 pm

Keith,

> I asked one of the code guys at work who said bash would be a "dumb way
> to do it" and suggested "expect". So I went to my bookstore today
> (three in fact) and they had nothing on expect. I found a book online
> and a few online resources - but nothing that I've been able to wrap my
> head around.


> So my question is two fold:
>
> Should I use bash or expect (or something else - this is a FreeBSD
> environment)


Bash alone would not be the best way to do it...but neither would
expect. If I am reading your request right, you can do everything you
want with one line using the nmap utility (though it will be a long
line. If you don't have it, then you can download it for free off
the web. Nmap will tell you exactly which ports are open and is very
flexible (>1600 line man page). If you want to make it pritty, then
use bash, awk, or perl.

Also, in a general sense, don't worry about expect, it's not that
usefull...or I should say learn a shell (ksh, bash, or zsh) and perl.
If you know those two then expect is not very usefull.

--Brett

Glenn Jackman

2006-09-20, 7:34 am

At 2006-09-19 06:11PM, "mr.bmonroe@gmail.com" wrote:
> Also, in a general sense, don't worry about expect, it's not that
> usefull...or I should say learn a shell (ksh, bash, or zsh) and perl.
> If you know those two then expect is not very usefull.


???

Expect is the single best tool to script an interactive application.

--
Glenn Jackman
Ulterior Designer
mr.bmonroe@gmail.com

2006-09-20, 1:29 pm


Glenn Jackman wrote:
> At 2006-09-19 06:11PM, "mr.bmonroe@gmail.com" wrote:
>
> ???
>
> Expect is the single best tool to script an interactive application.


Glen,

Yes, I agree but how often do you you need to use it? I've been an
Unix Admin for ~8 years now and can count on one hand (with missing
fingers) the number of time I've NEEDED to use expect. Of course
different jobs need different tools and if someone needs expect they
should use it. I just rarly need to use it because many interactive
commands I have run into support the shell <<EOF syntax.

--Brett

Glenn Jackman

2006-09-20, 1:29 pm

At 2006-09-20 10:45AM, "mr.bmonroe@gmail.com" wrote:
>
> Glenn Jackman wrote:
>
> Glen,
>
> Yes, I agree but how often do you you need to use it? I've been an
> Unix Admin for ~8 years now and can count on one hand (with missing
> fingers) the number of time I've NEEDED to use expect. Of course
> different jobs need different tools and if someone needs expect they
> should use it. I just rarly need to use it because many interactive
> commands I have run into support the shell <<EOF syntax.


True, I don't use expect a lot, but when you need it it's invaluable.
Depending on the application, entering passwords often does not work
with heredocs.

--
Glenn Jackman
Ulterior Designer
Why Tea

2006-09-21, 1:34 am


> True, I don't use expect a lot, but when you need it it's invaluable.
> Depending on the application, entering passwords often does not work
> with heredocs.



I don't use Expect a lot in admin but I used it quite a lot in
automated testings and it's invaluable. I believe it's always possible
to get Expect to enter passwords correctly. The hardest to get it right
was on SSH, but there are info on the Web to show you exactly how to do
it (as usual).

Kenny McCormack

2006-09-21, 7:33 am

In article <1158809524.357329.75850@d34g2000cwd.googlegroups.com>,
Why Tea <ytlim1@gmail.com> wrote:
>
>
>
>I don't use Expect a lot in admin but I used it quite a lot in
>automated testings and it's invaluable. I believe it's always possible
>to get Expect to enter passwords correctly. The hardest to get it right
>was on SSH, but there are info on the Web to show you exactly how to do
>it (as usual).


Can you give me a pointer (URL) to that info?

Why Tea

2006-09-22, 1:41 am

> >I don't use Expect a lot in admin but I used it quite a lot in
>
> Can you give me a pointer (URL) to that info?


This page, http://www.aerospacesoftware.com/ssh-login-howto.html,
should give you enough tips of how to do it.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com