Neddie
Posts: 35
Joined: Tue Feb 02, 2016 6:45 am

restart wifi , error in script

Tue Feb 23, 2016 9:28 am

I want to reboot my wireless network when it stops responding. I found this
script , but when I run it I get: line 3: [0: command not found
Any idea what I'm doing wrong?

ping -c4 192.168.1.1 > /dev/null

if [ $? != 0 ]
then
echo "No network connection, restarting wlan0"
/sbin/ifdown 'wlan0'
sleep 5
/sbin/ifup --force 'wlan0'
fi

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: restart wifi , error in script

Tue Feb 23, 2016 10:02 pm

Try this

Code: Select all

#!/bin/bash

 router="192.168.1.1"
 ip_addr="192.168.1.5/24"

/bin/ping -q -c1 $router > /dev/null

if [ $? -eq  0 ]
then
  : #  echo "Network active"
else
  echo "Network down, fixing..."
  /bin/kill -9 `pidof wpa_supplicant`
  /sbin/ifup --force wlan0
  /sbin/ifconfig "wlan0 $ip_addr"
  /sbin/ip route add default via $router dev wlan0
  cat > /etc/resolv.conf <<resolver
nameserver $router
nameserver 8.8.8.8
search example.co.uk
resolver

fi
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mnmlisme
Posts: 2
Joined: Fri Feb 26, 2016 2:02 am
Contact: Website

Re: restart wifi , error in script

Fri Feb 26, 2016 2:08 am

stumbled upon this thread when I was looking for a solution.

my problem: using raspberry pi as network monitoring too at a remote location (connected via internet) so I can:
- raspberry pi to check for internet connection, and if there's none, it should restart the modem using telnet (I found a tutorial online that show how to restart a broadband mode with telnet)
- use WOL to start a PC at the remote location
- act as samba server for the other PC to backup files to (CrashPlan)

for the first task, can I use the code you written?

thank you.
Raspberry Pi B+ with Raspbian Jessie Lite

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: restart wifi , error in script

Fri Feb 26, 2016 10:53 pm

If I didn't intend you to use it, I wouldn't have published it on here.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mnmlisme
Posts: 2
Joined: Fri Feb 26, 2016 2:02 am
Contact: Website

Re: restart wifi , error in script

Sat Feb 27, 2016 8:11 am

oh that's not what I mean.

I mean, is the code you wrote still fit my need?

where do I input the command to restart my modem (model Zyxel P660HN-T1 v2) or does your code restart any modem (universal)? and I assume I have to replace eth0 if my raspberry pi is connected via cable to the modem?

thanks,
Raspberry Pi B+ with Raspbian Jessie Lite

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: restart wifi , error in script

Sat Feb 27, 2016 10:59 am

mnmlisme wrote: I mean, is the code you wrote still fit my need?
Try it. If it doesn't work, feel free to re-write it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Beginners”