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[
ort]] [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[
ort]] [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[
ort]] [hostname] [port[s]]
Any suggestions?
Thanks,
-Sean