简体   繁体   中英

How do I make a bash alias with IP subnet variable for easier nmap scanning?

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 (eg 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.

It's just a function.

qmap() {
   nmap -sn "192.168.$1.1/24"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM