mirror of
https://github.com/DictXiong/dotfiles.git
synced 2024-11-24 06:37:01 +08:00
common.sh: is_port_free and get_free_port
This commit is contained in:
parent
fb43df06f4
commit
2ffc0e38f4
|
@ -233,6 +233,31 @@ get_os_name()
|
|||
echo $ans
|
||||
}
|
||||
|
||||
is_port_free() {
|
||||
( echo $1 | grep -qxE "[1-9][0-9]{0,4}" ) || false
|
||||
local cmd
|
||||
case $(get_os_type) in
|
||||
macos ) cmd="netstat -van | grep -q \".$1\"" ;;
|
||||
cygwin|msys ) cmd="netstat -ano | grep -q \":$1\"" ;;
|
||||
*) cmd="netstat -tuanp | grep -q \":$1\"" ;;
|
||||
esac
|
||||
if eval $cmd; then
|
||||
return 2
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
get_free_port() {
|
||||
while
|
||||
local port=$(shuf -n 1 -i 49152-65535)
|
||||
! is_port_free $port
|
||||
do
|
||||
continue
|
||||
done
|
||||
echo $port
|
||||
}
|
||||
|
||||
# if bash-ed, else source-d
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
$1 "${@:2}"
|
||||
|
|
Loading…
Reference in New Issue
Block a user