Check whether the port is open using bash and lsof and ss utilities

by means of lsof

( command -v lsof >/dev/null 2>&1 || ( echo "lsof not found!" && false ) ) && sudo lsof -Pni4UDP:12345

by means of ss

( command -v ss >/dev/null 2>&1 || ( echo "ss not found!" && false ) ) && ss -4Hnl -u '(sport=:12345)'

note: in both variants the return value is an empty string if the port is free and a non-empty string if the port is occupied or the corresponding utility is not found in the system

Leave a Reply