-1

I am trying to ping a range of IP addresses using Bash and generate a CSV file with the results.

My current code is as follows:

CSV_FILE="ping_results.csv"
echo "IP Address,Status" > $CSV_FILE

for i in {1..254}; do (host 10.184.2.$i | grep "name pointer" >/dev/null && echo "10.184.2.$i;Ping_Yes;$(host 10.184.2.$i | awk '{print $5}' | tr -d '.')" >> ping_results.csv || ping 10.184.2.$i -c 1 -w 5 >/dev/null && echo "10.184.2.$i;Ping_Yes;" >> ping_results.csv) &
done

echo "Ping-Ergebnisse wurden in ping_results.csv gespeichert."

The issue I'm facing is that sometimes the code reports that an IP address is pingable, but when I try to ping it manually, it's not actually reachable. I need help understanding why this is happening and how I can improve the code to ensure that the CSV file only contains accurate information about which IP addresses are actually pingable.

Ideally, the CSV file should contain the following information for each IP address:

  • IP Address
  • Status (Ping_Yes or Ping_No)
  • Hostname

My goal is to have a reliable and accurate CSV file that correctly identifies the pingable and non-pingable IP addresses within the specified range. How can I achieve this?


Here is the above code written more legibly:

CSV_FILE="ping_results.csv"
echo "IP Address,Status" > $CSV_FILE

for i in {1..254}; do
    (
        host 10.184.2.$i |
        grep "name pointer" >/dev/null &&
        echo "10.184.2.$i;Ping_Yes;$(host 10.184.2.$i | awk '{print $5}' | tr -d '.')" >> ping_results.csv ||
        ping 10.184.2.$i -c 1 -w 5 >/dev/null &&
        echo "10.184.2.$i;Ping_Yes;" >> ping_results.csv
    ) &
done

echo "Ping-Ergebnisse wurden in ping_results.csv gespeichert."
8
  • 1
    What does "Disclaimer CopyBash" mean?
    – mkrieger1
    Commented 2 days ago
  • Sry i send ist a frind to loock over beacause of my gramar dont know think it is kinda messed up
    – georg
    Commented 2 days ago
  • Why did you resolv hosts names if you store only IP in CSV and you build your loop from IP Address? Commented 2 days ago
  • 1
    Please don't cram most of your code onto 1 line when asking other people to try to understand it. I added a more legible version of your code to the end of your question now.
    – Ed Morton
    Commented 2 days ago

1 Answer 1

2

How I will parallelize multi scan

... And multiple host names resolution.

Note: As you stand for

  • IP Address
  • Status (Ping_Yes or Ping_No)
  • Hostname

My CSV will contain 3 columns: IP Address,Status,Hostname.

csvFile="ping_results.csv"
baseIP=10.184.2

. <(
    for i in {1..254};do
        host $baseIP.$i &
    done |
    sed -ne 's/^\([0-9]\+\)\.[0-9]\+\.[0-9]\+\.[0-9]\+\.in.*pointer \(.*\)\./hosts[\1]=\2/p'
)

. <(
    for i in {1..254}; do
        if ping -nW5 -c1 $baseIP.$i &>/dev/null; then
            echo ping[$i]=Ping_Yes
        else
            echo ping[$i]=Ping_No
        fi &
    done
)

{
    echo IP Address,Status,Hostname
    for i in ${!ping[@]}; do
        echo $baseIP.$i,${ping[i]},${hosts[i]:-unknown}
    done
} >$csvFile
7
  • do you know how to grap also the ipv6?
    – georg
    Commented yesterday
  • @georg Try nmap -sP instead! Commented yesterday
  • @georg Anyway YOUR syntax in not only limited to IPV4, but to 24 bits netmask! Try (and read) f-hauri.ch/vrac/netfunc.bash.txt Commented yesterday
  • but this is only ip v4 i need ipv6 but i have the problem that i need the hostname and then need the host comand over the host name so it shows me the ip v6
    – georg
    Commented yesterday
  • Your question is clearly based on small subnet of fixed 24 bits netmask. My answer is based on your question. My bash netfunc lib do offer full IPV4 support, masklen computation and so. If your have another problem, please post another question, more focused, with as many details you could! Commented yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.