Jump to content

Recommended Posts

DHCP Toggle

 

Find it on:

Short Description

Set your DHCP Settings via Alfred.

 

Long Description

DHCP Toggle allows you to alter your DHCP settings between a manual and an automatic configuration. When you set an automatic configuration, which is normal, your computer will receive an IP address directly from the router that you’re connected to. If you choose manual, you have the option of setting your own IP address. If the router’s IP address is 10.0.1.1, then you’ll be able to get an IP with a prefix of 10.0.1 and a suffix between 2 and 255. If the IP address is already used, then you’ll have to try again. DHCP Toggle will also set your router address to the IP prefix that you use, followed by 1, which is always reserved for the router.

 

You can use this when your Internet connection is weak, or if a router is clogged, or if, for some random reason, the router doesn’t like you connecting with an automatic IP address (the original need for this workflow).

 

Commands

dhcp <arg>

 

Available arguments:

  • None: defaults to your default IP prefix + random number 2–255
  • Switch to auto: auto
  • IP Address: 10.0.1.192
  • Number: 2–255, i.e. 32
  • Configure c

Configuration

 

Default Prefix

You can set a default prefix, the first three numbers of an IP address, which will be given precedence over the other available prefixes. If you just type a three-digit number (between 2–255), then it will set your IP address to DEFAULTIP.NUMBER.

 

IP Prefixes

You can set a list of prefixes that you commonly use, and those will appear in the script filter to choose easily. If no prefixes file is found, then the following list will be populated:

10.0.0
10.0.1
192.168.0
192.168.1
192.168.2
192.1.10

You can add and remove from this list from the configuration option.

 

Passwordless Execution (sudo)

 

If you would prefer to use this workflow without a password, you can select the option to add a record to your sudoers file. The script will add the line

$USER ALL=NOPASSWD:/usr/sbin/networksetup

to your /etc/sudoers file, where $USER is your username; so, the $USER will now be able to execute the command networksetup without needing to enter a password. The script checks the syntax with visudo to make sure that it doesn’t bork your /etc/sudoers file. So, it’s safe.

 

Demo

 

dhcp-toggle.gif

 

Credit

Thanks for version 2.0 goes to Pryley (Github | Alfred Forum) who opened a pull request for the added features and contributed some code.

 

Issues

Please report coding issues on the Github issue queue. Other support should be posted on the Alfred Forum thread.

Edited by Shawn Rice

Share this post


Link to post

A few things:

 

(1) Why did you rewrite this in Applescript?

(2) Why are you resetting the subnet mask/router/DNS?

 

Giving it the option to set an explicit manual IP is a good addition, but requiring that (or just requiring an argument) severely limits the convenience that the workflow provides. When I was using it often, I just used it without any argument to let me switch between manual and automatic, and the manually generated IP address worked 99% of the time, with that last 1% happening when the random IP inadvertently tried to grab on that was already in use, in which case, I just hit `dhcp man` again, and everything worked out.

Share this post


Link to post
  1. I rewrote it in Applescript to allow me to set a manual IP and the custom DNS server without having to enter a password twice.

  2. Setting a manual IP/Subnet/Router/DNS is needed often when a) you are not able to move closer to the router and B) the router is almost at capacity with connected devices or c) you are connecting to wifi through a weak extender. I have experienced the following scenarios on multiple occasions, on multiple wifi networks (especially in Europe).

  3. Also it doesn't make sense to me to use a 192.1.10 prefix as default as that is rarely used in most connections I've encountered (e.g. most Cisco routers on business networks use the 10.0.0 prefix for the gateway, D-Link/Netgear use 192.168.0, Linksys use 192.168.1, Belkin/SMC use 192.168.2, etc.)

Edited by pryley

Share this post


Link to post

from my standpoint to make this workflow complete there is one thing missing - turning off dhcp, which in effect switches off the ethernet connection. It's important when you have to switch from ethernet to WiFi occasionally while e.g. working in two independent networks. The problem is that you have to turn off the ethernet before switching to the WiFi network, otherwise the Mac will stick to the old dhcp settings. I'm not sure but most likely the following command should do the trick. Anyway, hopefully, you'll incorporate this very important functionality to your workflow.

 

networksetup -setv4off Ethernet

 

(I believe that after applying this instruction, using your workflow's commands such as: 'dhcp auto', 'dhcp man', etc. will switch ethernet on again.

 

I've also found a relevant topic here: http://osxdaily.com/2014/04/18/disable-ipv6-mac-os-x/

Edited by aharry

Share this post


Link to post

Currently, the workflow touches only the WiFi settings (see scripts/control.sh).

 

If you do have an ethernet plugged in, then the Mac will (almost) always prioritize ethernet over wifi, and, so, if you want to use DHCP settings on wifi, then you do need to disable ethernet or just unplug. So, the Mac won't actually "stick to the old dhcp settings" but will just stick use the ethernet connection and its settings.

 

You can change the priority of the connections (WiFi, Ethernet, VPN, etc...). For more information, see Apple's support page.

 

The command you're using won't actually turn Ethernet off; it'll just disable ipv4, leaving ipv6 on. If you want to turn it off, then you can use 

sudo ifconfig en0 down

and change the argument to `up` in order to turn it back on.

 

This feature request is, however, an edge case for the workflow. Switching the dhcp settings between routers makes much more sense when you're jumping between multiple networks with different router setups, which is something that happens much, much more often when you're not tethered via an ethernet cord. In the past, when I've needed to go between ethernet and wifi, I really just unplug the cable when I need the wifi and turn wifi off when I need the cable. I also am so incredibly swamped that I don't have time to code the edge case into something low priority (I no longer need to use this workflow because my networks have changed), but I will accept pull requests on the GH repo if you want to contribute. If so, the two relevant files to change are `scripts/script.sh` (the script filter) and `scripts/control.sh`, which is the controller where the actual commands go.

 

Put in a pull request, and I'll be happy to review it, integrate it, and credit you.

 

-- Shawn

Share this post


Link to post

The path is relative to the workflow root, and the workflow root is variable on each computer. The easiest way to find it is to open Alfred Preferences, navigate to the workflows tab, right-click (two-fingered or ctrl click) the workflow, and select "Show in Finder."

 

You can also find all the scripts in the github repo, and the relative paths will be the same.

Share this post


Link to post

Thanks, I've come to a conclusion that setting the priority of Internet connections is the best solution. Below I've provided a simple step-by-step do-it-yourself:

 

1. In system's settings choose Network,

 

2. Click "Set Service Order" as shown below:

5sxsU.png

 

 

3. Drag the connections into preferred order (the connection highest on the list gains the most priority):

oWkDl.png

 

4. Click OK to save the new settings.

 

That's all.

 

This works for me very well.

 

In case of other difficulties you may want to search for other solutions in this post: http://apple.stackexchange.com/questions/98815/how-does-the-mac-choose-which-connection-to-use-when-both-wifi-and-ethernet-are

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...