-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetConfig
More file actions
executable file
·197 lines (179 loc) · 4.38 KB
/
netConfig
File metadata and controls
executable file
·197 lines (179 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
# Shell script scripts to set ROS network configuration
# ----------------------------------------------------------------
# Copyright(c)2016-2017 Chaoyang Liu <http://chaoyangliu.cc>
# This script is licensed under GNU GPL version 3 or above
# ----------------------------------------------------------------
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-r] [-n HOSTNAME] [-rm]...
Complete ROS net configuration simply and tidily.
-h display this help and exit
-n hostname set the hostname of ros master
-r use remote mode, used to communicate in multiply machines via ros network
-l use local mode, not communicating with other machines
-m set current computer as ros master
-d display current configuration
EOF
}
# Set ros master
set_ros_master() {
master_hn=$1.local
master_uri=http://$master_hn:11311
sed -i "s%\(^export\ ROS_MASTER_URI=\).*%\1$master_uri%" ./config/rosNetCfg
#display_remote_master_ip $master_hn
}
# Set ros ip
# Inspired by this question from stackoverflow:
# http://stackoverflow.com/questions/21336126/linux-bash-script-to-extract-ip-address
set_ros_ip() {
ip_add=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}')
sed -i "s/\(^export\ ROS_IP=\).*/\1$ip_add/" ./config/rosNetCfg
}
# display remote ros master ip address from hostname
# Inspired by this question from stackoverflow:
# https://serverfault.com/questions/170706/easy-way-to-get-ip-address-from-hostname-using-a-unix-shell?newreg=36e1160986704b8eb8e16524aa0381af
display_remote_master_ip() {
master_ip="`gethostip -d "$1"`"
if [ -z "$master_ip" ]
then
echo "Can not detect remote master, please check if remote is started."
else
echo "The IP address of ros master is: $master_ip"
fi
}
# Set ros hostname
set_ros_hostname() {
hn=$(hostname).local
sed -i "s/\(^export\ ROS_HOSTNAME=\).*/\1$hn/" ./config/rosNetCfg
}
display_config() {
echo "The current configuration is: "
cat ./config/rosNetCfg | while read line
do
echo $line
done
}
getopt --test > /dev/null
if [[ $? -ne 4 ]]
then
echo "Error:`getopt --test` failed in this environment."
exit 1
fi
#---------------------------------------------
# d | h | l | m | n | r
# 0 | 1 | 2 | 3 | 4 | 5
#---------------------------------------------
# Options of this tool
SHORT=dfhlmn:r
LONG=force,local,remote,master,display,name:,help
# Use getopt tool to parse options from users
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? -ne 0 ]]
then
show_help
exit 2
fi
eval set -- "$PARSED"
# Init flag as 0
flag=$(expr 0)
# Process the options from users
# Inspired by this question from stackoverflow:
# http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146
while true; do
case "$1" in
-d|--display)
flag=$(($flag + 1))
shift
;;
-h|--help)
flag=$(($flag + 2))
shift
;;
-l|--local)
flag=$(($flag + 2 * 2))
shift
;;
-m|--master)
flag=$(($flag + 2 * 2 * 2))
shift
;;
-r|--remote)
flag=$(($flag + 2 * 2 * 2 * 2 * 2))
shift
;;
-n|--name)
flag=$(($flag + 2 * 2 * 2 * 2))
name="$2"
shift 2
;;
--) # End of all options
shift
break
;;
*)
exit 3
;;
esac
done
case $(expr $flag) in
1)
# option:-d
display_config
;;
2)
# option:-h
show_help
;;
4)
# option:-l
set_ros_master $(hostname)
set_ros_ip
set_ros_hostname
;;
8)
# option:-m
set_ros_master $(hostname)
;;
16)
# option:-n
set_ros_master $name
display_remote_master_ip $name.local
;;
32)
# option:-r
while true; do
read -p "Do you wish to set this computer as ros master?" yn
case $yn in
[Yy]* )
set_ros_master $(hostname)
break
;;
[Nn]* )
read -p "Please enter the name of ros master: " masterName
set_ros_master $masterName
break
;;
* ) echo "Please answer yes or no."
;;
esac
done
;;
40)
# options:-rm
set_ros_master $(hostname)
set_ros_ip
set_ros_hostname
;;
48)
# options:-rn name
set_ros_master $name
set_ros_ip
set_ros_hostname
display_remote_master_ip $name.local
;;
*)
;;
esac
exit 0