I have prepared two simple Python scripts. One for connecting to the
network,
and the second one for disconnecting from the network.
Of course, they should be further improved:
1. You should add support for modem locking file
2. You should allow script to find the right special device file,
not assuming, that it should always be "/dev/ttyUSB0" (it may
be wrong assumption if you connect any USB->RS232 adapter
to your machine)
In fact the best cure would be to prepare the udev rules, so that
the modem gets a special device name, e.g. /dev/tty_e173
3. On disconnecting, you should be able to kill only this dhclient
which services the wwan0 interface. Now the script kills all
dhclient processes.
Anyway below are my "quick&dirty" scripts (please consider, that some
usenet archives break the indentation in Python scripts, you may need
to fix it before use).
==== e173_NDIS_on.py ============
#!/usr/bin/python
import os
import thread
import threading
class msg_fifo():
def __init__(self):
self.lock=threading.Condition()
self.queue=[]
def append(self,data):
self.lock.acquire()
self.queue.append(data)
self.lock.notifyAll()
self.lock.release()
def pop(self,tmout):
self.lock.acquire()
if len(self.queue) == 0:
self.lock.wait(tmout)
if len(self.queue) == 0:
self.lock.release()
return None
res=self.queue.pop(0)
self.lock.release()
return res
responses = msg_fifo()
resp_lock=thread.allocate_lock()
def receive(in_file,out_fifo):
line=""
while True:
c=in_file.read(1)
if c=="\n":
out_fifo.append(line)
line = ""
else:
line += c
modem="/dev/ttyUSB0"
m_out=open(modem,"w")
m_in=open(modem,"r")
# Start the receiving thread
rcv_thread = thread.start_new_thread(receive,(m_in,responses))
m_out.write("ATZ\r")
m_out.flush()
while True:
l = responses.pop(12)
print l
if l=="OK\r":
break
# else:
# raise SystemExit("Unexpected response: "+l)
m_out.write("AT^NDISDUP=1,1,\"my_apn_name\"\r")
m_out.flush()
while True:
l = responses.pop(12)
print l
if l=="OK\r":
break
# else:
# raise SystemExit("Unexpected response!")
os.system("/sbin/ifconfig wwan0 hw ether 00:01:02:03:04:05")
os.system("/sbin/dhclient wwan0")
======== e173_NDIS_off.py =============================
#!/usr/bin/python
import thread
import threading
import os
class msg_fifo():
def __init__(self):
self.lock=threading.Condition()
self.queue=[]
def append(self,data):
self.lock.acquire()
self.queue.append(data)
self.lock.notifyAll()
self.lock.release()
def pop(self,tmout):
self.lock.acquire()
if len(self.queue) == 0:
self.lock.wait(tmout)
if len(self.queue) == 0:
self.lock.release()
return None
res=self.queue.pop(0)
self.lock.release()
return res
responses = msg_fifo()
resp_lock=thread.allocate_lock()
def receive(in_file,out_fifo):
line=""
while True:
c=in_file.read(1)
if c=="\n":
out_fifo.append(line)
line = ""
else:
line += c
modem="/dev/ttyUSB0"
m_out=open(modem,"w")
m_in=open(modem,"r")
# Start the receiving thread
rcv_thread = thread.start_new_thread(receive,(m_in,responses))
m_out.write("AT^NDISDUP=1,0\r")
m_out.flush()
while True:
l = responses.pop(12)
if l=="OK\r":
break
# else:
# raise SystemExit("Unexpected response!")
os.system("killall dhclient")
os.system("ifconfig wwan0 down")