浏览代码

Install batman script for client

Bob Mottram 10 年前
父节点
当前提交
e3b56db91a
共有 1 个文件被更改,包括 115 次插入70 次删除
  1. 115
    70
      src/freedombone-client

+ 115
- 70
src/freedombone-client 查看文件

@@ -31,9 +31,11 @@ CURR_USER=$USER
31 31
 # Version number of this script
32 32
 VERSION="1.01"
33 33
 
34
-# mesh networking settings
35
-BRIDGE_BATMAN_IPV6=
36
-PEER_BATMAN_IPV6=
34
+WIFI_CHANNEL=2
35
+
36
+# B.A.T.M.A.N settings
37
+BATMAN_CELLID='02:BA:00:00:03:01'
38
+ESSID='mesh'
37 39
 
38 40
 # ssh (from https://stribika.github.io/2015/01/04/secure-secure-shell.html)
39 41
 SSH_CIPHERS="chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr"
@@ -84,69 +86,116 @@ function configure_ssh_client {
84 86
 }
85 87
 
86 88
 function mesh_batman {
87
-  if [ ! $BRIDGE_BATMAN_IPV6 ]; then
88
-      return
89
-  fi
90
-  sudo apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl
91
-  sudo apt-get -y install python-dev libevent-dev ebtables python-pip git
92
-
93
-  sudo modprobe batman-adv
94
-  [ $? -ne 0 ] && echo "B.A.T.M.A.N module not available" && exit 76482
95
-  if ! grep -q "batman_adv" /etc/modules; then
96
-      sudo echo 'batman_adv' >> /etc/modules
97
-  fi
98
-
99
-  # If no address has been given then create a ramdom one
100
-  if [ ! $PEER_BATMAN_IPV6 ]; then
101
-      hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
102
-      a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
103
-      b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
104
-      c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
105
-      d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
106
-      PEER_BATMAN_IPV6="$IPV6_NETWORK:$a:$b:$c:$d"
107
-  fi
108
-
109
-  sudo cp /etc/network/interfaces ~/interfaces
110
-  if ! grep -q "# Mesh Networking (B.A.T.M.A.N)" ~/interfaces; then
111
-      echo '' >> ~/interfaces
112
-      echo '# Mesh Networking (B.A.T.M.A.N)' >> ~/interfaces
113
-      echo 'iface bat0 inet6 static' >> ~/interfaces
114
-      echo '    pre-up modprobe ipv6' >> ~/interfaces
115
-      echo "    address $PEER_BATMAN_IPV6" >> ~/interfaces
116
-      echo '    netmask 64' >> ~/interfaces
117
-      sudo mv ~/interfaces /etc/network/interfaces
118
-  else
119
-      sudo rm ~/interfaces
120
-  fi
121
-
122
-  echo '#!/bin/bash' > /tmp/freedombone_mesh
123
-  echo '' > /tmp/freedombone_mesh
124
-  echo '# stop network manager to make the mesh network work' >> /tmp/freedombone_mesh
125
-  echo 'service networking stop' >> /tmp/freedombone_mesh
126
-  echo '' >> /tmp/freedombone_mesh
127
-  echo -n '# configure the wlan interface to operate with ' >> /tmp/freedombone_mesh
128
-  echo 'mtus of 1532(batman requires it) and turn enc off ' >> /tmp/freedombone_mesh
129
-  echo 'to ensure it works' >> /tmp/freedombone_mesh
130
-  echo 'ifconfig wlan0 down' >> /tmp/freedombone_mesh
131
-  echo 'ifconfig wlan0 mtu 1532' >> /tmp/freedombone_mesh
132
-  echo 'iwconfig wlan0 enc off' >> /tmp/freedombone_mesh
133
-  echo '' >> /tmp/freedombone_mesh
134
-  echo '# add the interface to the ad-hoc network - or create it.' >> /tmp/freedombone_mesh
135
-  echo -n "iwconfig wlan0 mode ad-hoc essid mesh ap " >> /tmp/freedombone_mesh
136
-  echo "$BRIDGE_BATMAN_IPV6 channel 2" >> /tmp/freedombone_mesh
137
-  echo '' >> /tmp/freedombone_mesh
138
-  echo -n '# add wlan0 to the batman-adv virtual interface(so it can ' >> /tmp/freedombone_mesh
139
-  echo 'communicate with other batman-adv nodes)' >> /tmp/freedombone_mesh
140
-  echo 'batctl if add wlan0' >> /tmp/freedombone_mesh
141
-  echo 'ifconfig wlan0 up' >> /tmp/freedombone_mesh
142
-  echo 'ifconfig bat0 up' >> /tmp/freedombone_mesh
143
-  echo '' >> /tmp/freedombone_mesh
144
-  echo '# get the ip address for the node from the bridge connected to the dhcp server' >> /tmp/freedombone_mesh
145
-  echo 'dhclient bat0' >> /tmp/freedombone_mesh
146
-  echo '' >> /tmp/freedombone_mesh
147
-  echo 'exit 0' >> /tmp/freedombone_mesh
148
-  chmod +x /tmp/freedombone_mesh
149
-  sudo mv /tmp/freedombone_mesh /usr/bin/mesh
89
+  apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl
90
+  apt-get -y install python-dev libevent-dev ebtables python-pip
91
+  apt-get -y install wireless-tools rfkill
92
+
93
+  batman_script=/tmp/batman
94
+
95
+  echo '#!/bin/sh' > $batman_script
96
+  echo '' >> $batman_script
97
+  echo '# Mesh definition' >> $batman_script
98
+  echo "ESSID=$ESSID" >> $batman_script
99
+  echo "CELLID=$BATMAN_CELLID" >> $batman_script
100
+  echo "CHANNEL=$WIFI_CHANNEL" >> $batman_script
101
+  echo '' >> $batman_script
102
+  echo '# Ethernet bridge definition (bridged do bat0)' >> $batman_script
103
+  echo 'BRIDGE=br-mesh' >> $batman_script
104
+  echo 'IFACE=wlan0' >> $batman_script
105
+  echo 'EIFACE=eth0' >> $batman_script
106
+  echo 'IFACE_EXISTS=$(grep "$IFACE" /proc/net/dev)' >> $batman_script
107
+  echo '' >> $batman_script
108
+  echo 'if [ -e /etc/default/batctl ]; then' >> $batman_script
109
+  echo '    . /etc/default/batctl' >> $batman_script
110
+  echo 'fi' >> $batman_script
111
+  echo '' >> $batman_script
112
+  echo 'start() {' >> $batman_script
113
+  echo '    if [ -z "$IFACE" ] ; then' >> $batman_script
114
+  echo '        echo "error: unable to find wifi interface, not enabling batman-adv mesh"' >> $batman_script
115
+  echo '        return' >> $batman_script
116
+  echo '    fi' >> $batman_script
117
+  echo '    echo "info: enabling batman-adv mesh network $ESSID on $IFACE"' >> $batman_script
118
+  echo '    systemctl stop networking' >> $batman_script
119
+  echo '    sleep 5' >> $batman_script
120
+  echo '' >> $batman_script
121
+  echo '    # Might have to re-enable wifi' >> $batman_script
122
+  echo '    rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true' >> $batman_script
123
+  echo '' >> $batman_script
124
+  echo '    ifconfig $IFACE down' >> $batman_script
125
+  echo '    ifconfig $IFACE mtu 1528' >> $batman_script
126
+  echo '    iwconfig $IFACE enc off' >> $batman_script
127
+  echo '    iwconfig $IFACE mode ad-hoc essid $ESSID channel $CHANNEL' >> $batman_script
128
+  echo '    sleep 1' >> $batman_script
129
+  echo '    iwconfig $IFACE ap $CELLID' >> $batman_script
130
+  echo '' >> $batman_script
131
+  echo '    modprobe batman-adv' >> $batman_script
132
+  echo '    batctl if add $IFACE' >> $batman_script
133
+  echo '    ifconfig $IFACE up' >> $batman_script
134
+  echo '    ifconfig bat0 up promisc' >> $batman_script
135
+  echo '' >> $batman_script
136
+  echo '    #Use persistent HWAddr' >> $batman_script
137
+  echo '    ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")' >> $batman_script
138
+  echo '    if [ ! -f /var/lib/mesh-node/bat0 ]' >> $batman_script
139
+  echo '    then' >> $batman_script
140
+  echo '        mkdir /var/lib/mesh-node' >> $batman_script
141
+  echo '        echo "${ether_new}" > /var/lib/mesh-node/bat0' >> $batman_script
142
+  echo '    else' >> $batman_script
143
+  echo '        ether=$(cat /var/lib/mesh-node/bat0)' >> $batman_script
144
+  echo '        ifconfig bat0 hw ether ${ether}' >> $batman_script
145
+  echo '    fi' >> $batman_script
146
+  echo '' >> $batman_script
147
+  echo '    if [ "$EIFACE" ] ; then' >> $batman_script
148
+  echo '        ifconfig $EIFACE up promisc' >> $batman_script
149
+  echo '        brctl addbr $BRIDGE' >> $batman_script
150
+  echo '        brctl addif $BRIDGE bat0' >> $batman_script
151
+  echo '        brctl addif $BRIDGE $EIFACE' >> $batman_script
152
+  echo '        ifconfig $BRIDGE up' >> $batman_script
153
+  echo '    fi' >> $batman_script
154
+  echo '}' >> $batman_script
155
+  echo '' >> $batman_script
156
+  echo 'stop() {' >> $batman_script
157
+  echo '    if [ -z "$IFACE" ] ; then' >> $batman_script
158
+  echo '        echo "error: unable to find wifi interface, not enabling batman-adv mesh"' >> $batman_script
159
+  echo '        return' >> $batman_script
160
+  echo '    fi' >> $batman_script
161
+  echo '    if [ "$EIFACE" ] ; then' >> $batman_script
162
+  echo '        brctl delif $BRIDGE bat0' >> $batman_script
163
+  echo '        brctl delif $BRIDGE $EIFACE' >> $batman_script
164
+  echo '        ifconfig $BRIDGE down || true' >> $batman_script
165
+  echo '        brctl delbr $BRIDGE' >> $batman_script
166
+  echo '        ifconfig $EIFACE down -promisc' >> $batman_script
167
+  echo '    fi' >> $batman_script
168
+  echo '' >> $batman_script
169
+  echo '    ifconfig bat0 down -promisc' >> $batman_script
170
+  echo '' >> $batman_script
171
+  echo '    batctl if del $IFACE' >> $batman_script
172
+  echo '    rmmod batman-adv' >> $batman_script
173
+  echo '    ifconfig $IFACE mtu 1500' >> $batman_script
174
+  echo '    ifconfig $IFACE down' >> $batman_script
175
+  echo '    iwconfig $IFACE mode managed' >> $batman_script
176
+  echo '' >> $batman_script
177
+  echo '    systemctl restart networking' >> $batman_script
178
+  echo '}' >> $batman_script
179
+  echo '' >> $batman_script
180
+  echo 'if [ ! $IFACE_EXISTS ]; then' >> $batman_script
181
+  echo '    echo "Interface $IFACE was not found"' >> $batman_script
182
+  echo '    stop' >> $batman_script
183
+  echo '    exit 1' >> $batman_script
184
+  echo 'fi' >> $batman_script
185
+  echo '' >> $batman_script
186
+  echo 'case "$1" in' >> $batman_script
187
+  echo '    start|stop)' >> $batman_script
188
+  echo '        $1' >> $batman_script
189
+  echo '        ;;' >> $batman_script
190
+  echo '    *)' >> $batman_script
191
+  echo '    echo "error: invalid parameter $1"' >> $batman_script
192
+  echo '    echo "usage: $0 {start|stop}"' >> $batman_script
193
+  echo '    exit 2' >> $batman_script
194
+  echo '    ;;' >> $batman_script
195
+  echo 'esac' >> $batman_script
196
+  echo 'exit 0' >> $batman_script
197
+  chmod +x $batman_script
198
+  sudo mv $batman_script /usr/bin/batman
150 199
 }
151 200
 
152 201
 function show_help {
@@ -164,10 +213,6 @@ case $key in
164 213
     -h|--help)
165 214
     show_help
166 215
     ;;
167
-    --bridge-ip)
168
-    shift
169
-    BRIDGE_BATMAN_IPV6="$1"
170
-    ;;
171 216
     *)
172 217
     # unknown option
173 218
     ;;