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











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 (/(|)/,""); up = $NF}'


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









New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold 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 (/(|)/,""); up = $NF}'


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









New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold 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 (/(|)/,""); up = $NF}'


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









New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 (/(|)/,""); up = $NF}'


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









New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 27 at 1:17









Rui F Ribeiro

38.3k1476127




38.3k1476127






New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 27 at 0:16









Huud Rych

62




62




New contributor




Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Huud Rych is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold 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.






put on hold 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 (/(|)/,""); up = $NF}' |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
      2 days ago




















    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 (/(|)/,""); up = $NF}' |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
              2 days ago

















            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 (/(|)/,""); up = $NF}' |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
              2 days ago















            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 (/(|)/,""); up = $NF}' |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 (/(|)/,""); up = $NF}' |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
              2 days ago




















            • 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
              2 days ago


















            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
            2 days ago






            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
            2 days ago





            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio