如何从终端启用/禁用移动宽带?

我在Natty Narwhal上使用中兴USB调制解调器。 每件事情都很好,但有时会断开连接。 我想写一个Shell脚本,如果它断开连接或连接5秒后收到的数据小于20 KB,则重新连接移动宽带。

所以我的问题是如何启用/禁用移动宽带? 如何检查收到的数据? 以及如何启用/禁用网络服务?

注意:只有终端命令或者如果你能写脚本,我会非常感激。

打开终端窗口并输入:

sudo gedit /etc/init.d/mobile-broadband-connect 

然后复制并粘贴(根据您的需要进行更改):

注意:替换为

 #!/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  ;; stop) echo "Stopping Mobile Broadband Connection." nmcli -t con down id  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 

重新启动以完成安装和自动连接。

  • 重新启动系统以完成安装。
  • 重启后,USB设备激活前最多需要60秒。
  • 激活时 – 移动宽带连接将被激活并自动连接。

完成……

我创建了一个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 . 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