-1

Been poking around for a bit trying to find a straight answer but VScode keeps giving me grief when checking the syntax and the alias doesn't seem to work.

I often switch between networks with devices that aren't easily discoverable and their IP addresses often change. I've found myself using nmap to quickly scan the network devices for the ports I'm looking for (e.g. 22 open ssh when working with a pi).

For example…

qmap11='nmap -sn 192.168.1.1/24'
qmap21='nmap -sn 192.168.2.1/24'
qmap31='nmap -sn 192.168.3.1/24'

As you can imagine this isn't the cleanest way to do this, and I want to run something like the following

$ qmap 3
# to run nmap -sn 192.168.3.1/24

Or…

$ qmap ip
# to run something like nmap -sn [whatever subnet I'm on]
# so I don't have to first check what the network is

I've been tinkering with building a function… but want to figure this out first.

Other Posts:

Alias with variable in bash

Linux bash script to extract IP address

Which terminal command to get just IP address and nothing else?

How to detect the OS from a Bash script?

Make a Bash alias that takes a parameter?

Theser other posts definitely hint at what I'm tryingn to figure out.

JWhiteUX
  • 47
  • 9
  • 1
    how do you get from `[qmap] 3` to `192.168.3.1/24`? sure, I could guess `192.168.?.1/24` but then how do you get to `100 192.168.1.1/24` from `qmap ??` ? how do you determine `whatever subnet I'm on`? what code have you tried so far? [please update the question with these additional details] – markp-fuso Dec 29 '21 at 19:46
  • 3
    I suggest to use a function. – Cyrus Dec 29 '21 at 19:52
  • @markp-fuso - The `qmap 3` is what I'm trying to build. The prior examples are above. e.g. `qmap31='nmap -sn 192.168.3.1/24'` - Note that this is an alias, with a hardcoded IP address of `192.168.3.1/24` - Or something like `qmap -s 3` to replace the x in `192.168.X.1/24` – JWhiteUX Dec 29 '21 at 22:47
  • You can't aliases for this. You **must** use a function instead. Aliases can only do prefix substitution and nothing else. – Charles Duffy Dec 29 '21 at 23:32
  • Please don't turn the question into an answer. Your question should remain strictly a question. I have rolled back your recent edits. – tripleee Dec 30 '21 at 07:42

1 Answers1

2

It's just a function.

qmap() {
   nmap -sn "192.168.$1.1/24"
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111