problem feeding netcat a list of hosts

Hi, I'm having difficulty in making a bash script to get netcat to scan a list of hosts and their ports from another file and could use some help. Here's an example host list, "nc.host":

192.168.2.110 22

And here's the first script I tried to feed "nc.host" into netcat:

"nc1.sh"

#!/bin/bash
for i in `cat nc.host`
do
nc -v $i
done

..... looks simple enough, but here's the error I get when I run the script:

[root@Dagobah test1]# ./nc1.sh
usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]
[-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [hostname] [port[s]]
usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]
[-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [hostname] [port[s]]

I got two usage errors right in a row. It's as if netcat is receiving the IP address and the port number as two separate arguments, and it doesn't know how to process the command "nc -v 192.168.2.110" or "nc -v 22". (Weird, I didn't tell the script to add an end- of- line to the IP address???) Maybe it's the single space between the IP and port in the nc.hosts file?

So I altered the script in the hopes of getting around the whitespace problem:

"nc2.sh"

#!/bin/bash
for i in `awk '{print $1" "$2}' nc.hosts`
do
nc -v $i
done

.. but I get the same two netcat ussage errors right in a row again!

I tried a shortcut way and it still fails:

[root@Dagobah test1]# awk '{print $1" "$2}' nc.host
192.168.2.110 22 #### it looks like it outputs right! =/

[root@Dagobah test1]# awk '{print $1" "$2}' nc.host | nc -v
usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]
[-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [hostname] [port[s]]

Any suggestions?

Thanks,
-Sean

Try this

while read host;do nc -z $host;done < nc.host

You should read the man page for your nc

OK, I tried the while/do and it worked, thank you very much. Now I have something new to work with.

FYI, I did read the man page (and other sources) before posting. Looking at the man page again I still don't see where it explains what I was looking for. The closest is the DATA TRANSFER section, but maybe barely hints at it.

At any rate, thank you for the help.

If you read carefully man nc you will see the difference between -v and -z .:wink: