Skip to content

Commit 8ba0d0c

Browse files
julianoesJonasVautherin
authored andcommitted
Enable passing hostnames instead of IPs
We can use `getent hosts` and `awk` to figure out the IP from a hostname. This is helpful when using the docker image as part of a docker compose setup with bridge network instead of host network. Signed-off-by: Julian Oes <[email protected]>
1 parent 079dc85 commit 8ba0d0c

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

entrypoint.sh

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function show_help {
44
echo ""
5-
echo "Usage: ${0} [-h | -v VEHICLE | -w WORLD] [IP_API | IP_QGC IP_API]"
5+
echo "Usage: ${0} [-h | -v VEHICLE | -w WORLD] [HOST_API | HOST_QGC HOST_API]"
66
echo ""
77
echo "Run a headless px4-gazebo simulation in a docker container. The"
88
echo "available vehicles and worlds are the ones available in PX4"
@@ -12,12 +12,24 @@ function show_help {
1212
echo " -v Set the vehicle (default: iris)"
1313
echo " -w Set the world (default: empty)"
1414
echo ""
15-
echo " <IP_API> is the IP to which PX4 will send MAVLink on UDP port 14540"
16-
echo " <IP_QGC> is the IP to which PX4 will send MAVLink on UDP port 14550"
15+
echo " <HOST_API> is the host or IP to which PX4 will send MAVLink on UDP port 14540"
16+
echo " <HOST_QGC> is the host or IP to which PX4 will send MAVLink on UDP port 14550"
1717
echo ""
1818
echo "By default, MAVLink is sent to the host."
1919
}
2020

21+
function get_ip {
22+
output=$(getent hosts "$1" | awk '{print $1}')
23+
if [ -z $output ];
24+
then
25+
# No output, assume IP
26+
echo $1
27+
else
28+
# Got IP, use it
29+
echo $output
30+
fi
31+
}
32+
2133
OPTIND=1 # Reset in case getopts has been used previously in the shell.
2234

2335
vehicle=iris
@@ -38,22 +50,12 @@ done
3850

3951
shift $((OPTIND-1))
4052

41-
# All the leftover arguments are supposed to be IPs
42-
for arg in "$@"
43-
do
44-
if ! [[ ${arg} =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
45-
echo "Error: invalid IP: ${arg}!"
46-
echo ""
47-
show_help
48-
exit 1
49-
fi
50-
done
5153

5254
if [ "$#" -eq 1 ]; then
53-
IP_QGC="$1"
55+
IP_QGC=$(get_ip "$1")
5456
elif [ "$#" -eq 2 ]; then
55-
IP_API="$1"
56-
IP_QGC="$2"
57+
IP_API=$(get_ip "$1")
58+
IP_QGC=$(get_ip "$2")
5759
elif [ "$#" -gt 2 ]; then
5860
show_help
5961
exit 1;

0 commit comments

Comments
 (0)