Operating System - HP-UX
1755050 Members
3113 Online
108829 Solutions
New Discussion юеВ

Bourne Shell script writing

 
SOLVED
Go to solution
Nisar Ahmad
Regular Advisor

Bourne Shell script writing

Hi There

Please advise/help me in writing a script for the following practical situation;

The IP addresses of the computers in the company's solaris and linux labs are
grouped into ranges of thirty. It seems there's not an exact match between the "physical" and "logical" location of computers- so machines in the same
IP address range aren't necessarily located in the same classroom at the moment. The IP address ranges for each group of computers is:

Group A 138.25.9.80 - 138.25.9.109
Group B 138.25.9.110 - 138.25.9.139
Group C 138.25.9.180 - 138.25.9.209
Group D 138.25.9.210 - 138.25.9.239
Group E 138.25.11.60 - 138.25.11.89

Write a (Bourne!) shell called macfinder that gathers the MAC address and hostname for each machine in the lab. The script should accept the first IP address in the range and the Room or Group description as it's arguments. It
should write its output (30 records) to stdout. The data file format will
have the following structure:

IP Address,MAC Address,Hostname,Location or Group

for example

138.25.8.2,00:03:ba:01:7a:72,charlie,Server Room on level 3

138.25.9.1,08:00:20:fc:fa:47,linus,Server Room on level 3

The command could be issued like this

./macfinder 138.25.9.80 FIT Solaris Machines in Group A >> Part1.dat

Hint: The commands arp, ping and nslookup will be of some help in gathering
the information you require to create your records.

Thanks a lot in advance

Nisar
7 REPLIES 7
Darrell Allen
Honored Contributor

Re: Bourne Shell script writing

Hi Nisar,

What part of your script have you written so far? There are exceptions to every rule but generally people posting have made an attempt to write their own script and request assistance in correcting problems they encounter.

Of course, someone could be bored and decide to write the complete script for you.

Good luck!

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Steven Sim Kok Leong
Honored Contributor

Re: Bourne Shell script writing

Hi,

From the range of IP addresses, I would presume that your labs are all in a single Ethernet broadcast domain i.e. there is no need to route between labs. This presumption is needed for arp to be useful. Otherwise, you will only be getting the MAC address of your router.

If it is a single Ethernet broadcast domain, you can perform something like the following in your script:

# ping broadcast address
ping 138.25.9.255

# list out MAC addresses, server names, IP addresses:
arp -a

arp -a will attempt to perform a name resolution on the IP addresses. Use awk to parse the output to print out the relevant fields in the correct order.

Hope this helps. Regards.

Steven Sim Kok Leong
Trevor Dyson
Trusted Contributor

Re: Bourne Shell script writing

Hi,

HP Education have a course that will definitely help you write this script. It is called POSIX Shell Programming and will teach you the skills you need to write POSIX, Bourne and Korn shell scripts.

See http://education.hp.com for more info
I've got a little black book with me poems in
Steve Steel
Honored Contributor
Solution

Re: Bourne Shell script writing

Hi


For script examples look at

http://www.introcomp.co.uk/hpux/index.html



Just get each machien to mail you its info daily . Or collect it to a file and ftp to a central point.


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Paula J Frazer-Campbell
Honored Contributor

Re: Bourne Shell script writing

Hi Nisar,

This looks remarkable like an instructor / exam paper question.


The HINT bit at the end should guide you.

Ping
Arp
Nslookup

Look at man arp, ping and nslookup.

Paula
If you can spell SysAdmin then you is one - anon
harry d brown jr
Honored Contributor

Re: Bourne Shell script writing

Using Steven's example of broadcast ping and arp, try this:


The shell really doesn't matter because there is nothing shell related.

#!/usr/bin/ksh
#
# ping broadcast address
#
ping 138.25.9.255
#
# arp, cut the fields, pretty up the output, then sort into temporary file
#
arp -a|cut -d" " -f2,4|sed -e "s/(//" -e "s/)//" -e "s/ /,/" | sort >/tmp/macsin
put
#
# join the two files
#
join -j 1 -t, -e "NOT DEFINED" -o 2.1,2.2,1.3 /tmp/macsDB /tmp/macsinput >/tmp/n
ew_macsDB
#
# rename new file
#
mv /tmp/new_macsDB /tmp/macsDB

You have to maintain "/tmp/macsDB" with your favorite editor (vi).


Actually, thinking about this a little more, the above script won't catch some things. This is really a case for perl, the use of the bourne shell is really silly.



live free or die
harry
Live Free or Die
John Carr_2
Honored Contributor

Re: Bourne Shell script writing

Hi

you can do it like the attached script.

John.