Looping through multiple networks

How can you perform multiple loops in a script?

Say I wanted a script to record all the hostnames and their IP addresses of two particular networks and save the results in one file.

I can do this for one network but when you add a second network it causes a whole bunch of issues.
Yes I have access to both networks and yes I can ping everything (IP and Hostname)

Here is how I have it written for one network and it works pretty well.  I am looping through the third and fourth octets only.  However, I need to loop through the second octet now.
in this example the second one is 220, I would need to add 221 as well. How would I add that loop?

#Third octet
for t in 0 4 6 7 55 86 230 231
do

#Forth Octet
   for f in {1..254}
   do
   (
      ip =15.220.$t.$f
      echo processing $ip
       hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       if [ $? == 0]; then
             hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       fi
       if [ $? == 0] ; then
             echo 15.220.$t.$f-$hostname >> $OUTPUTFILE
             echo Hostname:$hostname
           echo
       fi
    ) &
   done
   wait
done

Thanks
John
John SheehySystem Security ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skullnobrainsCommented:
same way :

for s in 220 221
do
 ...
  ip=15.$s.$t.$f
  ...
done

the wait command is useless

you may want to echo the list of machines into xargs so you can run multiple tests in //
here is an example ( with a bash-ism } that scans 10 hosts at a time

for i in `seq 0 255` ; do echo "15.{220,221}.{0,4,6,7,55,86,230,231}.$i" ; done | xargs -P 10 -n 1 -i % sh -c 'sshpass ... ssh user@% ... 1>&2 && echo %' >> OUTFILE
arnoldCommented:
Instead of analyzing your code, please provide info on what you are working with.

You seem to have several segments, your IP is by default 15.220.
not sure why don't you test first whether the IP is pingable, or if the windows firewall is enabled, test a connection.

What are you waiting between the inner and outer ?
not sure why you have 1 as the starting point, if you have a non-standard default gateway on the segment, it will not be excluded from the attempt.


Why are you running the same command twice, if you want to make two attemps, why not use a while loop with a break on a second attempt?

Or in the existing, why not use else when the first succeeds no need to run the second.

"    echo processing $ip
       hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       if [ $? == 0]; then
             hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       fi
       if [ $? == 0] ; then
             echo 15.220.$t.$f-$hostname >> $OUTPUTFILE
             echo Hostname:$hostname
           echo "
John SheehySystem Security ManagerAuthor Commented:
We are running Red Hat EL 6.5.  All machines are Linux no Windows.
This is a closed network with no attachment to the outside world.

We started with one network and that lasted quite some time.  Well when we expanded a new closed network was created and it's quite a distance away.  So the network folks went ahead and created a connection between both networks.  Same IP schema just changed the second octet.

So because we are expanding quite rapidly the Sys. Admins are adding machines one a daily basis.  

I run audits on a daily basis and I need to know what machines have been added.  So I run the script I posted above and that returns all the machines that can be pinged going through a list of networks and IPs and outputs the IPaddress and Hostname to a file.  I then run a separate scripts that compares today's file with yesterdays file and let's me know what was added or what was removed.

I created a separate script that does the exact same thing as the one I posted; just changed the 220 to 221 and let that script merge the two files.  Which is exactly what I am doing right now.  I was looking to see if I could eliminate one script by adding the second network to the first script.  Just when i did it I added the these lines:

#Second Octet
for s in 220 221
do

and then changed ip =15.220.$t.$f   to    ip=15.$s.$t.$f.

Those changes did not work.  I am not by any means an expert at scripting so everything I know is all self taught.  So if my scripts don't make sense I'm ok with that.  They work and that's what I need them to do.  But I am always eager to learn more and why something doesn't make sense to someone who has been doing this for a while.

John
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

arnoldCommented:
you need three loops as skull provided that is the outer most to change the third.

The issue you will run into is if the third octet are ....

second octet loop
#Second Octet
for s in 220 221
do

#Third octet
for t in 0 4 6 7 55 86 230 231
do

#Forth Octet
   for f in {1..254}
   do
   (
      ip =15.$s.$t.$f
      echo processing $ip
       hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       if [ $? == 0]; then
             hostname =`sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o Connection Attempts=1 -o NumberofpasswordPrompts=1 $user@$ip "hostname"
       fi
       if [ $? == 0] ; then
             echo 15.220.$t.$f-$hostname >> $OUTPUTFILE
             echo Hostname:$hostname
           echo
       fi
    ) &
   done
   wait
done # third octet

done #second octet

Open in new window


I combined your code with Skull's example

The presumption that the same third segments are in use on the new network as they are on the prior.

If not, those segments on the new 221 that are not in use, will take longer to process because the access attempts will have to wait till they timeout.

I am puzzled why do you attempt two accesses sequentially?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skullnobrainsCommented:
@arnold provided a complete code. looks good to me.

you may also want to try this implementation ( -P 10 is the parallelism for the scan )

for i in `seq 0 255` ; do printf '%s ' 15.{220,221}.{0,4,6,7,55,86,230,231}.$i ; done | xargs -P 10 -n 1 -I£ sshpass -p $pw ssh -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o ConnectionAttempts=1 -o NumberofpasswordPrompts=1 $user@£ 'printf %s-%s £ `hostname`'

Open in new window

John SheehySystem Security ManagerAuthor Commented:
Sorry for the long delay in a response.  
I made the correction to the script and it is running now.  Seems to e working but there is a slight delay going from one network to another.  But that is just fine for me.

As far as doing it sequentially.  A few of the networks have a different login name.  So where it says $user@ip in the actual script there is $user and $user2  I didn't want to make it too complicated for the question I was asking.  But that is why.  

As soon as the script finishes I will update and close this question and give credit where credit is due.

John
John SheehySystem Security ManagerAuthor Commented:
The solutions provided have been great.  And they are exactly what I needed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.