LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-29-2009, 02:47 PM   #1
lilrazzzzz
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Rep: Reputation: 0
Unhappy Simple variable bash script help/question


I am trying to do something very simple, but after looking all over the forum I have not found my answer. I need to create a script that will scan any network for live hosts (via PING) whether it is a /24 or /30 network and output the live hosts to a text file. The problem I am running into is creating that variable to tell the script where to start the scan and where to end the scan. The expression I have been using which works [only because I am hard coding it to start at 1 and end at 255] is:
-------------------------------------
echo "What are the first 3 octets of the IP range? i.e. 192.168.1"
read range

for ip in 192.168.1.{1..255}; do
ping -c 1 -t 1 $ip > /dev/null;
if [ $? -eq 0 ]; then

echo "Please wait while ICMP packets are sent to each host..."
echo ${ip} >> IPs.txt
fi
done
-------------------------------------
This works, returning all of the live hosts in a /24 network. Except if I want to scan a /30 network then there are only 2 IP addresses to scan i.e 192.168.1.0 is the network address, 192.168.1.1 could be one live node, 192.168.1.2 could be the second live node, and 192.168.1.4 is the end of the network. So in theory there are only two IPs to scan (192.168.1.2 & 192.168.1.3 I just want to make it so where i have

for ip in 192.168.1.{1..255}; do

Just make the 1 and 255 a variable, along with the starting IP info.

So in my happy place it should look like this:

echo "What are the first 3 octets of the IP range? i.e. 192.168.1"
read range
echo "What IP address do you want to start the scan on? i.e. 1"
read start
echo "What IP address do you want to end the scan on? i.e. 4"
read end

for ip in $range.{$start..$end}; do
ping -c 1 -t 1 $ip > /dev/null;
if [ $? -eq 0 ]; then

echo "Please wait while ICMP packets are sent to each host..."
echo ${ip} >> IPs.txt
fi
done

------------------------------

The issue is I cannot get this to work, I have attempted (seq) commands for if etc and I cannot believe how much trouble I am having attempting to do something so simple. Any help would be much appreciated as I have found so many answers to my bash questions within this forum. Thanks in advance
 
Old 04-29-2009, 03:14 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I have not really understood the network problem, since I am not an expert, but you can try something like this:
Code:
for ip in $(seq -f"$range.%.0f" $start $end)
do
  echo $ip
done
does this solve the problem?

PS: the following should work, too:
Code:
for ip in $(eval echo $range.{$start..$end})
do
  echo $ip
done

Last edited by colucix; 04-29-2009 at 03:17 PM.
 
Old 04-29-2009, 03:59 PM   #3
lilrazzzzz
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Awesome I used the second one and it works. thank you very much! HOWEVER, I just realized one major problem...I learned that the $? will return the exit code from the previous line, so from my reading a successful ping will return a value 0 and a unsuccessful ping will return a value 1. Hence my IF statement, it should only output the pings that were successful to the IPs.txt. During my debugging I put echo $? to see what the return code was and they are all 1's when really some should be a 0. Any clue? When I hard coded this with static IP addresses, it worked with no problem and only the successful pings were outputted to the IPs.txt.

Code:
for ip in $(eval echo $range.{$start..$end})
ping -c 1 -t 1 $ip > /dev/null;
if [ $? -eq 0 ]; then
Once I get this problem resolved I am all set with it functioning flawlessly.
 
Old 04-29-2009, 04:30 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I think the problem is the -t option. Again I don't really understand it (Set the IP Time to Live) but if I try
Code:
$ ping -c 1 -t 1 www.google.it
PING www.l.google.com (72.14.221.103) 56(84) bytes of data.
From 192.168.100.1: icmp_seq=1 Time to live exceeded

--- www.l.google.com ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
$ echo $?
1
As you can see I get an error even if google is alive. Maybe you want the -W option instead, to set a timeout in case of absence of response. So you can do:
Code:
for ip in $(eval echo $range.{$start..$end})
do
  if ping -c1 -W1 $ip > /dev/null
  then
    echo $ip >> IPs.txt
  fi
done
Note that you can avoid the if [ $? -eq 0 ], since you can test a command directly in the if statement. It will evaluate the exit code of the command and return true if the exit code is 0, false if the exit code is any other number.
 
Old 04-29-2009, 06:50 PM   #5
lilrazzzzz
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Original Poster
Rep: Reputation: 0
wow that did it!! Amazing, sometimes my brain doesn't realize how easy it is...thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 04:08 AM
A hopefully simple bash variable usage question PTrenholme Programming 14 11-15-2008 03:53 PM
simple bash variable command itz2000 Programming 6 06-19-2006 09:24 PM
simple bash script question bullium Programming 3 11-01-2004 03:42 PM


All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration