我在Natty Narwhal上使用ZTE USB调制解调器。一切正常,但有时会断开连接。我想编写一个Shell脚本,如果断开连接或连接5秒钟后接收到的数据小于20 KB,该脚本将重新连接移动宽带。
所以我的问题是如何启用/禁用移动宽带?如何检查收到的数据?以及如何启用/禁用网络服务?
注意:仅适用于终端命令, 或者如果您可以编写脚本,我将非常感谢。
我在Natty Narwhal上使用ZTE USB调制解调器。一切正常,但有时会断开连接。我想编写一个Shell脚本,如果断开连接或连接5秒钟后接收到的数据小于20 KB,该脚本将重新连接移动宽带。
所以我的问题是如何启用/禁用移动宽带?如何检查收到的数据?以及如何启用/禁用网络服务?
注意:仅适用于终端命令, 或者如果您可以编写脚本,我将非常感谢。
Answers:
打开终端窗口并输入:
sudo gedit /etc/init.d/mobile-broadband-connect
然后复制并粘贴此内容(根据您的需要更改):
注意:将替换<Your Mobile Broadband Connection Name Here>
为您的连接名称。
#!/bin/bash
case "$1" in
start)
echo "Starting Mobile Broadband Connection."
while true; do
# testing...to see if gsm is on the list of active devices
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
if [ $? -eq 0 ]; then
break
else
# not connected, sleeping for a second
sleep 1
fi
done
# now once GSM modem shows up, run these commands
nmcli -t nm wwan on
nmcli -t con up id <Your Mobile Broadband Connection Name Here>
;;
stop)
echo "Stopping Mobile Broadband Connection."
nmcli -t con down id <Your Mobile Broadband Connection Name Here>
nmcli -t nm wwan off
;;
status)
# Check to see if the process is running with Network Manager dev status
nmcli -p dev
;;
*)
echo "Mobile Broadband Startup Service"
echo $"Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
更改此文件的执行权限:
sudo chmod +x /etc/init.d/mobile-broadband-connect
要运行此脚本的服务,请执行以下操作:
sudo update-rc.d mobile-broadband-connect defaults
该脚本已注册为系统启动服务,因此您可以使用以下命令启动,停止或检查脚本的状态:
sudo service mobile-broadband-connect start
sudo service mobile-broadband-connect stop
sudo service mobile-broadband-connect status
重新启动以完成安装和自动连接。
完成...
我创建了如下的shell脚本,并将其放进去Startup Applications
,它就像一个魅力!我对此感到满意,但是如果您能做得更好,我将非常感激。
#!/bin/bash
while true; do
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
if [ $? -eq 0 ]; then
#jdownloader is still in the download status so stop it because
#internet is disconnected and jdownloader won't resume download
#when connected again
#jdownloader --stop-download
#sometimes I can not get connected after disconnection when
#I click on <name of the network connection>. I have to disable
#and enable Mobile Broadband
nmcli -t nm wwan off
sleep 1
nmcli -t nm wwan on
sleep 1
nmcli -t con up id "Tata Docomo Internet"
#wait approximately 15 sec to get connected
#if anyone can add better command to check for it just comment it :-p
sleep 15
#now connected to internet so start download
#jdownloader --start-download
fi
#it does not worth keep it checking every millisecond.
#my connection will be reestablished within 5-15 seconds
sleep 2
#if anyone can code it better please feel free to comment
#TO-DO:: check for data received. if data < 15 KB after 20 seconds of connection
#reconnect mobile broadband connection
done
#!/bin/sh
echo "Starting Mobile Broadband Connection. Tej"
while true; do
# testing...to see if gsm is on the list of active devices
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
if [ $? -eq 0 ]; then
break
else
# not connected, sleeping for a second
sleep 1
fi
done
# now once GSM modem shows up, run these commands
while true; do
# Enable Mobile Broadband
nmcli -t nm wwan on
# Connect to network
nmcli -t con up id "BSNL/CellOne New GPRS/3G 1"
# Check status if connected or not
nmcli -f device,state -t dev | grep ttyACM0 | awk -F':' '{print $2}' | { read status; }
echo $status;
if [$status == "connected"]; then
break
else
# not connected, sleeping for a second
nmcli -t nm wwan off
sleep 1
fi
done
sudo rm /etc/init.d/mobile-broadband-connect && sudo update-rc.d mobile-broadband-connect remove
并删除了此服务。然后一切顺利。如何摆脱这个?