Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Last active August 26, 2016 19:33
Show Gist options
  • Save wturnerharris/5a0353ff4841d4e039ca to your computer and use it in GitHub Desktop.
Save wturnerharris/5a0353ff4841d4e039ca to your computer and use it in GitHub Desktop.
Multi-user runlevel script (rc.local) for rPi #1
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# Wait for the TV-screen to be turned on...
while ! $( tvservice --dumpedid /tmp/edid | fgrep -qv 'Nothing written!' ); do
bHadToWaitForScreen=true;
printf "===> Screen is not connected, off or in an unknown mode, waiting for it to become available...\n"
sleep 10;
done;
printf "===> Screen is on, extracting preferred mode...\n"
_DEPTH=32;
_XRES=1080
_YRES=1920
_GROUP=CEA
_MODE=16
#eval $( edidparser /tmp/edid | fgrep 'preferred mode' | tail -1 | sed -Ene 's/^.+(DMT|CEA) \(([0-9]+)\) ([0-9]+)x([0-9]+)[pi]? @.+/_GROUP=\1;_MODE=\2;_XRES=\3;_YRES=\4;/p' );
#printf "===> Resetting screen to preferred mode: %s-%d (%dx%dx%d)...\n" $_GROUP $_MODE $_XRES $_YRES $_DEPTH
tvservice --explicit="$_GROUP $_MODE"
sleep 1;
printf "===> Resetting frame-buffer to %dx%dx%d...\n" $_XRES $_YRES $_DEPTH
fbset --all --geometry $_XRES $_YRES $_XRES $_YRES $_DEPTH -left 0 -right 0 -upper 0 -lower 0;
sleep 1;
echo "===> Ethernet going down."
ifdown eth0
ifconfig eth0 10.0.2.1 netmask 255.255.255.0
echo "===> Configuring ethernet forwarding..."
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
if [ -f /boot/xinitrc ]; then
echo "===> Linking xinitrc and starting X...";
ln -fs /boot/xinitrc /home/pi/.xinitrc;
su - pi -c 'startx' &
fi
exit 0
@wturnerharris
Copy link
Author

For lines 43-49, the configuration is forwarding its network connection from Wifi, which receives the internet, to the ethernet port. The client machine needs to replace those lines with:

echo "===> Configuring the network\n"
echo "nameserver 8.8.8.8" > /etc/resolv.conf

Both machines are configured with static addresses.

@wturnerharris
Copy link
Author

Client /etc/network/interfaces

auto lo

iface lo inet loopback
iface eth0 inet static

address 10.0.2.3
netmask 255.255.255.0
broadcast 10.0.2.255
gateway 10.0.2.1

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Host /etc/network/interfaces

auto wlan0

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet static
address 10.0.1.230
netmask 255.255.255.0
gateway 10.0.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

@wturnerharris
Copy link
Author

Find IP/MAC address

In case you have to re-connect the WiFi to a different network and your only means is to ssh over ethernet, you might need to find out the IP and/or MAC address of the device over ethernet. While good ol arp utility could get the job done, I ended up using another similar utility called arp-scan, which is more robust in that it actually does the scanning and pinging directly within the utility whereas arp can only return known addresses of devices that recently connected. You can get around that by pinging every device in the range of IP addresses on the network, but that is exhausting.

  1. Download arp-scan:
    brew install arp-scan
  2. Scan the network, looking for mac addresses registered to Raspberry Pi Foundation:
    arp-scan --interface=en0 --localnet | grep Raspberry

Profit!

Additional Considerations

I should also note that if you know the networking device's manufacturer, then you could search with arp and grep for the manufacturer's prefix code, which is the first 24 bits (or the first six digits excluding the colons) of the address. This could narrow things down for you. This might mean searching for possible manufacturer codes using the IEEE registry, which shows that Raspberry Pi Foundation is registered with the b8:27:eb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment