我的问题可能看起来很奇怪,但如果你的互联网连接每隔几个小时就会中断一次,那么这确实很有意义。
我想知道如何使用 shell 脚本重新启动网络管理器。
现在我知道这sudo service network-manager restart
就是在终端中执行此操作的方法,但遗憾的是这在 shell 脚本中不起作用。
有任何想法吗 ?
答案1
我也遇到过类似的问题。我的网络非常不稳定,当电源波动时,调制解调器就会离线,除非你拔下调制解调器并重新插入,否则无法看到它。否则,你可以使用 usb_modeswitch。所以我采用了上述脚本并对其进行了调整,以完成这两项任务。
#!/bin/bash
while true; do
#Anything less than a solid connection reboots the USB modem
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:connected$"
if [ ! $? -eq 0 ]; then
#Reset USB Modem (12d1:1001 will have to be changed to match your modem)
sudo usb_modeswitch -R -v 12d1 -p 1001
#Wait 20 Seconds before trying to bring up the Broadband connections
sleep 20
nmcli -t nm wwan on
#Wait Another 20 Seconds then test if the connection came up on its own as it is set to auto-connect
sleep 20
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:disconnected$"
if [ $? -eq 0 ]; then
nmcli -t con up id "Zantel connection"
sleep 15
fi
#wait approximately 15 sec to get connected
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
谢谢!
答案2
不,这一点也不奇怪,当我使用 USB 网络设置调制解调器时,我也经常遇到连接问题。
以下是具体操作方法
gksu 服务网络管理器重启
将其保存在扩展名为 .sh 的文件中,然后右键单击>>属性>>权限以授予可执行文件权限
答案3
nmcli
是一个用于控制 NetworkManager 并获取其状态的命令行工具。
我在使用移动宽带时也遇到了同样的问题。
我创建了一个如下所示的 shell 脚本。保存它,授予执行权限,然后将其放入启动应用程序中,它就可以完美运行了!如果连接断开,它将自动连接。这正是我想要的。
您需要更改网络 ID(对我来说是"Tata Docomo Internet"
)。将其替换"Tata Docomo Internet"
为您的移动宽带连接名称。
#!/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
答案4
这是对前一个问题的更好的解决方案。
我已经创建了 2 个脚本,第一个是这个……
sudo /home/{your user name}/UMM.sh
如果您创建了一个图标,通过启动这个“终端”,您可以输入您的 sudo 密码。
它调用的 UMM.sh 如下... 要使它工作,您必须阅读并更改脚本顶部的内容。您可能还需要更改一些行sleep
以适合您的情况。
玩得开心!
#!/bin/bash
# *****************************************************************
#Change the following values based on your connection type and name
#Running 'nmcli -t -f TYPE,STATE dev'
#cdma or gsm (edit below)
ConType="cdma"
#Running 'nmcli -t -f NAME,TYPE con'
#The name of your connection in the network manager (edit below)
ConName="Zantel connection"
#Running 'lsusb'
#Get the Vendor and Model ID for your modem (edit below)
USB_Vendor=12d1
USB_Product=1001
#Running 'ifconfig'
#Get the Internet Connection (edit below)
IntCon=ppp0
# ******************************************************************
Network_State=$(nmcli -t -f TYPE,STATE dev)
echo -e
echo -e "Current Network Status: \r"
echo $Network_State
i=0
error_count=0
#default 600
Good_connect_count=600
i=$[Good_connect_count - 50]
Default_ping_host="google.com"
pingtest=0
while true; do
#First off determine nature of problem if one exists
#Test to see if our connection type even exists
MT=C nmcli -t -f TYPE,STATE dev | grep -q $ConType
if [ $? -eq 0 ]; then
#Check to see if the WWAN is enabled
MT=C nmcli -t -f WWAN nm | grep -q "enabled"
if [ $? -eq 0 ]; then
#Check to see if we have a ppp0 connection
MT=C ifconfig | grep -q $IntCon
if [ $? -eq 0 ]; then
error_count=$error_count
else
dt=$(date)
echo At $dt we have no $IntCon connection...
echo Attempting to start $ConName
nmcli -t con up id "$ConName"
sleep 5
MT=C ifconfig | grep -q $IntCon
if [ $? -eq 0 ]; then
#error_count=0
i=$Good_connect_count
else
error_count=$[$error_count+1]
echo "$error_count Error(s) in a row"
fi
fi
else
dt=$(date)
echo At $dt we have no WWAN...attempting to Enable
nmcli -t nm wwan on
sleep 5
MT=C nmcli -t -f WWAN nm | grep -q "enabled"
if [ $? -eq 0 ]; then
error_count=$error_count
else
error_count=$[$error_count+1]
echo "$error_count Error(s) in a row"
fi
fi
else
dt=$(date)
echo At $dt we dont have our $ConType connection
echo Rebooting USB Device $USB_Vendor : $USB_Product
#Reset USB Modem
usb_modeswitch -Q -R -v $USB_Vendor -p $USB_Product
error_count=0
sleep 25
fi
if [ $error_count -ge 3 ]; then
dt=$(date)
echo We have an issue. Rebooting USB Device at $dt.
usb_modeswitch -Q -R -v $USB_Vendor -p $USB_Product
sleep 25
error_count=0
i=0
fi
i=$[$i+1]
if [ $i -ge $Good_connect_count ]; then
dt=$(date)
if [ $pingtest -eq 0 ]; then
MT=C ping -c1 $Default_ping_host | grep -q "64 bytes from"
if [ $? -eq 0 ]; then
echo At $dt connection is up and ping test passed!
#error_count=0
i=0
pingtest=1
else
echo Connection is present but cant confirm ping connectivity. Retrying...
error_count=$[$error_count+1]
echo "$error_count Error(s) in a row"
fi
else
pingtest=0
MT=C wget --spider http://www.hp.com 2>&1 | grep -q "200"
if [ $? -eq 0 ]; then
echo At $dt connection is up and http test passed!
error_count=0
i=0
else
echo Connection is present but cant confirm http connectivity. Retrying...
error_count=$[$error_count+1]
echo "$error_count Error(s) in a row"
fi
fi
fi
done