简体   繁体   中英

Script to check if wifi is connected

In Max OS Mojave, is it possible to create a shell script or something similar that checks for an active wifi connection, and if not, disables wifi then re-enable?

A little background. Basically I need to make sure that I can connect to my computer at home from anywhere. Normally this is no problem, but we have been having power outages with current storms and when the power comes back on, my Mac does not always reconnect to my wifi network (may have something to do with router, it is old). It's not a common problem, but I'll be darned if it doesn't happen at the most inconvenient times. In order to fix this, I simply have to disable wifi, then re-enable. This fixes the issue and the wifi auto reconnects. But I can't do that if I'm not at home.

I've considered a script that runs every 20 minutes or so that disables wifi, then re-enables it using launchd which technically fixes my problem, however I don't want to have to keep disabling the script while using my Mac because if I don't disable it, I'll loose wifi every 20 minutes.

Any information would be greatly appreciated.

networksetup is an amazing command such that I haven't even scratched the surface. Here's something that works:

if ! networksetup -getinfo Wi-Fi | grep Router
then
  networksetup -setairportnetwork en0 $routername $routerpassword
fi

This will request data about the Wi-Fi network service, which is 5-10 lines of various info, including whether or not it is connected to a router, and which. If that line is missing, you are not connected.

In that case, you can explicitly request to be connected to a specific router (with a specific username and password). Or, alternately, you can do the same thing you did before, which you say works as well (and you don't need to write down your router's credentials).

(There may well be a smarter way. As I said, that command can do so many things.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM