LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Wireless Networking (https://www.linuxquestions.org/questions/linux-wireless-networking-41/)
-   -   Script to turn wifi on/off (https://www.linuxquestions.org/questions/linux-wireless-networking-41/script-to-turn-wifi-on-off-4175564405/)

erixoltan 01-19-2016 03:05 PM

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.

TB0ne 01-20-2016 07:45 AM

Quote:

Originally Posted by erixoltan (Post 5481106)
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.

Emerson 01-20-2016 07:50 AM

Quote:

It does have an Ethernet connection to a router that is not connected to the Internet.
.......

TB0ne 01-20-2016 08:11 AM

Quote:

Originally Posted by Emerson (Post 5481488)
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.....

erixoltan 01-20-2016 08:11 AM

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.

TB0ne 01-20-2016 08:36 AM

Quote:

Originally Posted by erixoltan (Post 5481495)
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.

erixoltan 01-20-2016 11:35 AM

That's great advice. I'm going to mark this as solved.


All times are GMT -5. The time now is 11:51 PM.