Reputation: 442
I'm doing a script that uses nmap
to get all the devices on a ip address range and their mac address. I want to create a file for each device, like IPaddress_macaddress.
I did that to get the values but I don't know how I can dynamically create files.
sudo nmap -n -sP 192.168.1.* |
awk '/Nmap scan report/{printf $5;printf " ";getline;getline;printf $3;}' > file.txt
This prints on file.txt :
192.168.1.10 DC:EX:03:0S:4B:31
192.168.1.11 A4:2G:8C:E8:5A:65
192.168.1.32 9C:80:GF:J0:53:6F
192.168.1.23 64:7C:54:CC:SD:C4
192.168.1.77 256
The format of this file is
ipaddress macaddress
I want to parse that file to create for each line a new file with the name of the content of each line, adding an underscore between the ipaddress and the macaddress. To correspond to ipaddress_macaddress.txt
So, for that file.txt, with a script, I want it to create 192.168.1.10_DC:EX:03:0S:4B:31.txt etc
I don't know how can I parse it intelligently
Upvotes: 0
Views: 398
Reputation: 12877
As an awk onliner you could do:
nmap -n -sP 192.168.1.* | awk '{ if ($0 ~ /Nmap scan/) { ip=$5 } if ($0 ~ /MAC/) { mac=$3;det[mac]=ip } } END { for ( i in det ) { system("echo \""i" "det[i]"\" > "i"_"det[i]".txt") } }'
This will take the output from nmap and then search for "Nmap scan" and place the ip address in the variable ip and then "MAC" placing the mac address in mac. An array is then created with the ip address and the MAC address. This is then looped through and the awk system function is utilised to create the files with the ip and MAC addresses.
Upvotes: 1
Reputation: 10865
$ ls
file
$ while read ip mac; do touch ${ip}_${mac}.txt; done < file
$ ls
192.168.1.10_DC:EX:03:0S:4B:31.txt 192.168.1.23_64:7C:54:CC:SD:C4.txt 192.168.1.77_256.txt
192.168.1.11_A4:2G:8C:E8:5A:65.txt 192.168.1.32_9C:80:GF:J0:53:6F.txt file
Upvotes: 1