Register
It is currently Mon Sep 01, 2014 5:19 am

Nested loop problem with dynamic variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue Jul 22, 2008 4:54 pm   

Joined: Tue Jul 22, 2008 4:36 pm
Posts: 2
Hi Guys,

I am looking for a script with a nested loop. My script reads a list of IP addresses and runs the nmap command against each IP in the file. Said file is then used to probe which version of ssh is on that system.

IP file looks like this:
192.168.1.1
192.168.1.150

and port info file is like this:
"22" "tcp" "open" "openssh 4.7p1" "ubuntu"
"22" "tcp" "open" "openssh 4.3" "suse"

So far I have

while read ip_address
do

echo "Scanning $ip_address"

while read portinfo
service=`cat $portinfo | awk '{ print $3 }'

if [ "$portinfo" = openssh 4.7p1"
then
echo "Vulnerable SSH version found
fi
done < $ip_address.ports
done < iplist.txt

I can't seem to make this work at all, and keep seeing syntax error near line done < $ip_address.ports

Both the iplist.txt and $ip_address.ports files are already created. I have omitted this code as it is simply an nmap scan with some grepping :)

I know this is something very basic messing the script up, but I cannot see what.

Thanks,
Robert


Top
 Profile  
 PostPosted: Wed Jul 23, 2008 8:50 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
hey Robert

This is actually a pretty easy fix... but yes, it would get overlooked many times (I know I've done it billions myself)

change $ip_address.ports to ${ip_address}.ports and you should be good..

hope that helps :)


Top
 Profile  
 PostPosted: Wed Jul 23, 2008 10:37 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Also it's "while test; do foo(); done"

For example:
Code:
while read ip; do
  scan_ip($ip)
done < iplist.txt


Also I think it would be better design (And probably faster) To log all the scans to a single file, then process that. nmap has some built in output modes for easy parsing, read the manpage.


Top
 Profile  
 PostPosted: Wed Jul 23, 2008 10:49 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
well... he was also missing a backtick on a line... service=`cat $portinfo | awk '{ print $3 }'
an open quote... "$portinfo" = openssh 4.7p1"
and closed bracket.. if [ "$portinfo" = openssh 4.7p1"

but i was just pointing out what the bash interpreter was yelling about...


Top
 Profile  
 PostPosted: Wed Jul 23, 2008 12:15 pm   

Joined: Tue Jul 22, 2008 4:36 pm
Posts: 2
Hi Guys,

First of all, many thanks for your help.

my code was slightly different to that which was posted, as I was at work and did not have the code in front of me, so had to work from memory and an email of the output.

I have corrected the bug using jbsnake's {} addition. In my original code, I did have the backtick and close bracket sections :) but thanks for the heads up none the less.

While I can appreciate that combining the functions to make nmap scan a subnet is better in a programming sense, the auditing procedure we use here accounts for time spent. So although not the most optimised solution for code, I can prove via the file creation dates that I didn't spend 40 minutes drinking coffee and running a script! :)

If anyone would like a copy of the script to modify/improve I would be more than happy to send it via email or PM.

I have GPL'ed it, but don't want to post on the internet as it's my feeling the actual code could benefit a script kiddie and this is not something I would like to happen.


Top
 Profile  
 PostPosted: Thu Jul 24, 2008 9:16 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Glad the issue was resolved. I knew it wasn't the syntax errors (which is why I ignored them in the first post), just because of the error you stated. You wouldn't have gotten that far if the other things played a factor :wink:


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP