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
# 8  
Old 04-11-2011
Hmm.. the bash script is not working..
But this one is usefull, thanks for that!
Code:
IP=192.168.1.5 SUBNET=${IP%.*} echo $SUBNET 192.168.1

And now the second part: the below script will ping the given ip adress:
Code:
echo "Enter IP-addres:"
read host1
ping -c 1 $host1

How can I ping from $host to $host2 in the below script??

Code:
echo "Enter beginning IP-addres:"
read host1
echo "Enter ending IP-addres:"
read host2
ping -c 1 $host1 [..] $host2

Something else then [..] must be filled in, so I can ping from beginnin ip (something like 192.168.1.1) till ending ip (something like 192.168.1.100)

Last edited by Scott; 04-11-2011 at 08:53 AM..
# 9  
Old 04-11-2011
The seq command may come in handy.
Code:
N=100
for n in $(seq 1 $N); do
    echo Do something with $n
done

# 10  
Old 04-11-2011
Only if netmask is 255.255.255.0 (24 bits)
bash code:
  1. #!/bin/bash
  2. read -p "Enter beginning IP-addres: " host1
  3. read -p "Enter ending IP-addres: " host2
  4. SUBNET=${host1%.*}
  5. netId1=${host1#$SUBNET.}
  6. netId2=${host2#$SUBNET.}
  7. for ((i=netId1; i<=netId2; i++)); do
  8.     ping -c 1 ${SUBNET}.$i
  9. done

Last edited by frans; 04-11-2011 at 02:27 PM.. Reason: Forgot two dots in lines 5 & 6
# 11  
Old 04-11-2011
What in the above script will change when it is 255.255.254.0?

And if I run the above script I get the next error:
Code:
unix1: line 7: ((: .100: syntax error: operand expected (error token is ".100")

Thanks frans, I think we are in the right direction.

Last edited by Scott; 04-11-2011 at 08:53 AM.. Reason: Please use code tags
# 12  
Old 04-11-2011
Quote:
Originally Posted by frans
bash code:
  1. netId1=${host1#$SUBNET}
  2. netId2=${host2#$SUBNET}
I think you're missing a dot.
Code:
netId1=${host1#$SUBNET.}
netId2=${host2#$SUBNET.}

This User Gave Thanks to KenJackson For This Post:
# 13  
Old 04-11-2011
Quote:
Originally Posted by KenJackson
I think you're missing a dot.
Code:
netId1=${host1#$SUBNET.}
netId2=${host2#$SUBNET.}

OMG Smilie
I'll correct the original post.
Thanks
# 14  
Old 04-12-2011
Many thanks for this one...
The above script is working! I can ping from beginning to ending IP-adresses very fast!

Lets go to the second part... Smilie
When no beginning and ending IP is given, so when the fields are empty.
The script must ping my own subnet!
This is the subnet where the system is logged in. So if I am in 192.168.100.* segmet it must ping from 192.168.100.1 to 192.168.100.255..

How can we accomplish this?

---------- Post updated at 08:35 AM ---------- Previous update was at 08:32 AM ----------

Or... the second part can be done like:

ping -b x.x.x.255

the tripple x must be filled in my the script by grepping my ip and pasting it there.
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