Bash script for ping in your own subnet


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script for ping in your own subnet
# 1  
Old 04-09-2011
Power Bash script for ping in your own subnet

I have a question for one who wants to help me.

I want to create a bash script to ping IP-adresses. It must first ask me the beginnen IP, then the ending IP like: 192.168.100.1 - 192.168.100.255.
When nothing is filled in, then it must find my subnet and ping that, like when my ip is 192.168.100.6, it must ping from 192.168.100.1 - 192.168.100.255.

Yes I do have a script where the IP is already filled in, but not one, which knows in which subnet you are, to ping all of the IP adresses there...

How can I accomplish this?
Help is welcome!
# 2  
Old 04-09-2011
If you want to know your external IP, this is the simplest way:
Code:
IP="$(curl -s jackson.io/ip/)"

If you want to know your local IP, this should work:
Code:
DEV="$(netstat -nr | sed -n '/^0.0/s/.* //p')"
IP="$(/sbin/ifconfig $DEV 2>/dev/null | sed -n '/net /s/.*addr:\([0-9.]*\).*/\1/p')"

# 3  
Old 04-09-2011
netmask (utility of the Debian project) can be useful
Code:
~$ netmask -r 192.168.100.6/24
192.168.100.0-192.168.100.255 (256)

perts of the the man page:
Code:
SYNOPSIS
       netmask [ options ] spec [ spec ... ]
DESCRIPTION
       This  program  accepts and produces a variety of common network address and netmask formats.  Not only can it convert address and
       netmask notations, but it will optimize the masks to generate the smallest list of rules.  This is very handy if you've ever con‐
       figured  a  firewall  or  router and some nasty network administrator before you decided that base 10 numbers were good places to
       start and end groups of machines.

(...)

DEFINITIONS
       A spec is an address specification, it can look like:

address             One address.
address1:address2   All addresses from address1 to address2.
address1:+address2  All addresses from address1 to address1+address2.
address/mask        A group starting at address spanning mask.

       An address is an internet network address, it can look like:
ftp.gnu.org   An internet hostname.
209.81.8.252  A standard dotted quad internet address notation.
100           A decimal number (100 in this case).
0100          An octal number preceded by "0" (64 in this case).
0x100         A hexadecimal number preceded by "0x" (256 in this case).

       A mask is a network mask, it can look like:
255.255.224.0  A dotted quad netmask (netmask will complain if it is not a valid netmask).
0.0.31.255     A Cisco style inverse netmask (with the same checks).
8              The number of bits set to one from the left (CIDR notation).
010            The number of bits set to one from the left in octal.
0x10           The number of bits set to one from the left in hexadecimal.

AUTHOR
       netmask was written by Robert Stone.  Some algorithm design and optimization was provided by Tom  Lear.   This  manual  page  was
       written by Robert Stone.

# 4  
Old 04-10-2011
I use the script below to see my own IP from bash script:
Code:
#!/bin/bash
ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1 }'

It will show your IP. If my IP is something like 192.168.1.5, it has to ping every addres in the 192.168.1.* segment.
This script must also ping every address when I am also in a different segment like 192.1.1.*

How can I accomplish that?

---------- Post updated at 06:55 AM ---------- Previous update was at 06:18 AM ----------

The above will only grep the whole ip. I want to grep 192.168.1.
Only the first 3 adresses. The last one will be filled in by a sequence {1..255}

The question is how to grep the first 3 numbers. now it is 192.168.1 but on a different subnet it could be something like 10.1.1.*

Last edited by Scott; 04-11-2011 at 08:52 AM.. Reason: Code tags
# 5  
Old 04-10-2011
You're assuming it's a 24-bit subnet. That's usually a good assumption with a local address like 192.168.x.x. But there's no reason it couldn't be a 16-bit or 26-bit or other size subnet (16-bit or larger for 192.168).

The Genmask column of the netstat -nr command output reveals the size of the netmask. A 24-bit subnet is shown as 255.255.255.0. A 27-bit subnet would be 255.255.255.224. Both are legitimate.

To answer your 3-number question:
Code:
IP=192.168.1.5
SUBNET=${IP%.*}
echo $SUBNET
192.168.1

