2 ms·
Kind of unrelated but here's a little bit of bash I use for checking this locally: # Check if the port is available is_port_free() { if [[ "$1" =~ ^[0-
by keepamovin 3y ago
Kind of unrelated but here's a little bit of bash I use for checking this locally:
# Check if the port is available
is_port_free() {
if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -ge 1024 ] && [ "$1" -le 65535 ]; then
echo "Valid port number." >&2
else
echo "Invalid port number." >&2
return 1
fi
if netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ":'$1'$" {exit 1}'; then
# Return a truthy value if the port is available
return 0
else
# Return a falsey value if the port is in use
return 1
fi
}
- another2another 3y agonetstat seems to be disappearing from some linux distros - pretty sure it's not on my current Ubuntu by default, so I have to use 'ss' instead (which does seem to be quicker).
- keepamovin 3y agoCool, thanks for the tip! Yeah, I noticed I had to install netstat some times. Maybe I'll update my scripts to use ss instead. Do you have the conversion? I guess I could ask ChatGPT, but...while you're here? :) heh