Automate getting my IP and copying to clipboard

What is the easiest way to do this on a Mac? I just need to get my current IP and copy it to clipboard? I can do a bit of Python and bash, but never used AppleScript.

It would be nice if it used things that might set me on an interesting path to learning more about automation.

So I am realising that I am about to enter the world of automation, and there are many options.

The Python seems to be easy enough:

import socket    
hostname = socket.gethostname()    
IPAddr = socket.gethostbyname(hostname)    
print("Your Computer Name is:" + hostname)    
print("Your Computer IP Address is:" + IPAddr)

so… how do I get the output onto the clipboard?

Do you want the IP address of your computer, or the IP address of your computer on the internet (through your ISP)?

Edit: these are shell commands.

From your ISP:

curl https://www.myip.com/ | egrep -o "span.+?\"ip.+?[0-9.]+.+?span" | egrep -o "[0-9.]+" | pbcopy

Your IP address on your network could have multiple answers. You can expand on this code if you want to narrow it down to one answer.

ifconfig | grep inet | egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | egrep -v "127.0.0.1" | egrep -v "\.255$" | pbcopy

Ah - it was pbcopy that I wanted.

So I can do a text expander shortcut to write that to terminal. How might a go a step further and automate:

  • opening terminal
  • writing that to it?

Or is there a better way?

Never used TE, but looks like you can execute a shell command:

https://textexpander.com/help/desktop/applescript.html

So, the Apple Script (for copying to clipboard, no need to run anything from terminal) seems to be:

set theIP to do shell script "curl ifconfig.co"
set the clipboard to theIP

I can add this to Text Expander.

Very happy!

1 Like

or automate it through Keyboard Maestro with a shortcut to send it to your clipboard from wherever you are? TE implies a text entry option, there might not be one.

Agree. KM makes it easy with its %MacIPAddress% token.

2 Likes

Alfred can do that! You can find the workflow here.

Just type “ip” in Alfred search bar and voila!

2 Likes

Thanks, that’s also a great solution. FYI, your link (1.0.0.) didn’t actually work, but v1.0.3. on GitHub worked

1 Like