Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
Forked from RichardBronosky/static_ip.sh
Last active October 5, 2020 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shagamemnon/6b161003e0cbc94badd37a28b05b3deb to your computer and use it in GitHub Desktop.
Save shagamemnon/6b161003e0cbc94badd37a28b05b3deb to your computer and use it in GitHub Desktop.
Script to generate /etc/dhcpcd.conf for static IP on Raspbian Raspberry Pi
#!/bin/bash -e
sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
if [[ $# -ne 0 ]]; then
cat<<EOF
Usage:
$0
DEV=eth0 $0
EOF
exit 1
fi
dev=${DEV:-wlan0}
ip -4 addr show dev $dev | awk -v dev=$dev '/inet/ {ip=$2} END{print "interface "dev; print "static ip_address="ip}'
ip route | grep default | awk '{print "static routers="$3}'
awk '/nameserver/{dns=dns" "$2} END {sub(/^ */,"",dns); print "static domain_name_servers="dns}' < /etc/resolv.conf
# vim: et sw=4 ts=4 sts=4 syntax=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment