Skip to content

Commit 805304f

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add a function to get an available random port"
2 parents d9d10df + 309b99e commit 805304f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

functions

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,24 @@ function set_systemd_override {
732732
sudo systemctl daemon-reload
733733
}
734734

735+
# Get a random port from the local port range
736+
#
737+
# This function returns an available port in the local port range. The search
738+
# order is not truly random, but should be considered a random value by the
739+
# user because it depends on the state of your local system.
740+
function get_random_port {
741+
read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
742+
while true; do
743+
for (( port = upper_port ; port >= lower_port ; port-- )); do
744+
sudo lsof -i ":$port" &> /dev/null
745+
if [[ $? > 0 ]] ; then
746+
break 2
747+
fi
748+
done
749+
done
750+
echo $port
751+
}
752+
735753

736754
# Restore xtrace
737755
$_XTRACE_FUNCTIONS

0 commit comments

Comments
 (0)