0

As you know, you cannot use the mkdir command with slashes.

I am creating an automation and what i did first, was to nmap every IP Addresses i chose, "192.168.1.0" for example, as the first argument.

then, make a folder named the first argument, which is "192.168.1.0" = $1 ==> ./script.sh 192.168.1.0

but, I want the user to put the IP Address like this: "192.168.1.0/24". How do i use the mkdir command now to still create a folder named "192.168.1.0" without /24 as the first argument still?

I tried many variations with sed commands. I hope i was clear enough to explain myself.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Oded
  • 13
  • 4
  • Does Kali use bash, or some more basic shell? If it uses bash, try `mkdir "${1%/*}"`. The `%/*` part means "remove something matching `/*` from the end of the argument". – Gordon Davisson Aug 14 '22 at 01:36
  • The linked duplicate's answers suggest quite a lot of potential solutions; that said, the one I would use is `mkdir "${1%%/*}"` – Charles Duffy Aug 14 '22 at 01:44
  • BTW, there's no need to duplicate information from your question's tagging in its title. (Also, generally speaking, tagging should be limited to things that can help put it in front of the right experts; the fact that the data came from nmap has nothing to do with how you process it for mkdir, f/e, and someone who knows nmap well will have no better ability to answer this question than someone who's never heard of it) – Charles Duffy Aug 14 '22 at 01:46

1 Answers1

0

Here's a possible solution using cut:

#!/bin/bash
dir_name=$(echo "$1" | cut -d '/' -f 1)

mkdir -v "$dir_name"

Output: example

This obviously doesn't validate the IP address, for that you could do some parsing with a regex.

mikyll98
  • 1,195
  • 3
  • 8
  • 29
  • I need the folder name stays as the IP Address itself without /24 or _24. This is a great example of how i need it to be looks like: https://i.imgur.com/5CUHS6b.png – Oded Aug 14 '22 at 01:48
  • Not sure if I have understood at this point, so you want to create a folder named from example 192.168.1.0 when you pass 192.168.1.0/24? You want to remove the "/24"? – mikyll98 Aug 14 '22 at 01:59
  • Exactly what i want to do. – Oded Aug 14 '22 at 11:46
  • @Oded I've edited the comment with a possible solution – mikyll98 Aug 14 '22 at 13:42