1

I’m using nmap for network reporting with something like the below example:

nmap 192.168.1.0/24 -oG filename_$(date +'%d-%m-%Y-%T').txt

How do I add the network scanned into the name of the file created? So for example, the file created looks like 192.168.1.0/24_07-01-2022.txt

Thanks!

  • 3
    / is one of only two characters that cannot appear in a file name. \0 is the other. You cannot create the file as hoped for. You can run NET=192.168.1.0 nmap ${NET}/24 -oG f${NET}_$(date %d-%m-%Y-%T).txt – doneal24 Apr 07 '22 at 15:50
  • 2
    Strictly speaking, you can create the file as shown, but that would be one file and one directory: 192.168.1.0 would be a directory and 24_07-01-2022.txt a file in that directory. I suspect that isn't what you want. – terdon Apr 07 '22 at 15:57

1 Answers1

1

as pointed out by @doneal24, / is one of the characters that cannot appear in a filename in an unix filesystems. See there.

You can use variable substitution for swapping / with _ (or with any other character you like except / and \000 (null character)):

net=192.168.1.0/24 && nmap "$net" -oG "${net/\//_}_$(date +'%d-%m-%Y-%T')"

Later when you need to pick back the networks scanned just replace the _ (or anything else) with / to get the network + netmask.

terdon
  • 242,166
  • 1
    Also note that nmap "$net" -oG "${net/\//_}_$(date +'%d-%m-%Y-%T')" wouldn't work on systems where option processing is done the standard way (or if $POSIXLY_CORRECT is in the environment on GNU systems at least). More generally you want to have options before non-option arguments. So nmap -oG "${net/\//_}_$(date +'%d-%m-%Y-%T')" -- "$net" here. – Stéphane Chazelas Apr 07 '22 at 18:24
  • Yep @StéphaneChazelas but i have reused the code given in the OP – DanieleGrassini Apr 07 '22 at 19:26