Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to generate /etc/dhcpcd.conf for static IP on Raspbian Raspberry Pi
#!/bin/bash -e
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
@RichardBronosky

This comment has been minimized.

Copy link
Owner Author

@RichardBronosky RichardBronosky commented Oct 26, 2017

Based on https://raspberrypi.stackexchange.com/a/37921/8375

Sample output:

interface wlan0
static ip_address=10.1.10.136/24
static routers=10.1.10.1
static domain_name_servers=75.75.75.75 75.75.76.76 2001:558:feed::1 2001:558:feed::2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment