def stop_router(): #bring down the interface cli.execute_shell('ifconfig mon.' + wlan + ' down') #TODO: Find some workaround. killing hostapd brings down the wlan0 interface in ifconfig. #~ #stop hostapd #~ if cli.is_process_running('hostapd')>0: #~ cli.writelog('stopping hostapd') #~ cli.execute_shell('pkill hostapd') #stop dnsmasq if cli.is_process_running('dnsmasq')>0: cli.writelog('stopping dnsmasq') cli.execute_shell('killall dnsmasq') #disable forwarding in iptables. cli.writelog('disabling forward rules in iptables.') cli.execute_shell('iptables -P FORWARD DROP') #delete iptables rules that were added for wlan traffic. if wlan != None: cli.execute_shell('iptables -D OUTPUT --out-interface ' + wlan + ' -j ACCEPT') cli.execute_shell('iptables -D INPUT --in-interface ' + wlan + ' -j ACCEPT') cli.execute_shell('iptables --table nat --delete-chain') cli.execute_shell('iptables --table nat -F') cli.execute_shell('iptables --table nat -X') #disable forwarding in sysctl. cli.writelog('disabling forward in sysctl.') r = cli.set_sysctl('net.ipv4.ip_forward','0') print r.strip() #cli.execute_shell('ifconfig ' + wlan + ' down' + IP + ' netmask ' + Netmask) #cli.execute_shell('ip addr flush ' + wlan) print 'hotspot has stopped.' return
def start_router(): if not check_dependencies(): return elif not check_interfaces(): return pre_start() s = 'ifconfig ' + wlan + ' up ' + IP + ' netmask ' + Netmask print 'created interface: mon.' + wlan + ' on IP: ' + IP r = cli.execute_shell(s) cli.writelog(r) #cli.writelog('sleeping for 2 seconds.') print 'wait..' cli.execute_shell('sleep 2') i = IP.rindex('.') ipparts=IP[0:i] #stop dnsmasq if already running. if cli.is_process_running('dnsmasq')>0: print 'stopping dnsmasq' cli.execute_shell('killall dnsmasq') #stop hostapd if already running. if cli.is_process_running('hostapd')>0: print 'stopping hostapd' cli.execute_shell('killall hostapd') #enable forwarding in sysctl. print 'enabling forward in sysctl.' r=cli.set_sysctl('net.ipv4.ip_forward','1') print r.strip() #enable forwarding in iptables. print 'creating NAT using iptables: ' + wlan + '<->' + ppp cli.execute_shell('iptables -P FORWARD ACCEPT') #add iptables rules to create the NAT. cli.execute_shell('iptables --table nat --delete-chain') cli.execute_shell('iptables --table nat -F') r=cli.execute_shell('iptables --table nat -X') if len(r.strip())>0: print r.strip() cli.execute_shell('iptables -t nat -A POSTROUTING -o ' + ppp + ' -j MASQUERADE') cli.execute_shell('iptables -A FORWARD -i ' + ppp + ' -o ' + wlan + ' -j ACCEPT -m state --state RELATED,ESTABLISHED') cli.execute_shell('iptables -A FORWARD -i ' + wlan + ' -o ' + ppp + ' -j ACCEPT') #allow traffic to/from wlan cli.execute_shell('iptables -A OUTPUT --out-interface ' + wlan + ' -j ACCEPT') cli.execute_shell('iptables -A INPUT --in-interface ' + wlan + ' -j ACCEPT') #start dnsmasq s = 'dnsmasq --dhcp-authoritative --interface=' + wlan + ' --dhcp-range=' + ipparts + '.20,' + ipparts +'.100,' + Netmask + ',4h' print 'running dnsmasq' r = cli.execute_shell(s) cli.writelog(r) #~ f = open(os.getcwd() + '/hostapd.tem','r') #~ lout=[] #~ for line in f.readlines(): #~ lout.append(line.replace('<SSID>',SSID).replace('<PASS>',password)) #~ #~ f.close() #~ f = open(os.getcwd() + '/hostapd.conf','w') #~ f.writelines(lout) #~ f.close() #writelog('created: ' + os.getcwd() + '/hostapd.conf') #start hostapd #s = 'hostapd -B ' + os.path.abspath('run.conf') s = 'hostapd -B ' + os.getcwd() + '/run.conf' cli.writelog('running hostapd') #cli.writelog('sleeping for 2 seconds.') cli.writelog('wait..') cli.execute_shell('sleep 2') r = cli.execute_shell(s) cli.writelog(r) print 'hotspot is running.' return