Ping with a variable in linux script - linux

I want that my script pings the ip-addresses
192.168.0.45
192.168.0.17
192.168.0.108
by doing this:
bash Script.sh 45 17 108
I want to give the last numbers with bash to ping this ip-addresses.
I don't know how I have to do this. Do I have to work with a 'case' in a do while or something else??

#!/bin/bash
for i in $*; do
ping 192.168.0.$i
done

I want to give the last numbers with bash to ping this ip-addresses.
I presume, you want to ping the addresses simultaneously. In that case you can do this:
Script.sh:
#!/bin/bash
ping 192.168.0.$1 & ping 192.168.0.$2 & ping 192.168.0.$3 &
This will send all of the three ping commands to background where they will be executed simultaneously and print continuous output on terminal.
You can do this with a for loop too:
#!/bin/bash
for i in $*;do
ping 192.168.0.$i &
done
The for loop method can take any number of arguments

Related

Stopping the Ping process in bash script?

I created a bash script to ping my local network to see which hosts is up and I have a problem in stopping the Ping process by using ctrl+C once it is started
the only way i found to suspend it but even the kill command doesn't work with the PID of the Ping
submask=100
for i in ${submask -le 110}
do
ping -n 2 192.168.1.$submask
((submask++))
done
Ctrl + C exit ping, but another ping starts. So you can use trap.
#!/bin/bash
exit_()
{
exit
}
submask=100
while [ $submask -le 110 ]
do
fping -c 2 192.168.77.$submask
((submask++))
trap exit_ int
done
I suggest you to limit the amount of packets sent with ping with the option -c.
I also corrected the bash syntax, guessing what you intend to do.
Finally, it is faster to run all the ping processes in parallel with the operand &.
Try:
for submask in ${100..110}
do
echo ping -c 1 192.168.1.$submask &
done

Need help for simle bash script

Can someone help me with the syntax for a simple bash script that trying to write:
echo ping -c 1
echo nslookup
Basically I want to receive output of one line from the ping and the nslookup information for a domain that I'm checking. Unfortunately I'm unable to get this correctly.
P.s. this is basically the first thing that I'm trying to accomplish in bash.
Thank you in advance!
Thank you for the provided information on the matter. I felt little ashamed from the nature of my question so I spent little more time to read. The solution that I found is the following:
#!/bin/bash
for i in $*;do
ping -c 1 $i &
nslookup $i &
done
#
'
Once I added the scrit to the /bin folder I used the commands:
chmod +x "script name"
dos2unix ""scrit name"
so not I'm able to use it only by typing the name of the script.
hi after writing and saving script and giving permission to the script
just go the folder and
./
of else you just can use
sh
Hi i would be better if you elaborate your problem, if you want one line output for ping command filter using head
eg
ping www.google.com | head -n 1
or if you are thinking of sending one packet of data to the server
ping -n 1 www.google.com
nslookup www.google.com
and if your are writing Bash Script
#!/bin/bash
ping -n 1 www.google.com
nslookup www.google.com
save the file
give execute permission and run
Please let me know i answered your question

Bash Script: options - case: range of multiple numbers

I'm working on a script in Linux Bash, with different kinds of options to use.
Basically, the program is going to ping to the given ip-address.
Now, I want to enable the user to write a range of ip-adresses in the terminal, which the program then will ping.
Fe: bash pingscript 25 - 125
the script will then ping all the addresses between 192.168.1.25 and 192.168.1.125.
That's not to hard, I just need to write a little case with
[0-9]-[0-9] ) ping (rest of code)
Now the problem is: this piece of code will only enable me to ping numbers of fe. 0 - 9 and not 10 - 25.
For that I'd need to write:
[0-9][0-9]-[0-9][0-9] (fe: ping 25 - 50)
But then there's the possibility of having 1 number on one side and 2 on the other: [0-9]-[0-9][0-9] (fe: ping 1 - 25)
or: [0-9]-[0-9][0-9][0-9] (fe: ping 1 - 125)
and so on... That means there're a lot of possibilities.
There's probably another way to write it, but how?
I don't want any letters to be in the arguments, but I can't start with that (loop-through system).
How about that:
for i in 192.168.1.{25..125}; do ping -qnc1 $i; done
Or in a script with variables as arguments:
for i in $(seq -f "192.168.1.%g" $1 $2); do ping -qnc1 -W1 $i; done
Where the first argument is the number where to begin and the second argument where to end. Call the script like this:
./script 25 125
The ping options:
-q: that ping doesn't print a summary
-n: no dns lookups
-c1: Only send 1 package
-W1: timeout to 1 second (can be increased of cource)
You can use extended pattern matching in your script by enabling the extglob shell option:
shopt -s extglob
So you can use braces with a + quantifier like this:
#!/bin/bash
shopt -s extglob
case $1 in
+([0-9])-+([0-9]) )
# do stuff
;;
esac

Passing Arguments to Running Bash Script

I have a bash script that takes a list of IP Addresses, and pings them every 15 seconds to test connectivity. Some of these IP Addresses are servers and computers as to which I have the ability to control. I would like to be able to do something of the following:
Run The Bash File
It pings non-controlled IP Addresses
It will list the controlled Computers
When a computer turns off, it sends my script a response saying it turned off
The script outputs accordingly
I have the code all set up that pings these computers every 15 seconds and displays. What I wish to achieve is to NOT ping my controlled computers. They will send a command to the bash script. I know this can be done by writing a file and reading such file, but I would like a way that changes the display AS IT HAPPENS. Would mkfifo be an viable option?
Yes, mkfifo is ok for this task. For instance, this:
mkfifo ./commandlist
while read f < ./commandlist; do
# Actions here
echo $f
done
will wait until a new line can be read from FIFO commandlist, read it into $f and execute the body.
From the outside, write to the FIFO with:
echo 42 > ./commandlist
But, why not let the remote server call this script, perhaps via SSH or even CGI? You can setup a /notify-disconnect CGI script with no parameters and get the IP address of the peer from the REMOTE_ADDR environment variable.

How do I save several running ping thread feedback/results to each related file output?

I have written below script in linux shell script, for pinging several routers in parallel and save output to files and other script analysis for packet lost on links. as you can see all pings run in background and simulate parralelism or multithreading.
for ips in 100.28.139.5 100.20.12.90 100.23.13.74 100.25.131.10
do
ping $ips -s 500 -c 500 &> ${ips}.500.text &
ping $ips -s 1500 -c 500 &> ${ips}.1500.text &
ping $ips -s 4500 -c 500 &> ${ips}.4500.text &
done
I have tried rewrite it by java but I find it so big(>100 lines) and I didn't able to save the thread results to related ping file output.
I need dedicate logger for each thread, to save outputs.
How do I save several running ping thread feedback/results to each related file output?
When you create your thread, using the costructor you will pass him certain data: suppose the url to be pinged. Using such a information you create your own file on disk where to output data coming from ping feedback.

Resources