Skip to content

Instantly share code, notes, and snippets.

@bgromov
Created March 6, 2018 23:44
Show Gist options
  • Save bgromov/80e250dc09cf2825618d64bb82a19cfa to your computer and use it in GitHub Desktop.
Save bgromov/80e250dc09cf2825618d64bb82a19cfa to your computer and use it in GitHub Desktop.
ROS environment with IP from the main network interface
#!/bin/bash
NET_IF=`netstat -rn | awk '/^0.0.0.0/ {thif=substr($0,74,10); print thif;} /^default.*UG/ {thif=substr($0,65,10); print thif;}'`
PRIMARY_NET_IF=($NET_IF)
export ROS_IP=`ifconfig ${PRIMARY_NET_IF[0]} | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'`
if [ -z ${ROS_IP} ]
then
export ROS_IP=127.0.0.1
fi
echo 'ROS_IP='${ROS_IP}
export ROS_HOSTNAME=${ROS_IP}
echo 'ROS_HOSTNAME='${ROS_HOSTNAME}
if [ -z $1 ]
then
echo 'I am the master!'
ROS_MASTER_IP=${ROS_IP}
else
echo 'Setting ROS master IP to '$1
ROS_MASTER_IP=${1}
fi
export ROS_MASTER_URI=http://${ROS_MASTER_IP}:11311
echo 'ROS_MASTER_URI='${ROS_MASTER_URI}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment