LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking > Linux - Wireless Networking
User Name
Password
Linux - Wireless Networking This forum is for the discussion of wireless networking in Linux.

Notices


Reply
  Search this Thread
Old 01-19-2016, 03:05 PM   #1
erixoltan
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Rep: Reputation: Disabled
Script to turn wifi on/off


Is it possible to use a shell script to turn wifi off and on in Linux?

Due to a company security policy, we can't use wifi networking except between midnight and 1AM. I have a laptop with wifi, that has to be disabled most of the time. It does have an Ethernet connection to a router that is not connected to the Internet.

The laptop needs to connect to the wireless network every day, do an update from the repositories and download some data. I want to create a cron job that runs at midnight and performs the following steps.
(1) Enable wifi and connect to the wireless network.
(2) Perform WGET operations to download a number of data files. Maybe do critical software updates.
(3) Turn the wifi back off.
(4) Reconnect to the local router via Ethernet.

So the script needs to be able to manage the network connections automatically, and it needs to be able to run repeatedly. We're not setting the connections up for the first time, just enabling/disabling them.
 
Old 01-20-2016, 07:45 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by erixoltan View Post
Is it possible to use a shell script to turn wifi off and on in Linux?
Due to a company security policy, we can't use wifi networking except between midnight and 1AM. I have a laptop with wifi, that has to be disabled most of the time. It does have an Ethernet connection to a router that is not connected to the Internet.

The laptop needs to connect to the wireless network every day, do an update from the repositories and download some data. I want to create a cron job that runs at midnight and performs the following steps.
(1) Enable wifi and connect to the wireless network.
(2) Perform WGET operations to download a number of data files. Maybe do critical software updates.
(3) Turn the wifi back off.
(4) Reconnect to the local router via Ethernet.

So the script needs to be able to manage the network connections automatically, and it needs to be able to run repeatedly. We're not setting the connections up for the first time, just enabling/disabling them.
Very possible...and also very pointless in the scenario you're describing. Since you say the laptop is ON, and already connected to the local wired network, then why turn on wifi at all??? Download what you need over the wired connection.

You can use ifconfig to bring an interface up/down, and wget to do whatever else you need for downloading. Should be very easy to script...there are thousands of bash scripting tutorials you can find with a brief Google search. Where are you stuck?? What have you written so far? We're happy to help, but we WILL NOT write your script for you.
 
1 members found this post helpful.
Old 01-20-2016, 07:50 AM   #3
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
Quote:
It does have an Ethernet connection to a router that is not connected to the Internet.
.......
 
Old 01-20-2016, 08:11 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by Emerson View Post
It does have an Ethernet connection to a router that is not connected to the Internet.
Whoops...missed that part.

Still confusing, though...hard to imagine a network that does NOT have an outbound connection somehow, or that wifi is only available for one hour a day, in the middle of the night, or there aren't some sort of provisions made at the company to let folks do updates during the day.

...unless this is homework.....
 
Old 01-20-2016, 08:11 AM   #5
erixoltan
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Sorry, my fault: I should have explained better.

They have a disconnected subnet. There is a policy that the systems on the local network can't get data from the Internet except for a single data file that we need to download on a daily basis. There's also a policy that we can't have radio signals. The only exception is for an hour at night when it's OK to turn on the radio. But even then, the subnet stays disconnected and only this one laptop can perform this one update.

So they have a wired router that is not connected to the Internet. I have an Ethernet cable from the laptop to that local router. I need to pull down a data file and the easiest thing seems to be, do it over wifi during the night when the wireless signal is available for one hour. While I'm at it, I can get updates.

Here's a script I have worked out since I posted this question yesterday. It's intended to be run as root from a cron job.
Code:
# turn off the ethernet interface.
ifconfig eth0 disable
# turn on wifi interface.
ifconfig wlan0 enable
# turn on wifi radio
iwconfig wlan0 txpower on

# repository update (optional)
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y

# Download update data file
wget url-of-data-file
chown user name-of-file

# turn off radio power
iwconfig wlan0 txpower off
# turn off wifi interface
ifconfig wlan0 disable
# turn ethernet back on
ifconfig eth0 enable
I would be grateful if anyone has criticisms or maybe there's some flaw in the script, or issue I'm not aware of. I'm separately turning off and on the radio just to be sure there's no interference.
 
Old 01-20-2016, 08:36 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by erixoltan View Post
Sorry, my fault: I should have explained better.

They have a disconnected subnet. There is a policy that the systems on the local network can't get data from the Internet except for a single data file that we need to download on a daily basis. There's also a policy that we can't have radio signals. The only exception is for an hour at night when it's OK to turn on the radio. But even then, the subnet stays disconnected and only this one laptop can perform this one update.

So they have a wired router that is not connected to the Internet. I have an Ethernet cable from the laptop to that local router. I need to pull down a data file and the easiest thing seems to be, do it over wifi during the night when the wireless signal is available for one hour. While I'm at it, I can get updates.

Here's a script I have worked out since I posted this question yesterday. It's intended to be run as root from a cron job.
Code:
# turn off the ethernet interface.
ifconfig eth0 disable
# turn on wifi interface.
ifconfig wlan0 enable
# turn on wifi radio
iwconfig wlan0 txpower on

# repository update (optional)
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y

# Download update data file
wget url-of-data-file
chown user name-of-file

# turn off radio power
iwconfig wlan0 txpower off
# turn off wifi interface
ifconfig wlan0 disable
# turn ethernet back on
ifconfig eth0 enable
I would be grateful if anyone has criticisms or maybe there's some flaw in the script, or issue I'm not aware of. I'm separately turning off and on the radio just to be sure there's no interference.
That looks like it'll work nicely, providing you don't encounter any errors. If any of those steps fail, the rest will just continue on, or try to...but if it's not critical, it shouldn't matter, especially if you're just doing apt-get's and downloading a single file.

Since this is for a company, I'd suggest you get with your data-security folks, and ask them to work with you, to provide a more bulletproof solution that fits in with your company's policies. Perhaps putting a dedicated VM Server in your DMZ, that does NOTHING but fetch such things, and acts as a repository would be in order. That would leave that machine in their control, and you could update as you see fit during the day.
 
Old 01-20-2016, 11:35 AM   #7
erixoltan
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
That's great advice. I'm going to mark this as solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't turn off wifi in laptop hottdogg Slackware 8 10-10-2013 10:14 PM
turn off wifi at bootup? dvp1964 Linux - Wireless Networking 1 02-14-2011 03:49 PM
How do I turn my wifi on in my netbook? help please. first post. asmallchild Linux - Networking 16 01-03-2010 02:20 PM
turn on wifi with power on button irvken Linux - Hardware 1 10-20-2007 12:04 PM
Wifi card won't turn on? wmshub Linux - Wireless Networking 1 08-06-2005 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking > Linux - Wireless Networking

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration