LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2012, 09:00 PM   #1
jase4867
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
Can't get netcat port scan to output to a file


Hi all,

Just starting to learn some bash scripting, and I'm having trouble understanding how the redirection operators work. I wrote a very simple bash script, where I run a netcat port scan against a text file, which contains a list of hosts (not very practical, but just messing around with it), and I can't get it to send the output to a text file. The text file itself gets created, but there is no data in it. Here's what my script looks like:

#!/bin/bash

for ip in $(cat ips.txt); do
nc -vv -z $ip 1-100 > results.txt
done


I'm sure it's a simple fix, but something isn't clicking in my head. Can anyone offer some pointers?

Thanks!
 
Old 07-08-2012, 09:11 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Do you get any output with
Code:
nc -vv -z <someip> 1-100
Lets check its working; I used my local IP addr to test and got plenty of output.
I noticed that if you re-direct to a file, only successful cxns ie open ports are written to stdout; failures go to stderr.
To collect even those
Code:
nc -vv -z <someip> 1-100>nc.out 2>&1
Try just that to test with...
 
Old 07-08-2012, 09:49 PM   #3
jase4867
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi Chris,

The second example you gave worked like a charm! I'll give it a try with the ips.txt file, and see what happens there.

Thanks again!

Jason
 
Old 07-08-2012, 11:13 PM   #4
jase4867
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
So, the script is still a bit flaky acting. While it was running against the list of IP's, it would update the nc.out file, but then it would zero itself out, and start over. When the script finished, the only data that was in the nc.out file, was the last host that was scanned.

Is there a better way to write this script, than what I have?
 
Old 07-09-2012, 01:55 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Indeed, you just need to learn some bash operators
Code:
# this outputs to (overwrites) a file
prog > file

# this appends to a file
prog >> file

# this takes in put from(!) a file
prog < file
Think carefully about what your loop is doing and/or try
Code:
#!/bin/bash
set -xv
The latter shows what the parser is doing...

some useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 07-09-2012, 10:40 PM   #6
jase4867
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Making the last change on the script, fixed it, and gave me the output I was looking for. Changed the redirect from > to >>.

Thanks again for the help, and the resource links!

Jason
 
Old 07-10-2012, 12:31 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
No worries mate
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using BusyBox version of netcat for listening tcp port A-Rap Linux - Software 4 03-15-2012 05:27 PM
send output of namp scan to CSV file rsmccain Linux - Networking 11 04-18-2009 09:31 PM
Nessus scan and no port scan possible? memo007 Linux - Security 1 09-08-2008 07:21 PM
best port scanner To scan open port in a network tanveer Linux - Security 8 01-21-2007 09:19 PM
netcat slow scan noir911 Linux - Security 4 06-28-2006 04:04 AM


All times are GMT -5. The time now is 02:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration