Ping certain range of ip address


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Ping certain range of ip address
# 1  
Old 11-23-2016
Linux Ping certain range of ip address

hey guys,
In my program i am giving an initial ip & end ip.
i just want to check every ip in that range is pingable or not

Code:
echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
for (( i=0; i<=$endip; i++ ))
do
if ping -c 4 $inip
then
echo "pingable"
else
echo "not pingable"
fi
done
echo "Done"

my initial ip is 172.30.36.1
my end ip is 172.30.36.10
How do i increment the last digits in a loop
# 2  
Old 11-23-2016
Try
Code:
BIP1=$((0x$(printf "%02X" ${inip//./ })));
BIP2=$((0x$(printf "%02X" ${endip//./ })));
for ((I=BIP1; I<=BIP2; I++))
  do ping $(printf "%d.%d.%d.%d\n" $((I>>24)) $((I>>16&255)) $((I>>8&255))  $((I&255)) )
  done

# 3  
Old 11-23-2016
This is how i tried
Code:
echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
a= echo $inip | awk -F. '{print $4}'
b= echo $endip | awk -F. '{print $4}'
c= echo $inip | head -c10

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
if ping -c 4 $c$i
then
echo "pingable"
else
echo "not pingable"
fi
done
}
pingfunc
echo "Done"

but when i try to run it shows me an error
./pingtest.sh: line 11: ((: i=: syntax error: operand expected (error token is "=")
Help me out from this
# 4  
Old 11-23-2016
Your variable assignment doesn't work that way:
- no space after the = sign
- use "command substitution" $(...) for the pipes

You could use shell's "parameter expansion" (instead of above pipes) to extract patterns/substrings from the variables for the assignments.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-24-2016
Hey Rudic,
Thanks for the information
As u said there should be no space after the = sign
i just changed the code like this,its working as expected
Code:
echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
a=$(echo $inip | awk -F. '{print $4}')
b=$(echo $endip | awk -F. '{print $4}')
c=$(echo $inip | head -c10)

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
if ping -c 4 $c$i
then
echo "pingable"
else
echo "not pingable"
fi
done
}
pingfunc
echo "Done"


Last edited by Meeran Rizvi; 11-24-2016 at 04:16 AM..
# 6  
Old 11-24-2016
Try also
Code:
IP1=172.30.36.1
IP2=172.30.36.10
for ((i=${IP1##*.}; i<=${IP2##*.}; i++)); do echo ping -c4 ${IP1%.*}.$i; done

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. UNIX for Advanced & Expert Users

C program to detect duplicate ip address if any after assigning ip address to ethernet interface

Hi , Could someone let me know how to detect duplicate ip address after assigning ip address to ethernet interface using c program (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies

3. IP Networking

Ping domain indicates the external ip address and not the local ip

when i execute from local machine ping domainname i get the external ip address but i am on local dns and i expect the local ip address.. using nslookup : no problem so i cannot find why... thanks (2 Replies)
Discussion started by: activedms
2 Replies

4. Shell Programming and Scripting

How to get reason for ping failure using perls Net::Ping->new("icmp");?

Hi I am using perl to ping a list of nodes - with script below : $p = Net::Ping->new("icmp"); if ($p->ping($host,1)){ print "$host is alive.\n"; } else { print "$host is unreacheable.\n"; } $p->close();... (4 Replies)
Discussion started by: tavanagh
4 Replies

5. Shell Programming and Scripting

PING to get ip address

I want to print the domain:ip for a list of domains in a text file. the problem is godaddy has blocked the domains so the result of a ping is like this: PING domain.com (68.178.232.99): 56 data bytes --- domain.com ping statistics --- 1 packets transmitted, 0 packets received, 100.0%... (4 Replies)
Discussion started by: vanessafan99
4 Replies

6. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

7. Solaris

ping with source address

Hello, is there any way to ping with source address on Solaris ? (3 Replies)
Discussion started by: marmellata
3 Replies

8. Shell Programming and Scripting

Shell script to ping a range of IPs

Hi Can someone give me a shell script that can ping a range of IPs and return IPs which are not pingable. Range for example say 192.168.0.1 to 192.168.0.50 and whichever are not pingable then return the IP. Thanks for your help (3 Replies)
Discussion started by: tannu
3 Replies

9. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

10. UNIX for Dummies Questions & Answers

Ping file with ip address

Hi, If have a file with 93 ip address that i want to ping in first place (later i want to use this file for snmp output of those ip addresses). First i want to do is to ping all those ip addresses. I know i can put for every ip address "ping" with the vi. But i want the keep this file... (3 Replies)
Discussion started by: Rensen
3 Replies
Login or Register to Ask a Question