Reputation: 121
I am trying to write a bash script that will take port id I retrieve from an nmap command, and then use those ports to send a file using netcat. So I got some basic knowledge using this link,
and nmap man page. As a result I came up with the following script:
#! /bin/bash
FILE="input.txt"
sudo nmap -p1-20000 -sS 192.168.122.35 -oG - | awk '$4=="Status:" && $5=="Up" {cat $FILE | nc $2 $6}'
So the issue I am having has to do with the $6. It is suppose to be the port numbers retrieved from the nmap command but it actually is nothing at all. I was wondering if you guys would be able to help me retrieve the port ids.
Upvotes: 0
Views: 549
Reputation: 189936
Awk is a separate language, you can't just plonk in external commands.
A simple fix is to use Awk's system
function:
awk -v FILE="$FILE" '$4=="Status:" && $5=="Up" {
system("nc " $2 " " $6 "<\"" FILE "\"")}'
(notice also the refactoring to avoid the useless cat
, and the assignment to make the shell's FILE
variable visible to Awk as a variable).
But perhaps it makes more sense to reroll this into a shell script with Awk as just a simple helper:
#! /bin/bash
FILE="input.txt"
sudo nmap -p1-20000 -sS 192.168.122.35 -oG - |
awk '$4=="Status:" && $5=="Up" { print $2, $6 }' |
while read host port; do
nc "$host" "$port" <"$FILE"
done
This also avoids the complex quoting inside the system
call.
Upvotes: 1