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 main(args): global wlan, ppp, IP, Netmask #the_version = open("VERSION").read().strip() the_version = "0.1.7" print "****" print "Hotspotd " + the_version print "A simple daemon to create wifi hotspot on Linux!" print "****" print "Copyright (c) 2014-2016" print "Prahlad Yeri<*****@*****.**>\n" scpath = os.path.realpath(__file__) realdir = os.path.dirname(scpath) os.chdir(realdir) #cli.arguments = args #initialize cli.arguments = type('', (object,),{"verbose":False})() newconfig = False if not os.path.exists('hotspotd.json'): configure() newconfig=True if len(cli.check_sysfile('hostapd'))==0: print "hostapd is not installed on your system. This package will not work without it.\nTo install hostapd, run 'sudo apt-get install hostapd'\nor refer to http://wireless.kernel.org/en/users/Documentation/hostapd after this installation gets over." time.sleep(2) dc =json.load(open('hotspotd.json')) wlan = dc['wlan'] ppp = dc['inet'] IP=dc['ip'] Netmask=dc['netmask'] SSID = dc['SSID'] password = dc['password'] if args == 'configure': if not newconfig: configure() elif args == 'stop': stop_router() elif args == 'start': if (cli.is_process_running('hostapd') != 0 and cli.is_process_running('dnsmasq') != 0): print 'hotspot is already running.' else: start_router()
def main(args): global wlan, ppp, IP, Netmask scpath = os.path.realpath(__file__) realdir = os.path.dirname(scpath) os.chdir(realdir) #print 'changed directory to ' + os.path.dirname(scpath) #if an instance is already running, then quit #const.verbose = args.verbose #const.command = args.command #const.blocking = args.blocking #const.argv = [os.getcwd() + '/server.py'] + sys.argv[1:] cli.arguments = args #initialize newconfig = False if not os.path.exists('hotspotd.json'): configure() newconfig=True if len(cli.check_sysfile('hostapd'))==0: print "hostapd is not installed on your system.This package will not work without it.To install it, try 'sudo apt-get install hostapd' or http://wireless.kernel.org/en/users/Documentation/hostapd after this installation gets over." time.sleep(2) dc =json.load(open('hotspotd.json')) wlan = dc['wlan'] ppp = dc['inet'] IP=dc['ip'] Netmask=dc['netmask'] SSID = dc['SSID'] password = dc['password'] if args.command == 'configure': if not newconfig: configure() elif args.command == 'stop': stop_router() elif args.command == 'start': if (cli.is_process_running('hostapd') != 0 and cli.is_process_running('dnsmasq') != 0): print 'hotspot is already running.' else: start_router()
def main(args): global wlan, ppp, IP, Netmask scpath = os.path.realpath(__file__) realdir = os.path.dirname(scpath) os.chdir(realdir) #print 'changed directory to ' + os.path.dirname(scpath) #if an instance is already running, then quit #const.verbose = args.verbose #const.command = args.command #const.blocking = args.blocking #const.argv = [os.getcwd() + '/server.py'] + sys.argv[1:] cli.arguments = args #initialize newconfig = False if not os.path.exists('hotspotd.json'): configure() newconfig=True dc =json.load(open('hotspotd.json')) wlan = dc['wlan'] ppp = dc['inet'] IP=dc['ip'] Netmask=dc['netmask'] SSID = dc['SSID'] password = dc['password'] if args.command == 'configure': if not newconfig: configure() elif args.command == 'stop': stop_router() elif args.command == 'start': if (cli.is_process_running('hostapd') != 0 and cli.is_process_running('dnsmasq') != 0): print 'hotspot is already running.' else: start_router()
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
parser.add_argument('command', choices=['start', 'stop', 'configure']) args = parser.parse_args() #const.verbose = args.verbose #const.command = args.command #const.blocking = args.blocking #const.argv = [os.getcwd() + '/server.py'] + sys.argv[1:] cli.arguments = args #initialize newconfig = False if not os.path.exists('hotspotd.json'): configure() newconfig=True dc =json.load(open('hotspotd.json')) wlan = dc['wlan'] ppp = dc['inet'] IP=dc['ip'] Netmask=dc['netmask'] SSID = dc['SSID'] password = dc['password'] if args.command == 'configure': if not newconfig: configure() elif args.command == 'stop': stop_router() elif args.command == 'start': if (cli.is_process_running('hostapd') != 0 and cli.is_process_running('dnsmasq') != 0): print 'hotspot is already running.' else: start_router()
args = parser.parse_args() #const.verbose = args.verbose #const.command = args.command #const.blocking = args.blocking #const.argv = [os.getcwd() + '/server.py'] + sys.argv[1:] cli.arguments = args #initialize newconfig = False if not os.path.exists('hotspotd.json'): configure() newconfig = True dc = json.load(open('hotspotd.json')) wlan = dc['wlan'] ppp = dc['inet'] IP = dc['ip'] Netmask = dc['netmask'] SSID = dc['SSID'] password = dc['password'] if args.command == 'configure': if not newconfig: configure() elif args.command == 'stop': stop_router() elif args.command == 'start': if (cli.is_process_running('hostapd') != 0 and cli.is_process_running('dnsmasq') != 0): print 'hotspot is already running.' else: start_router()