Skip to content

Instantly share code, notes, and snippets.

@pkorac
Last active September 13, 2024 19:16
Show Gist options
  • Save pkorac/03e0a4ad69b788e7dc47383bc0155fd7 to your computer and use it in GitHub Desktop.
Save pkorac/03e0a4ad69b788e7dc47383bc0155fd7 to your computer and use it in GitHub Desktop.
Show local IP and copy to Clipboard

Print & Copy Local IP

Grab your local IP when developing web-apps and paste it in your browser/mobile

This script will print out your local IP and copy it to clipboard. When serving/developing a web-project with Nuxt for example, You can then paste this url into your phone and test it on the device.

The url that gets printed/copied looks like so: http://192.168.0.1:3000

Installation

The easyest thing to do is to create a file in /usr/bin (you might need sudo permissions) Then make it executable with chmod +x YOUR_FILE_NAME.sh (you might need sudo permissions) All done!

#!/bin/bash
# Get the local IP address
local_ip=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -n 1)
# Construct the full URL
full_url="http://$local_ip:3000"
# Copy the full URL to clipboard
echo "$full_url" | pbcopy
# Display the IP address and full URL
echo "Your local IP address is: $local_ip"
echo
echo "Full URL for your Nuxt app:"
echo "$full_url"
echo
echo "The full URL has been copied to your clipboard."
echo "You can now paste it directly into your iPhone's browser to test your Nuxt web-page."
echo
echo "Remember to start your Nuxt app with: npm run dev -- --host"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment