自动重新连接无线连接

不幸的是,我家里的无线连接经常消失,需要重启无线路由器。

更糟糕的是,我的ubuntu媒体电脑在它消失后不会自动重新连接到无线网络,然后大约一分钟后出现。 有问题的网络在网络设置中设置为“自动连接”。

如果我手动选择我的无线网络,使用屏幕右上角的无线图标,一切正常,直到下次无线网络关闭时。

我正在寻找一种方法,所以我不必记得一直手动执行此操作。

我的笔记本电脑的英特尔无线WiFi 5100半高卡和驱动程序iwlagn驱动程序有类似的问题。 这个问题是iwlagn驱动程序的一个已知问题,最好的解决方法是禁用卡上的802.11n。

要禁用此卡上的802.11n,请创建/编辑/etc/modprobe.d/options.conf文件:

 sudo -H gedit /etc/modprobe.d/options.conf 

并添加以下内容。

 options iwlagn 11n_disable=1 11n_disable50=1 

这似乎是在网上发布的,没有很好的解决方案。 我想最好的修复/解决方法是让它检查互联网连接,如果它没有那么重新连接。 我通过ping测试google.com做了这个,然后我简单地让它重新启动网络。 代码未经过测试(重启部分和cron部分,如果语句测试),所以我只是等待它在某些时候断开连接。 我有一个Ubuntu Server 12.10,所以没有GUI,每次无线连接都很难连接显示器和键盘。

Cron部分通过webmin完成,所以Idk很多关于它。 脚本如下:

 # edited by dim_voly for networking restart on no pingback every 5 mins #!/bin/bash # Name of File: networkingCron # Purpose: to check if the internet is up (via ping test to google) and if not, restart networking service # this script is invoked via cron, ideally every 5 mins. #check if there is internet via ping test if ! [ "`ping -c 1 google.com`" ]; then #if ping exits nonzero... sudo service networking restart #restart the whole thing echo Networking service restarted due to no ping response from google.com fi echo Script 'networkingCron' completed, if no message above then there was no network restart. # dunno how to restart the wifi only since that is the only active connection that server uses. # also I don't think those echos go anywhere 

确保以root身份运行并确保脚本具有执行(u + x)权限。

链接:

更多现代版的@DougD脚本

 #!/bin/bash wlan=$(/sbin/ifconfig wlan0 | grep inet\ addr -c) if [ "$wlan" -eq 0 ]; then /sbin/ifdown wlan0 && /sbin/ifup wlan0 else echo interface is up fi 

这是使用service network-manager restart的替代方法:

 #!/usr/bin/env bash # 1. copy this script into # /usr/bin # 2. change permissions # root:/usr/bin# chmod +x checkwifi.sh # 3. add to cron as root # sudo su # crontab -e # add this to check your wifi every minute # * * * * * /usr/bin/checkwifi.sh is_ok=$(/sbin/ifconfig wlp2s0 | /bin/grep inet\ addr -c) if [ "$is_ok" -eq 0 ] ; then # restart /usr/sbin/service network-manager restart # wifi is ok /bin/echo $(date) "wifi was restarted" >> /user/user/Dropbox/wifi.log /bin/echo $(/sbin/ifconfig wlp2s0) >> /home/user/Dropbox/wifi.log else # wifi is ok /bin/echo $(date) "wifi is ok" >> /home/user/Dropbox/wifi.log /bin/echo $(/sbin/ifconfig wlp2s0) >> /home/user/Dropbox/wifi.log fi 

只需创建一个新文件vi /root/checkwanup并添加以下内容:

 #!/bin/bash wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l` if [ $wlan -eq 0 ]; then /sbin/ifdown wlan0 && /sbin/ifup wlan0 else echo interface is up fi 

然后chmod 555 /root/checkwanup并将其添加到您的crontab:

 crontab -e */15 * * * * /bin/bash /root/checkwanup 

资料来源: http : //sirlagz.net/2013/01/10/script-wifi-checker-script/

您可能希望看一下使用wpa_supplicant而不是网络管理器,但这在媒体中心时并不重要。 wpa_supplicant并不像网络管理员那样灵活,但是在尝试三次后它不会放弃。 看看这个答案 。

这是我的版本 – 它适用于NetworkManager:

 #!/bin/bash wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l` if [ $wlan -eq 0 ]; then /usr/bin/nmcli nm wifi off && /usr/bin/nmcli nm wifi on fi