jphphotography

PiFi rc.local V0.2

May 22nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.85 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # rc.local
  4. #
  5. # This script is executed at the end of each multiuser runlevel.
  6. # Make sure that the script will "exit 0" on success or any other
  7. # value on error.
  8. #
  9. # In order to enable or disable this script just change the execution
  10. # bits.
  11. #
  12. # By default this script does nothing.
  13.  
  14. # Print the IP address
  15. _IP=$(hostname -I) || true
  16. if [ "$_IP" ]; then
  17.   printf "My IP address is %s\n" "$_IP"
  18. fi
  19.  
  20. # RPi Network Conf Bootstrapper
  21.  
  22. createAdHocNetwork(){
  23.     echo "Starting AP Mode"
  24.     echo "Downing wlan0"
  25.     sudo ifconfig wlan0 down
  26.     sleep 1
  27.     echo "Stopping wpa_supplicant"
  28.     sudo killall -9 wpa_supplicant
  29.     sleep 1
  30.     echo "Starting hostapd"
  31.     sudo /etc/init.d/hostapd start
  32.     sleep 1
  33.     echo "Configuring wlan0 IP"
  34.     sudo ifconfig wlan0 10.10.10.1
  35.     sleep 1
  36.     echo "Starting dhcpd on wlan0"
  37.     sudo /usr/sbin/dhcpd wlan0
  38.     echo "Please connect to access point PiFi_Mini_AP"
  39.     echo "Password is pifimini00"
  40.     echo "Enjoy"
  41.     ./etc/airplay_monitor.sh& #start script to monitor airplay and pause mpc when user is connected
  42.     #Uncomment the line below if using an LCD and the PiFi LCD python scripts
  43.     #python /etc/mpc_lcd_info.py& #start lcd script that displays mpd info
  44.     echo "AP network created"
  45.     echo "The server ip is 10.10.10.1"
  46. }
  47.  
  48. echo "======================================================="
  49. echo "PiFi Network Conf Bootstrapper V0.2"
  50. echo "======================================================="
  51. echo "===Based on script by Lasse Christiansen at lcdev.dk==="
  52. echo "Scanning for known WiFi networks"
  53. connected=false
  54. for i in 1 # Initially was for i in 1 2 3 but found it took too long to failover to adhoc
  55. do
  56.  if connected=false
  57.         then
  58.         echo "Starting supplicant for WPA/WPA2"
  59.         wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
  60.         echo "Obtaining IP from DHCP"
  61.         if dhclient -1 wlan0
  62.         then
  63.             echo "Connected to WiFi"
  64.             connected=true
  65.             iwconfig 2> /dev/null | grep ESSID > /etc/wpa_supplicant/my_current_ssid.txt
  66.             cat /etc/wpa_supplicant/my_current_ssid.txt
  67.             echo "My IP address is:"
  68.             hostname -I
  69.             ./etc/airplay_monitor.sh& #start script to monitor airplay and pause mpc when user is connected
  70.             #Uncomment the next line if you're using an LCD, make sure you have created the python scripts first though
  71.             #python /etc/mpc_lcd_info.py& #start lcd script that displays mpd info
  72.             break
  73.         else
  74.             echo "DHCP server did not respond with an IP lease (DHCPOFFER)"
  75.             wpa_cli terminate
  76.             break
  77.         fi
  78.     else
  79.         echo "No ssid's listed in your wpa_supplicant.conf files were in range"
  80.     fi
  81. done
  82.  
  83. if ! $connected; then
  84.     createAdHocNetwork
  85. fi
  86.  
  87. exit 0
Advertisement
Add Comment
Please, Sign In to add comment