Return value from nmap IP address only and pass it into a loop in Bash [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have this script of nmap which retrieves only alive IP devices from the network.



nmap -sP 192.168.1.0/24 | awk '/is up/ print up; gsub (/('


I want to get the result IP addresses from nmap to loop through the below command, as far as I understand multiple results may not be having any return value from nmap.



echo "# This script checks if a remote device is alive"
read va * This is suppose to receive the IP address one by one
echo "Checking Device "$va
if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
echo $va" is Online !"
else
echo "Cannot proceed with remote connection device "$va" is Offline !"
fi









share|improve this question















closed as unclear what you're asking by Scott, RalfFriedl, Thomas, JigglyNaga, elbarna Nov 27 at 16:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Possible duplicate of while read loop
    – JigglyNaga
    Nov 27 at 13:31














up vote
1
down vote

favorite












I have this script of nmap which retrieves only alive IP devices from the network.



nmap -sP 192.168.1.0/24 | awk '/is up/ print up; gsub (/('


I want to get the result IP addresses from nmap to loop through the below command, as far as I understand multiple results may not be having any return value from nmap.



echo "# This script checks if a remote device is alive"
read va * This is suppose to receive the IP address one by one
echo "Checking Device "$va
if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
echo $va" is Online !"
else
echo "Cannot proceed with remote connection device "$va" is Offline !"
fi









share|improve this question















closed as unclear what you're asking by Scott, RalfFriedl, Thomas, JigglyNaga, elbarna Nov 27 at 16:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Possible duplicate of while read loop
    – JigglyNaga
    Nov 27 at 13:31












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this script of nmap which retrieves only alive IP devices from the network.



nmap -sP 192.168.1.0/24 | awk '/is up/ print up; gsub (/('


I want to get the result IP addresses from nmap to loop through the below command, as far as I understand multiple results may not be having any return value from nmap.



echo "# This script checks if a remote device is alive"
read va * This is suppose to receive the IP address one by one
echo "Checking Device "$va
if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
echo $va" is Online !"
else
echo "Cannot proceed with remote connection device "$va" is Offline !"
fi









share|improve this question















I have this script of nmap which retrieves only alive IP devices from the network.



nmap -sP 192.168.1.0/24 | awk '/is up/ print up; gsub (/('


I want to get the result IP addresses from nmap to loop through the below command, as far as I understand multiple results may not be having any return value from nmap.



echo "# This script checks if a remote device is alive"
read va * This is suppose to receive the IP address one by one
echo "Checking Device "$va
if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
echo $va" is Online !"
else
echo "Cannot proceed with remote connection device "$va" is Offline !"
fi






bash scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 at 1:17









Rui F Ribeiro

38.4k1477127




38.4k1477127










asked Nov 27 at 0:16









Huud Rych

62




62




closed as unclear what you're asking by Scott, RalfFriedl, Thomas, JigglyNaga, elbarna Nov 27 at 16:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Scott, RalfFriedl, Thomas, JigglyNaga, elbarna Nov 27 at 16:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Possible duplicate of while read loop
    – JigglyNaga
    Nov 27 at 13:31












  • 1




    Possible duplicate of while read loop
    – JigglyNaga
    Nov 27 at 13:31







1




1




Possible duplicate of while read loop
– JigglyNaga
Nov 27 at 13:31




Possible duplicate of while read loop
– JigglyNaga
Nov 27 at 13:31










2 Answers
2






active

oldest

votes

















up vote
2
down vote













You can check for opened tcp port 22 with nmap also.



nmap -p 22 192.168.1.0/24 -oG - | grep -oP "Host: K[^ ]+(?=.* 22/open/tcp.*)"





share|improve this answer



























    up vote
    0
    down vote













    Welcome to unix.stackexchange !



    xargs is your friend here.



    But first some changes to your script:



    $ cat test.sh
    #!/bin/bash
    echo "# This script checks if a remote device is alive"
    va=$1 # passing it as an argument is the right thing to do here
    echo "Checking Device "$va
    if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
    echo $va" is Online !"
    else
    echo "Cannot proceed with remote connection device "$va" is Offline !"
    fi


    And now the xargs magic (% will be replaced by each address at each call) :



    $ nmap -sP 172.20.10.1-2 | awk '/is up/ print up; gsub (/(' |xargs -I % bash test.sh %
    # This script checks if a remote device is alive
    Checking Device 172.20.10.1
    Cannot proceed with remote connection device 172.20.10.1 is Offline !
    # This script checks if a remote device is alive
    Checking Device 172.20.10.2
    Connection to 172.20.10.2 port 22 [tcp/ssh] succeeded!
    172.20.10.2 is Online !


    You can also replace bash test.sh % with echo ">>>%<<<" in order to experiment what happens then.



    However, Ipor Sircer answer is shorter to implement.



    But as always with Unix: there are plenty of solutions for every problem






    share|improve this answer




















    • Hi all, appreciate the help, will check how to fit it into my scenario..
      – Huud Rych
      Nov 27 at 9:43










    • ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
      – Huud Rych
      Nov 27 at 20:29











    • Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
      – lauhub
      Nov 28 at 9:04


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You can check for opened tcp port 22 with nmap also.



    nmap -p 22 192.168.1.0/24 -oG - | grep -oP "Host: K[^ ]+(?=.* 22/open/tcp.*)"





    share|improve this answer
























      up vote
      2
      down vote













      You can check for opened tcp port 22 with nmap also.



      nmap -p 22 192.168.1.0/24 -oG - | grep -oP "Host: K[^ ]+(?=.* 22/open/tcp.*)"





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        You can check for opened tcp port 22 with nmap also.



        nmap -p 22 192.168.1.0/24 -oG - | grep -oP "Host: K[^ ]+(?=.* 22/open/tcp.*)"





        share|improve this answer












        You can check for opened tcp port 22 with nmap also.



        nmap -p 22 192.168.1.0/24 -oG - | grep -oP "Host: K[^ ]+(?=.* 22/open/tcp.*)"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 at 0:28









        Ipor Sircer

        10.3k11024




        10.3k11024






















            up vote
            0
            down vote













            Welcome to unix.stackexchange !



            xargs is your friend here.



            But first some changes to your script:



            $ cat test.sh
            #!/bin/bash
            echo "# This script checks if a remote device is alive"
            va=$1 # passing it as an argument is the right thing to do here
            echo "Checking Device "$va
            if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
            echo $va" is Online !"
            else
            echo "Cannot proceed with remote connection device "$va" is Offline !"
            fi


            And now the xargs magic (% will be replaced by each address at each call) :



            $ nmap -sP 172.20.10.1-2 | awk '/is up/ print up; gsub (/(' |xargs -I % bash test.sh %
            # This script checks if a remote device is alive
            Checking Device 172.20.10.1
            Cannot proceed with remote connection device 172.20.10.1 is Offline !
            # This script checks if a remote device is alive
            Checking Device 172.20.10.2
            Connection to 172.20.10.2 port 22 [tcp/ssh] succeeded!
            172.20.10.2 is Online !


            You can also replace bash test.sh % with echo ">>>%<<<" in order to experiment what happens then.



            However, Ipor Sircer answer is shorter to implement.



            But as always with Unix: there are plenty of solutions for every problem






            share|improve this answer




















            • Hi all, appreciate the help, will check how to fit it into my scenario..
              – Huud Rych
              Nov 27 at 9:43










            • ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
              – Huud Rych
              Nov 27 at 20:29











            • Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
              – lauhub
              Nov 28 at 9:04















            up vote
            0
            down vote













            Welcome to unix.stackexchange !



            xargs is your friend here.



            But first some changes to your script:



            $ cat test.sh
            #!/bin/bash
            echo "# This script checks if a remote device is alive"
            va=$1 # passing it as an argument is the right thing to do here
            echo "Checking Device "$va
            if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
            echo $va" is Online !"
            else
            echo "Cannot proceed with remote connection device "$va" is Offline !"
            fi


            And now the xargs magic (% will be replaced by each address at each call) :



            $ nmap -sP 172.20.10.1-2 | awk '/is up/ print up; gsub (/(' |xargs -I % bash test.sh %
            # This script checks if a remote device is alive
            Checking Device 172.20.10.1
            Cannot proceed with remote connection device 172.20.10.1 is Offline !
            # This script checks if a remote device is alive
            Checking Device 172.20.10.2
            Connection to 172.20.10.2 port 22 [tcp/ssh] succeeded!
            172.20.10.2 is Online !


            You can also replace bash test.sh % with echo ">>>%<<<" in order to experiment what happens then.



            However, Ipor Sircer answer is shorter to implement.



            But as always with Unix: there are plenty of solutions for every problem






            share|improve this answer




















            • Hi all, appreciate the help, will check how to fit it into my scenario..
              – Huud Rych
              Nov 27 at 9:43










            • ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
              – Huud Rych
              Nov 27 at 20:29











            • Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
              – lauhub
              Nov 28 at 9:04













            up vote
            0
            down vote










            up vote
            0
            down vote









            Welcome to unix.stackexchange !



            xargs is your friend here.



            But first some changes to your script:



            $ cat test.sh
            #!/bin/bash
            echo "# This script checks if a remote device is alive"
            va=$1 # passing it as an argument is the right thing to do here
            echo "Checking Device "$va
            if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
            echo $va" is Online !"
            else
            echo "Cannot proceed with remote connection device "$va" is Offline !"
            fi


            And now the xargs magic (% will be replaced by each address at each call) :



            $ nmap -sP 172.20.10.1-2 | awk '/is up/ print up; gsub (/(' |xargs -I % bash test.sh %
            # This script checks if a remote device is alive
            Checking Device 172.20.10.1
            Cannot proceed with remote connection device 172.20.10.1 is Offline !
            # This script checks if a remote device is alive
            Checking Device 172.20.10.2
            Connection to 172.20.10.2 port 22 [tcp/ssh] succeeded!
            172.20.10.2 is Online !


            You can also replace bash test.sh % with echo ">>>%<<<" in order to experiment what happens then.



            However, Ipor Sircer answer is shorter to implement.



            But as always with Unix: there are plenty of solutions for every problem






            share|improve this answer












            Welcome to unix.stackexchange !



            xargs is your friend here.



            But first some changes to your script:



            $ cat test.sh
            #!/bin/bash
            echo "# This script checks if a remote device is alive"
            va=$1 # passing it as an argument is the right thing to do here
            echo "Checking Device "$va
            if [ $(nc -z "$va" 22; echo $?) -eq 0 ]; then
            echo $va" is Online !"
            else
            echo "Cannot proceed with remote connection device "$va" is Offline !"
            fi


            And now the xargs magic (% will be replaced by each address at each call) :



            $ nmap -sP 172.20.10.1-2 | awk '/is up/ print up; gsub (/(' |xargs -I % bash test.sh %
            # This script checks if a remote device is alive
            Checking Device 172.20.10.1
            Cannot proceed with remote connection device 172.20.10.1 is Offline !
            # This script checks if a remote device is alive
            Checking Device 172.20.10.2
            Connection to 172.20.10.2 port 22 [tcp/ssh] succeeded!
            172.20.10.2 is Online !


            You can also replace bash test.sh % with echo ">>>%<<<" in order to experiment what happens then.



            However, Ipor Sircer answer is shorter to implement.



            But as always with Unix: there are plenty of solutions for every problem







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 at 0:55









            lauhub

            430616




            430616











            • Hi all, appreciate the help, will check how to fit it into my scenario..
              – Huud Rych
              Nov 27 at 9:43










            • ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
              – Huud Rych
              Nov 27 at 20:29











            • Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
              – lauhub
              Nov 28 at 9:04

















            • Hi all, appreciate the help, will check how to fit it into my scenario..
              – Huud Rych
              Nov 27 at 9:43










            • ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
              – Huud Rych
              Nov 27 at 20:29











            • Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
              – lauhub
              Nov 28 at 9:04
















            Hi all, appreciate the help, will check how to fit it into my scenario..
            – Huud Rych
            Nov 27 at 9:43




            Hi all, appreciate the help, will check how to fit it into my scenario..
            – Huud Rych
            Nov 27 at 9:43












            ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
            – Huud Rych
            Nov 27 at 20:29





            ssh root@$var 'df -h; free -m' < /dev/null > > > > > Hi, so the above code with this line results in data received from the remote host, now I want to save this data to a txt file, how can I do that..
            – Huud Rych
            Nov 27 at 20:29













            Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
            – lauhub
            Nov 28 at 9:04





            Open a new question to ask this. Comments should be used only to clarify information, not for new topics (hence they have limited formating features). And do not forget to mark the solution of your question
            – lauhub
            Nov 28 at 9:04



            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?