1

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

closed as unclear what you're asking by Scott, RalfFriedl, Thomas, JigglyNaga, elbarna Nov 27 '18 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.

2

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.*)"
0

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

  • Hi all, appreciate the help, will check how to fit it into my scenario.. – Huud Rych Nov 27 '18 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 '18 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 '18 at 9:04

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