jphphotography

rc.local V0.4

Sep 2nd, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.03 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. _wlanexist=$(ifconfig | grep wlan) || true
  17.  
  18. if [ "$_IP" ]; then
  19.   printf "My IP address is %s\n" "$_IP"
  20. fi
  21.  
  22. # RPi Network Conf Bootstrapper
  23.  
  24. createAdHocNetwork(){
  25.     echo "Starting AP Mode"
  26.     echo "Downing wlan0"
  27.     sudo ifconfig wlan0 down
  28.     sleep 1
  29.     echo "Stopping wpa_supplicant"
  30.     sudo killall -9 wpa_supplicant
  31.     sleep 1
  32.     echo "Starting hostapd"
  33.     sudo /etc/init.d/hostapd start
  34.     sleep 1
  35.     echo "Configuring wlan0 IP"
  36.     sudo ifconfig wlan0 10.10.10.1
  37.     sleep 1
  38.     echo "Starting dhcpd on wlan0"
  39.     sudo /usr/sbin/dhcpd wlan0
  40.     echo "Please connect to access point PiFi_Mini_AP"
  41.     echo "Password is pifimini00"
  42.     echo "Enjoy"
  43.     ./etc/airplay_monitor.sh& #start script to monitor airplay and pause mpc when user is connected
  44.     #Uncomment the line below if using an LCD and the PiFi LCD python scripts
  45.     #python /etc/mpc_lcd_info.py& #start lcd script that displays mpd info
  46.     #Uncomment the line below if using buttons
  47.     #python /etc/buttons.py& #start button script
  48.     echo "AP network created"
  49.     echo "The server ip is 10.10.10.1"
  50. }
  51.  
  52. echo "======================================================="
  53. echo "PiFi Network Conf Bootstrapper V0.4"
  54. echo "======================================================="
  55. echo "===Based on script by Lasse Christiansen at lcdev.dk==="
  56. echo "Checking if USB Wifi card is present"
  57. connected=false
  58. if [ "$_wlanexist" ]; then
  59.         echo "USB Wifi is present, scanning for known WiFi networks"
  60.         for i in 1 # Initially was for i in 1 2 3 but found it took too long to failover to adhoc
  61.         do
  62.          if connected=false
  63.                 then
  64.                 echo "Starting supplicant for WPA/WPA2"
  65.                 wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
  66.                 echo "Obtaining IP from DHCP"
  67.                 if dhclient -1 wlan0
  68.                 then
  69.                     echo "Connected to WiFi"
  70.                     connected=true
  71.                     iwconfig 2> /dev/null | grep ESSID > /etc/wpa_supplicant/my_current_ssid.txt
  72.                     cat /etc/wpa_supplicant/my_current_ssid.txt
  73.                     echo "My IP address is:"
  74.                     hostname -I
  75.                     ./etc/airplay_monitor.sh& #start script to monitor airplay and pause mpc when user is connected
  76.                     #Uncomment the next line if you're using an LCD, make sure you have created the python scripts first though
  77.                     #python /etc/mpc_lcd_info.py& #start lcd script that displays mpd info
  78.                     #Uncomment the line below if using buttons
  79.                     #python /etc/buttons.py& #start button script
  80.                     break
  81.                 else
  82.                     echo "DHCP server did not respond with an IP lease (DHCPOFFER)"
  83.                     wpa_cli terminate
  84.                     break
  85.                 fi
  86.             else
  87.                 echo "No ssid's listed in your wpa_supplicant.conf files were in range"
  88.             fi
  89.         done
  90. else
  91.         echo "No wireless cards detected, trying wired connection"
  92.         if dhclient -1 eth0
  93.         then
  94.                 connected=true
  95.                 echo "Connected on ETH0"
  96.                 if [ "$_IP" ]; then
  97.                         printf "My IP address is %s\n" "$_IP"
  98.                 else
  99.                         echo "Unable to obtain IP on ETH0"
  100.                 fi
  101.         fi
  102. fi
  103.  
  104. if ! $connected; then
  105.         if [ "$_wlanexist" ]; then
  106.                 createAdHocNetwork
  107.         else
  108.                 echo "No wireless or wired connections could be established"
  109.                 sudo python /etc/no_connection_lcd.py&
  110.         fi
  111. fi
  112.  
  113. exit 0
Advertisement
Add Comment
Please, Sign In to add comment