# 6  
Old 04-10-2011
Here is my try, matching output for IP in all interfaces on machine.
Ping it once, if ping failes, print it (or do whatever you want there)

Code:
/inet addr:?[0-9]*.?[0-9]+\.[0-9]+\.[0-9]+/ {
gsub("(addr:|addr:127.0.0.1|.[0-9]*$)","",$2)
split($2,ipz,"\n")
	for ( i in ipz)
		for ( j=1; j <= 255; j++ )
		if (system("ping -c 1 " ipz[i]"."j ) != 0)
		print "ping FAILED on "ipz[i]"."j
}

Save this as pong.awk and run like
Code:
ifconfig | awk -f pong.awk

It will take a while ofcourse to 'scan' everything, if you have multiple interfaces in various subnets.
# 7  
Old 04-10-2011
Quote:
Originally Posted by KenJackson
You're assuming it's a 24-bit subnet. That's usually a good assumption with a local address like 192.168.x.x. But there's no reason it couldn't be a 16-bit or 26-bit or other size subnet (16-bit or larger for 192.168).
Yhat's why you can use the IP command to get information about address - mask, i.e:ip addr show eth0returns me
Code:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:21:97:22:e8:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.2/28 brd 192.168.0.15 scope global eth0

You can then tail/grep/sed/awk or what you can to get what you need (in this example, the mask has 28 bits).

You can also try ip route list which gives other useful information.

All those commands can be used to get info to ping the whole subnet.

Last edited by frans; 04-10-2011 at 02:53 PM.. Reason: added info aboute ip route
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Loop in /24 Subnet, No ping beyond .1 and .2

Running 3650 switch. I have this odd issue where I cannot get 4 new Centos 7 boxes pinging out on public IPs (nor pinging in), only gateway .1 and first public IP .2 This is what I see, which doesn't look normal. How do I fix this? The server itself is configured fine (Centos 7) # cat... (0 Replies)
Discussion started by: Bashed
0 Replies

2. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

3. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

4. Solaris

Guest LDOMS on same subnet cant ping eachother

Hi all, New to this forum. I have just been reading through a historical thread about some issues with IPMP. Some tips from "Peasant" where very useful. Please see below "Just couple of more hints regarding VM. For VDS, use one VDS - one guest LDOM, don't put everything in primary-vds.... (9 Replies)
Discussion started by: selectstar
9 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Linux

Ping check failed from Nagios master server on windows hosts in the same subnet

Hello All, We have added a windows host and its config files to Nagios master server and wanted to do a ping check alone at the moment however, the nagios master server identifies the host in its GUI and immediately disappears can anyone let me know the right approach to this one, We want to... (2 Replies)
Discussion started by: lovesaikrishna
2 Replies

7. UNIX for Dummies Questions & Answers

Ping bash script to file

Hello: I have this script: #!/bin/bash #for loop for ip in `cat ips` do ping $ip | grep "is alive">>pingtestlog done And its working properly with this input: ericadm@amxcruas1> cat ips 10.196.60.4 10.196.61.210 10.196.62.73 10.196.61.152 (5 Replies)
Discussion started by: asenav1
5 Replies

8. IP Networking

Migrating existing Subnet to a new subnet and changing ip addresses in UNIX

Hi, My project needs to migrate the existing Subnet (255.255.255.0) to a new subnet and change the ipaddresses (currently C class). How can I do that. I need some information. John (0 Replies)
Discussion started by: johnmarsh
0 Replies

9. UNIX for Dummies Questions & Answers

Unix TCP/IP ping to other subnet

I have Digital UNIX V4.0B (Rev. 564) on alpha machine. I'm trying to acces network in subnet (192.168.1.x). Ip on Alpha comp. is from 192.168.3.X subnet. My default gateway on this network is 192.168.3.1 and it working OK from other machines. This machine is visible from same subnet... (2 Replies)
Discussion started by: ermingut
2 Replies

10. Shell Programming and Scripting

Ping script using Redhat and BAsh

Alright, I have being checking out various posting here trying to hack together something for a friend Needed: A script that can run in cron doing: ping of several hosts and notifying via email when they are unavailable. I am not going to post my non working messes (I am a basic... (11 Replies)
Discussion started by: edkung
11 Replies
Login or Register to Ask a Question