Guest User

Untitled

a guest
Jan 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #!/bin/bash
  2. # Automatically setup routing and DNS for a PiZero connected over a USB-network
  3. # NOTE: Before running this script for the first time, you need to run the
  4. # following two commands on your Linux PC
  5. # sudo sysctl -w net.ipv4.ip_forward=1
  6. # sudo iptables -t nat -A POSTROUTING -s 169.254.0.0/16 -o eth0 -j MASQUERADE
  7. # (replace eth0 in the second command with your internet-facing network device,
  8. # e.g. wlan0 on a laptop)
  9.  
  10. # The Avahi-discovered hostname
  11. ZERO_HOSTNAME=raspberrypi.local
  12. # The SSH user
  13. ZERO_USERNAME=pi
  14. # The USB network device on the Zero-side (will always be usb0)
  15. ZERO_DEV=usb0
  16.  
  17. # The USB network device on the PC-side (will probably be usb0)
  18. PC_ZERO_DEV=usb0
  19.  
  20. # The internet-connected network device on the PC (will probably be eth0 or wlan0)
  21. PC_INET_DEV=$(route | grep ^default | awk '{print $8}')
  22. echo "PC_INET_DEV is $PC_INET_DEV"
  23.  
  24. ifconfig $PC_ZERO_DEV > /dev/null 2>&1
  25. if [[ $? -ne 0 ]]; then
  26. echo "PC_ZERO_DEV ($PC_ZERO_DEV) doesn't exist"
  27. exit 1
  28. fi
  29.  
  30. ifconfig $PC_INET_DEV > /dev/null 2>&1
  31. if [[ $? -ne 0 ]]; then
  32. echo "PC_INET_DEV ($PC_INET_DEV) doesn't exist"
  33. exit 1
  34. fi
  35.  
  36. DNS_SERVER=$(nmcli -t -f IP4 device list iface $PC_INET_DEV | grep DNS | head -1 | cut -d: -f2)
  37. echo "DNS_SERVER is $DNS_SERVER"
  38.  
  39. # The IP address assigned to the PC-side of the USB network device
  40. PC_ZERO_DEV_IP=$(ifconfig $PC_ZERO_DEV | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}')
  41. echo "PC_ZERO_DEV_IP is $PC_ZERO_DEV_IP"
  42. if [[ "$PC_ZERO_DEV_IP" != 169.254.* ]]; then
  43. echo "PC_ZERO_DEV_IP isn't in the link-local range"
  44. exit 1
  45. fi
  46.  
  47. # The IP address assigned to Zero-side of the USB network device
  48. ZERO_IP=$(ping -c1 $ZERO_HOSTNAME | grep $ZERO_HOSTNAME | head -1 | cut -d'(' -f2 | cut -d')' -f1)
  49. echo "ZERO_IP is $ZERO_IP"
  50. if [[ "$ZERO_IP" != 169.254.* ]]; then
  51. echo "ZERO_IP isn't in the link-local range"
  52. exit 1
  53. fi
  54.  
  55. # Setup default route and DNS server on Zero
  56. ssh $ZERO_USERNAME@$ZERO_HOSTNAME "sudo route add default gw $PC_ZERO_DEV_IP $ZERO_DEV; echo \"nameserver $DNS_SERVER\" | sudo resolvconf -a $ZERO_DEV"
Add Comment
Please, Sign In to add comment