problem feeding netcat a list of hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem feeding netcat a list of hosts
# 1  
Old 08-24-2008
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[Smilieort]] [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[Smilieort]] [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[Smilieort]] [hostname] [port[s]]


Any suggestions?

Thanks,
-Sean
# 2  
Old 08-25-2008
Try this
Code:
while read host;do nc -z $host;done < nc.host

You should read the man page for your nc
# 3  
Old 08-25-2008
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.
# 4  
Old 08-25-2008
Quote:
Originally Posted by seanovision
#!/bin/bash
for i in `cat nc.host`
do
nc -v $i
done
If you read carefully man nc you will see the difference between -v and -z .Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

2. Cybersecurity

Maybe a security problem involving Linux hosts

Hello, with the following problem I feel helpless because I do not know where to look for the problem. Sorry in advance for my poor English. At some point the interaction with people on Freenode IRC has become mysterious and offending so that I checked my modem/router device. When I tried to... (2 Replies)
Discussion started by: SInt
2 Replies

3. Shell Programming and Scripting

netstat command to be executed for list of hosts

Hi All, Need to run the netstat -i command on the list of hosts and check if "Ierrs" and "Oerrs" has value greaterthan 0. for Ex: below output, driver bge1 and bge3 has Oerrs and Ierrs value > 0, So, script should report saying "Netstat status for $host, driver bge1 has Oerrs = 20, Failed"... (5 Replies)
Discussion started by: Optimus81
5 Replies

4. UNIX for Dummies Questions & Answers

List processes that are running on other hosts

Hi: How to list processes from all hosts, as opposed to the one you are working at? "ps ux" appears to list processes of the user on a single host only. Thanks. N.B Phil (5 Replies)
Discussion started by: phil518
5 Replies

5. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

6. Shell Programming and Scripting

Problem with feeding password while changing to root user

i want to change user to "root" from another user while running a script. how can i automatically feed the password? for example, i want to write a script say "script.sh"... it will first run the command "p" as mhmn user, and then it will change the user to "root" by using "su - root" command. at... (1 Reply)
Discussion started by: mhmn
1 Replies

7. Shell Programming and Scripting

awk script on a list of hosts

SERVICE NOTIFICATION: SOC;invoice-skysmart-01.net;monthly_process_check;CRITICAL;notify-by-email;CRITICAL: (0) instance(s) of (monthly-processor6) running on host (less than 1) SERVICE NOTIFICATION: SOC;invoice-02.skysmart.net;JAVA_PROCESS_CHECK;CRITICAL;notify-by-email;CRITICAL: (0) instance(s)... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. IP Networking

nc (netcat): Connection refused problem

From my machine (ubuntu), I can do $ nc somemachine 80 But, when I want to connect to other machine that runs nc on different ports, it throws "Connection refused" error. The same error occurs on my local machine. For example, nc -l 5555 nc x.x.x.x 5555 --> this throws "Connection... (2 Replies)
Discussion started by: dinga
2 Replies

9. Shell Programming and Scripting

Change .bashrc on a list of hosts

Hi, My scripting skills are somewhat basic... I need a way to log into a list of hostname/IPs as a user, su to root and then create/append root's .bashrc Thanks (0 Replies)
Discussion started by: jag7720
0 Replies

10. Shell Programming and Scripting

remote hosts access problem on solaris

hey guys, i am on a box named pluto and i need to be able to log into another box named genesis. i need to be able to ssh into genesis as root and not get asked for the password. what file do i need to edit on genesis to make this happen? i searched for the .rhosts file it doesn't seem to exist.... (1 Reply)
Discussion started by: Terrible
1 Replies
Login or Register to Ask a Question