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

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

我在 Natty Narwhal 上使用 ZTE USB 调制解调器。一切正常,但有时会断开连接。我想编写 Shell 脚本,如果断开连接或连接 5 秒后收到的数据少于 20 KB,则重新连接移动宽带。

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

注意:仅限终端命令 或者如果你能编写脚本,我将非常感激。

答案1

打开终端窗口并输入:

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

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

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

完毕 ...

答案2

我创建了一个如下所示的 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

答案3

#!/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

相关内容