Display Hosts Alive with Fping

Display hosts alive with fping

You can speedup your network scan by adding additional flags:

fping -A -d -a -q -g -a -i 1 -r 0 192.168.1.0/24

−i n
The minimum amount of time (in milliseconds) between sending a ping packet to any target (default is 25).

−r n
Retry limit (default 3). This is the number of times an attempt at pinging a target will be made, not including the first try.

You need to be root if n < 10.

fping hosts in file and return down ips

It seems that parsing the data from fping is somewhat difficult. It allows the parsing of data for hosts that is alive but not dead. As a way round the issue and to allow for multiple host processing simultaneously with -f, all the hosts that are alive are placed in a variable called alive and then the hosts in the /tmp/hosts.txt file are looped through and grepped against the variable alive to decipher whether the host is alive or dead. A return code of 1 signifies that grep cannot find the host in alive and hence an addition to down.log.

alive=$(fping -c 1 -f ipsfile | awk -F: '{ print $1 }')
while read line
do
grep -q -o $line <<<$alive
if [[ "$?" == "1" ]]
then
echo $line >> down.log
fi
done < /tmp/hosts.txt

Fastest way to ping a network range and return responsive hosts?

You should use NMAP:

nmap -T5 -sP 192.168.0.0-255

Checking host availability by using ping in bash scripts

Ping returns different exit codes depending on the type of error.

ping 256.256.256.256 ; echo $?
# 68

ping -c 1 127.0.0.1 ; echo $?
# 0

ping -c 1 192.168.1.5 ; echo $?
# 2

0 means host reachable

2 means unreachable

PHP efficiency ping array of machines

PHP won't be the slow part here, the system ping command will be. Consider the worst case scenario where all of the hosts are offline. You're going to have a minimum waiting time of TTL * NumHosts.

A better solution would be having a background process running that pings the hosts every X seconds and updates some sort of status marker (flat file, database table, etc). The outward facing page will read those status markers instantly and the information will never be more than X seconds old. This also has the added benefit of potentially reducing strain on your server and the target hosts by limiting the amount of pinging that occurs.

If that setup is not a viable option, your best bet is to fiddle with the ping options or find a different tool.

How can I write a Linux bash script that tells me which computers are ON in my LAN?

I would suggest using nmap's ping-scan flag,

$ nmap -sn 192.168.1.60-70

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2009-04-09 20:13 BST
Host machine1.home (192.168.1.64) appears to be up.
Host machine2.home (192.168.1.65) appears to be up.
Nmap finished: 11 IP addresses (2 hosts up) scanned in 0.235 seconds

That said, if you want to write it yourself (which is fair enough), this is how I would do it:

for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done

..and an explanation of each bit of the above command:

Generating list of IP addresses

You can use the {1..10} syntax to generate a list of numbers, for example..

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10

(it's also useful for things like mkdir {dir1,dir2}/{sub1,sub2} - which makes dir1 and dir2, each containing sub1 and sub2)

So, to generate a list of IP's, we'd do something like

$ echo 192.168.1.{1..10}
192.168.1.1 192.168.1.2 [...] 192.168.1.10

Loops

To loop over something in bash, you use for:

$ for thingy in 1 2 3; do echo $thingy; done
1
2
3

Pinging

Next, to ping.. The ping command varies a bit with different operating-systems, different distributions/versions (I'm using OS X currently)

By default (again, on the OS X version of ping) it will ping until interrupted, which isn't going to work for this, so ping -c 1 will only try sending one packet, which should be enough to determine if a machine is up.

Another problem is the timeout value, which seems to be 11 seconds on this version of ping.. It's changed using the -t flag. One second should be enough to see if a machine on the local network is alive or not.

So, the ping command we'll use is..

$ ping -c 1 -t 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

Checking ping result

Next, we need to know if the machine replied or not..

We can use the && operator to run a command if the first succeeds, for example:

$ echo && echo "It works"

It works
$ nonexistantcommand && echo "This should not echo"
-bash: nonexistantcommand: command not found

Good, so we can do..

ping -c 1 -t 1 192.168.1.1 && echo "192.168.1.1 is up!"

The other way would be to use the exit code from ping.. The ping command will exit with exit-code 0 (success) if it worked, and a non-zero code if it failed. In bash you get the last commands exit code with the variable $?

So, to check if the command worked, we'd do..

ping -c 1 -t 1 192.168.1.1;
if [ $? -eq 0 ]; then
echo "192.168.1.1 is up";
else
echo "ip is down";
fi

Hiding ping output

Last thing, we don't need to see the ping output, so we can redirect stdout to /dev/null with the > redirection, for example:

$ ping -c 1 -t 1 192.168.1.1 > /dev/null && echo "IP is up"
IP is up

And to redirect stderr (to discard the ping: sendto: Host is down messages), you use 2> - for example:

$ errorcausingcommand
-bash: errorcausingcommand: command not found
$ errorcausingcommand 2> /dev/null
$

The script

So, to combine all that..

for ip in 192.168.1.{1..10}; do  # for loop and the {} operator
ping -c 1 -t 1 192.168.1.1 > /dev/null 2> /dev/null # ping and discard output
if [ $? -eq 0 ]; then # check the exit code
echo "${ip} is up" # display the output
# you could send this to a log file by using the >>pinglog.txt redirect
else
echo "${ip} is down"
fi
done

Or, using the && method, in a one-liner:

for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done

Problem

It's slow.. Each ping command takes about 1 second (since we set the -t timeout flag to 1 second). It can only run one ping command at a time.. The obvious way around this is to use threads, so you can run concurrent commands, but that's beyond what you should use bash for..

"Python threads - a first example" explains how to use the Python threading module to write a multi-threaded ping'er.. Although at that point, I would once again suggest using nmap -sn..

Linux shell (sh) CLI test if ping successful

dt=$(date +%d)

cksize=50

echo "Start $(date)"

while IFS= read -r sn
do

echo "*************************************************"
echo "Begin checking NODES client: $sn"

if ping -c 1 "$sn" -i 5 > /dev/null
then
echo "$sn node up"
else
echo "$sn node down"
fi
done < server_list

Ping a host and output in CSV format in bash

you can do it using this changes -

#!/bin/bash
a=""
while IFS=',' read -r host status
do
if [ "$host" = "Host" ]; then
a="${a}${host}"','"${status}"$'\n'
else
ping -c 1 "$host"
if [ $? -eq 0 ]; then
a="${a}${host}"',Alive'$'\n'
else
a="${a}${host}"',Down'$'\n'
fi
fi
done < ip.csv
echo -e "${a}" > ip.csv

Ping a list of host names and output the results to a csv in powershell

You can use the following code instead (I simply altered the write-host calls to CSV formatting) and execute it with "PowerShell.exe script.ps > output.csv"
Note that you must execute it from the folder that contains hnames.txt, or simply change the "hnames.txt" to a full path.

$names = Get-content "hnames.txt"

foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{
Write-Host "$name,down"
}
}

P.S. You can also use the Out-File Cmdlet to create the CSV file



Related Topics



Leave a reply



Submit