Nmap port scanning array - arrays

I am doing a nmap bash script, and I am just wondering if there is any possibility to use array list for my port commands. For example:
port=[23,45,75,65]
for i in 21 do
nmap -p x,y 192.168.1.$i
done
e.g. At the x,y place I want to use the number 23,45

I'm not sure if that's what you want, but you can try this:
ports="23,45,75,65"
for i in 21 do
nmap -p "$ports" 192.168.1.$i
done
You can also do:
ports="23,45,75,65"
targets="1-25"
nmap -p "$ports" "192.168.1.$targets"

Scanning an array of ports is already built in to nmap. See http://nmap.org/book/man-port-specification.html for more details on the syntax, but here's an excerpt that may give you what you need:
For example, the argument -p U:53,111,137,T:21-25,80,139,8080 would scan UDP ports 53, 111,and 137, as well as the listed TCP ports.

Related

Bash - ssh'ing to hosts in associative array

I am writing a script that stores client connection data such as session ID, server, and port in an associative array. I need to ssh onto these hosts, and run an lsof command to find what process is using this port.
declare -A HOSTMAP
HOSTMAP[session]=$session_ids
HOSTMAP[cname]=$cnames
HOSTMAP[port]=$ports
When printed, data in the array is displayed as so (host names and ports have been changed to protect the innocent)
for g in "${!HOSTMAP[#]}"
do
printf "[%s]=%s\n" "$g" "${HOSTMAP[$g]}"
done
[cname]=hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7
hostname8
[session]=44
5
3
9
14
71
65
47
[port]=11111
22222
33333
44444
55555
66666
77777
88888
I would like to do an operation akin to the following:
for session in $session_id
do
echo "Discovering application mapped to session ${session} on ${cname}:${port}"
ssh -tq ${cname} "lsof -Tcp | grep ${port}"
done
Many thanks in advance for advising on an elegant solution
bash doesn't allow nesting of arrays. Here, I would just use separate indexed arrays. However, since your session appears to be an integers, and indexed arrays are sparse, you can use the session id as the index for the cnames and the ports.
cnames=([44]=hostname1 [5]=hostname2 [3]=hostname3)
ports=([44]=1111 [5]=2222 [3]=3333)
for session in "${!cnames[#]}"; do
cname=${cnames[$session]}
port=${ports[$session]}
echo "Discovering application mapped to session ${session} on ${cname}:${port}"
ssh -tq ${cname} "lsof -Tcp | grep ${port}"
done
You can assign the output of ssh to a variable
result=$(ssh -tq ${cname} "lsof -Tcp | grep ${port}")
Then you can extract the data you want from $result.

nmap output Failed to open normal output file n for writing

when I execute using zenmap command, I get the error, Failed to open normal output file n for writing QUITTING!
How can I write the scan result to a file?
nmap -oN n "c:\\temp\\scan1.txt" 192.168.1.2
According to this quote from the manual:
OUTPUT:
-oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
and Grepable format, respectively, to the given filename.
It appears that the file designation should be placed immediately following the oN (ie, without the intervening n).
Make sure your directory and folder names are correct. This happens when you try to save the output file and path you gave does not exist or match.
try to first go to the directory on command line, then run the command with just name of the text like in the following;
nmap -oN scan.txt scanme.nmap.org
Just place a " - " before writing an target IP address
command:-sudo nmap -oG 192.168.18.0/24 -vv
result:-Failed to open machine output file 192.168.18.0/24 for writing
QUITTING!
after placing " - " before writing IP address
command:-sudo nmap -oG - 192.168.18.0/24 -vv
result:-# Nmap 7.91 scan initiated Wed Aug 25 15:22:58 2021 as: nmap -oG - -vv 192.168.18.0/24
#Ports scanned: TCP(1000;1,3-4,6-7,9,13,17,19-26,30,32-33,37,42-43,49,53,70,79-85,88-90,99-100,106,109-111,113,119,125,135,139,143-144,146,161,163,179,199,211-212,222,254-256,259,264,280,301,306,311,340,366,389,406-407,416-417,425,427,443-445,458,464-465,481,497,500,512-515,524,541,543-545,548,554-555,563,587,593,616-617,625,631,636,646,648,666-668,683,687,691,700,705,711,714,720,722,726,749,765,777,783,787,800-801,808,843,873,880,888,898,900-903,911-912,981,987,990,992-993,995,999-1002,1007,1009-1011,1021-1100,1102,1104-1108,1110-1114,1117,1119,1121.............................................................................................................................

Create and iterate through a list of numbers in Bash

I am trying to use cURL on about 200 select ports, and I would prefer to not have to do each one at the same time. I am also trying to learn the basics of Bash.
What I am trying to do is create a list of numbers and then iterate through each of those numbers. Here is what I have:
Ports={1,5,7,10,12}
for port in $Ports
do
$echo "Port $port"
curl "URL:$port"
done
Is this possible to do or am I thinking too high level? Thank you!
Try this:
#!/bin/bash
Ports="1 5 7 10 12"
for port in $Ports; do
echo "Port $port"
curl "URL:$port"
done

Findstr in windows bat

I use the following command
netstat -ano -p tcp | findstr "18812"
and got result like this:
TCP 0.0.0.0:18812 0.0.0.0:0 LISTENING 3116
TCP 127.0.0.1:3099 127.0.0.1:18812 ESTABLISHED 5112
TCP 127.0.0.1:18812 127.0.0.1:3099 ESTABLISHED 3116
But the line 2 is not what I want,i.e: I want the lines which the source port is 18812, rather than the dst port .
Any body knows how to deal with this? Thanks very much!
Edit: I tried regular expression, but "\d","+","\s" seems not work
One small improvement to the solution in your comment to fedmich. Probably not needed for port 18812 since the max number of digits is 5, and you've used all of them. But if you are looking for a 4 digit port (or smaller), you will want to make sure there is a space after the port number.
For example, if you were looking for source port 3099
netstat -ano -p tcp | findstr /R /C:"TCP[ ]*[0-9.]*:3099 "
Are you sure you are using regular expression on the first and last part?
".*STRING.*"
Try this out.
netstat -ano -p tcp | findstr ".*:18812.*"

Stream a continuously growing file over tcp/ip

I have a project I'm working on, where a piece of Hardware is producing output that is continuously being written into a textfile.
What I need to do is to stream that file as it's being written over a simple tcp/ip connection.
I'm currently trying to that through simple netcat, but netcat only sends the part of the file that is written at the time of execution. It doesn't continue to send the rest.
Right now I have a server listening to netcat on port 9000 (simply for test-purposes):
netcat -l 9000
And the send command is:
netcat localhost 9000 < c:\OUTPUTFILE
So in my understanding netcat should actually be streaming the file, but it simply stops once everything that existed at the beginning of the execution has been sent. It doesn't kill the connection, but simply stops sending new data.
How do I get it to stream the data continuously?
Try:
tail -F /path/to/file | netcat localhost 9000
try:
tail /var/log/mail.log -f | nc -C xxx.xxx.xxx.xxx 9000
try nc:
# tail for get last text from file, then find the lines that has TEXT and then stream
# see documentation for nc, -l means create server, -k means not close when client disconnect, waits for anothers clients
tail -f /output.log | grep "TEXT" | nc -l -k 2000

Resources