I'm writing a bash script to nmap scan for open ports, the scan those ports specifically with -A
nmap -T3 -p- $1 > "openPorts.txt" #$1 is an IP provided when calling the script
ports=$(grep "open" openPorts.txt | cut -d " " -f 1 | tr -d "/tcp" | tr '\n' ', ')
nmap -T3 -p "$ports" -A > "openPorts.txt"
however after running the script I get the print from the first nmap call in the file, then it encounters an error, the console reading:
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-02-01 16:44 EST
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.02 seconds
I'm new to scripting and even newer to bash so it's very possible it's a format thing. I looked for an answer online a bit but couldn't seem to figure out how to phrase the question so google knew what I was looking for.
-sidenote- I realize I can probably put the "$ports" declaration into the nmap scan but I couldn't figure out the syntax, and I'm not trying to make the most efficient thing in the world so it's not the end of the world to me.
What I would to to list opened ports via
nmapin 3 clean methods:I export the format to XML, then use a xpath query in
xmlstarlet.With grep:
With grep and cut, reusing most of your code:
As commented earlier, your try based on
tr -d '/tcp'can't work, becausetrdon't remove strings but characters.