├── .gitignore ├── README.md ├── RaspberryPiWifiSetup.py └── createwificonfig.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .idea/ 3 | 4 | interfaces 5 | wpa_supplicant.conf 6 | 7 | ### OSX template 8 | .DS_Store 9 | .AppleDouble 10 | .LSOverride 11 | 12 | # Icon must end with two \r 13 | Icon 14 | 15 | # Thumbnails 16 | ._* 17 | 18 | # Files that might appear in the root of a volume 19 | .DocumentRevisions-V100 20 | .fseventsd 21 | .Spotlight-V100 22 | .TemporaryItems 23 | .Trashes 24 | .VolumeIcon.icns 25 | 26 | # Directories potentially created on remote AFP share 27 | .AppleDB 28 | .AppleDesktop 29 | Network Trash Folder 30 | Temporary Items 31 | .apdisk 32 | ### JetBrains template 33 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 34 | 35 | *.iml 36 | 37 | ## Directory-based project format: 38 | # if you remove the above rule, at least ignore the following: 39 | 40 | # User-specific stuff: 41 | # .idea/workspace.xml 42 | # .idea/tasks.xml 43 | # .idea/dictionaries 44 | 45 | # Sensitive or high-churn files: 46 | # .idea/dataSources.ids 47 | # .idea/dataSources.xml 48 | # .idea/sqlDataSources.xml 49 | # .idea/dynamic.xml 50 | # .idea/uiDesigner.xml 51 | 52 | # Gradle: 53 | # .idea/gradle.xml 54 | # .idea/libraries 55 | 56 | # Mongo Explorer plugin: 57 | # .idea/mongoSettings.xml 58 | 59 | ## File-based project format: 60 | *.ipr 61 | *.iws 62 | 63 | ## Plugin-specific files: 64 | 65 | # IntelliJ 66 | /out/ 67 | 68 | # mpeltonen/sbt-idea plugin 69 | .idea_modules/ 70 | 71 | # JIRA plugin 72 | atlassian-ide-plugin.xml 73 | 74 | # Crashlytics plugin (for Android Studio and IntelliJ) 75 | com_crashlytics_export_strings.xml 76 | crashlytics.properties 77 | crashlytics-build.properties 78 | ### Python template 79 | # Byte-compiled / optimized / DLL files 80 | __pycache__/ 81 | *.py[cod] 82 | *$py.class 83 | 84 | # C extensions 85 | *.so 86 | 87 | # Distribution / packaging 88 | .Python 89 | env/ 90 | build/ 91 | develop-eggs/ 92 | dist/ 93 | downloads/ 94 | eggs/ 95 | .eggs/ 96 | lib/ 97 | lib64/ 98 | parts/ 99 | sdist/ 100 | var/ 101 | *.egg-info/ 102 | .installed.cfg 103 | *.egg 104 | 105 | # PyInstaller 106 | # Usually these files are written by a python script from a template 107 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 108 | *.manifest 109 | *.spec 110 | 111 | # Installer logs 112 | pip-log.txt 113 | pip-delete-this-directory.txt 114 | 115 | # Unit test / coverage reports 116 | htmlcov/ 117 | .tox/ 118 | .coverage 119 | .coverage.* 120 | .cache 121 | nosetests.xml 122 | coverage.xml 123 | *,cover 124 | 125 | # Translations 126 | *.mo 127 | *.pot 128 | 129 | # Django stuff: 130 | *.log 131 | 132 | # Sphinx documentation 133 | docs/_build/ 134 | 135 | # PyBuilder 136 | target/ 137 | ### Windows template 138 | # Windows image file caches 139 | Thumbs.db 140 | ehthumbs.db 141 | 142 | # Folder config file 143 | Desktop.ini 144 | 145 | # Recycle Bin used on file shares 146 | $RECYCLE.BIN/ 147 | 148 | # Windows Installer files 149 | *.cab 150 | *.msi 151 | *.msm 152 | *.msp 153 | 154 | # Windows shortcuts 155 | *.lnk 156 | 157 | **/.AppleDouble 158 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Raspberry Pi Wifi Setup 3 | ======================== 4 | 5 | Project to help create the interfaces and wpa_supplicant.conf files 6 | necessary to setup wifi on a Raspberry pi. 7 | 8 | After spending many frustrating hours trying to get the right magic incantation 9 | necessary to make this work - after I finally got what I believe to be a 10 | repeatable process - I thought I would script it so I can generate the files at will. 11 | 12 | This has worked for me on a model B Raspberry Pi. 13 | 14 | There are 2 files: 15 | 16 | RaspberryPiWifiSetup.py 17 | 18 | This class has a single static method that takes a ssid and an optional password, 19 | and produces strings for the interface and wpa_supplicant files. It assumes WPA 20 | security, at the moment, but the ssid can be either broadcast or hidden. If no 21 | password is given, it assume you are trying to connect to an open wifi network. 22 | 23 | createwificonfig.py 24 | 25 | This is a script that takes command line arguments for the ssid and pwd and then 26 | generates actual files. 27 | 28 | Usage: 29 | 30 | python createwificonfig.py --ssid="my ssid" --pwd="my password" 31 | 32 | I hope you find it useful. 33 | 34 | -------------------------------------------------------------------------------- /RaspberryPiWifiSetup.py: -------------------------------------------------------------------------------- 1 | __author__ = 'youngsoul' 2 | import string 3 | 4 | interfaces_template = """ 5 | 6 | auto lo 7 | iface lo inet loopback 8 | allow-hotplug eth0 9 | iface eth0 inet dhcp 10 | 11 | allow-hotplug wlan0 12 | auto wlan0 13 | iface wlan0 inet dhcp 14 | \tpre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B 15 | iface default inet dhcp 16 | 17 | """ 18 | 19 | wpa_supplicant_template2 = """ 20 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 21 | update_config=1 22 | ap_scan=2 23 | network={ 24 | ssid="$network_ssid" 25 | scan_ssid=1 26 | proto=RSN 27 | pairwise=CCMP 28 | key_mgmt=WPA-PSK 29 | psk="$network_password" 30 | } 31 | """ 32 | 33 | wpa_supplicant_template = """ 34 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 35 | update_config=1 36 | network={ 37 | ssid="$network_ssid" 38 | psk="$network_password" 39 | } 40 | """ 41 | 42 | open_wpa_supplicant_template = """ 43 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 44 | update_config=1 45 | ap_scan=2 46 | eapol_version=1 47 | network={ 48 | ssid="$network_ssid" 49 | scan_ssid=1 50 | key_mgmt=NONE 51 | } 52 | """ 53 | 54 | 55 | class RaspberryPiWifiSetup: 56 | 57 | @classmethod 58 | def create_wifi_configurations(cls, network_ssid, network_password=None): 59 | ''' 60 | Create the interfaces file content and wpa_supplicant file content 61 | given an network ssid and password. For a password protected network 62 | the SSID can be hidden. 63 | :param network_ssid: required parameter. SSID of the wifi network 64 | :param network_password: optional parameter. If not specified, 65 | then it is assumed to be an open network. 66 | If it is specified, it is assumed to be the 67 | network password 68 | :return: 69 | ''' 70 | intefaces_content = interfaces_template 71 | if not network_password: 72 | supplicant_content = string.Template(open_wpa_supplicant_template).substitute({"network_ssid": network_ssid}) 73 | else: 74 | supplicant_content = string.Template(wpa_supplicant_template).substitute({"network_ssid": network_ssid, "network_password": network_password}) 75 | 76 | return intefaces_content, supplicant_content 77 | 78 | if __name__ == '__main__': 79 | (i,w) = RaspberryPiWifiSetup.create_wifi_configurations("foo","bar") 80 | print i 81 | print w 82 | 83 | (a,b) = RaspberryPiWifiSetup.create_wifi_configurations("baz") 84 | print a 85 | print b -------------------------------------------------------------------------------- /createwificonfig.py: -------------------------------------------------------------------------------- 1 | __author__ = 'youngsoul' 2 | 3 | #from RaspberryPiWifiSetup import RaspberryPiWifiSetup 4 | 5 | import sys 6 | import getopt 7 | import os 8 | import string 9 | import platform 10 | 11 | script_dir = os.path.dirname(os.path.abspath(__file__)) 12 | 13 | 14 | interfaces_template = """ 15 | 16 | auto lo 17 | iface lo inet loopback 18 | allow-hotplug eth0 19 | iface eth0 inet dhcp 20 | 21 | allow-hotplug wlan0 22 | auto wlan0 23 | iface wlan0 inet dhcp 24 | \tpre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B 25 | iface default inet dhcp 26 | 27 | """ 28 | 29 | wpa_supplicant_template2 = """ 30 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 31 | update_config=1 32 | ap_scan=2 33 | network={ 34 | ssid="$network_ssid" 35 | scan_ssid=1 36 | proto=RSN 37 | pairwise=CCMP 38 | key_mgmt=WPA-PSK 39 | psk="$network_password" 40 | } 41 | """ 42 | 43 | wpa_supplicant_template = """ 44 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 45 | update_config=1 46 | network={ 47 | ssid="$network_ssid" 48 | psk="$network_password" 49 | } 50 | """ 51 | 52 | open_wpa_supplicant_template = """ 53 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 54 | update_config=1 55 | ap_scan=2 56 | eapol_version=1 57 | network={ 58 | ssid="$network_ssid" 59 | scan_ssid=1 60 | key_mgmt=NONE 61 | } 62 | """ 63 | 64 | 65 | class RaspberryPiWifiSetup: 66 | 67 | @classmethod 68 | def create_wifi_configurations(cls, network_ssid, network_password=None): 69 | ''' 70 | Create the interfaces file content and wpa_supplicant file content 71 | given an network ssid and password. For a password protected network 72 | the SSID can be hidden. 73 | :param network_ssid: required parameter. SSID of the wifi network 74 | :param network_password: optional parameter. If not specified, 75 | then it is assumed to be an open network. 76 | If it is specified, it is assumed to be the 77 | network password 78 | :return: 79 | ''' 80 | intefaces_content = interfaces_template 81 | if not network_password: 82 | supplicant_content = string.Template(open_wpa_supplicant_template).substitute({"network_ssid": network_ssid}) 83 | else: 84 | supplicant_content = string.Template(wpa_supplicant_template).substitute({"network_ssid": network_ssid, "network_password": network_password}) 85 | 86 | return intefaces_content, supplicant_content 87 | 88 | 89 | def _usage(): 90 | print("createwificonfig.py [-s|--ssid <>] [-p|--pwd <>]") 91 | 92 | 93 | def write_file(the_file, the_data): 94 | with open(the_file, "wt") as f: 95 | f.write(the_data) 96 | 97 | if __name__ == "__main__": 98 | argv = sys.argv[1:] 99 | 100 | try: 101 | opts, args = getopt.getopt(argv, "h", ["ssid=", "pwd="]) 102 | except getopt.GetoptError: 103 | _usage() 104 | sys.exit(2) 105 | 106 | if len(opts) == 0: 107 | _usage() 108 | sys.exit() 109 | 110 | pwd = None 111 | for opt, arg in opts: 112 | #print("opt: " + opt) 113 | if opt == '-h': 114 | _usage() 115 | sys.exit() 116 | elif opt in ("-s", "--ssid"): 117 | ssid = arg 118 | elif opt in ("-p", "--pwd"): 119 | pwd = arg 120 | 121 | 122 | print("ssid: " + str(ssid)) 123 | print("pwd: " + str(pwd)) 124 | 125 | (iface, supplicant ) = RaspberryPiWifiSetup.create_wifi_configurations(ssid,pwd) 126 | #print(iface) 127 | #print(supplicant) 128 | 129 | write_file(script_dir + "/interfaces", iface) 130 | write_file(script_dir + "/wpa_supplicant.conf", supplicant) 131 | 132 | print("Execute the following commands to setup WIFI configuration....") 133 | print("sudo rm /var/run/wpa_supplicant/wlan0") 134 | print("sudo pkill -f wpa_supplicant.conf") 135 | print("sudo cp ./interfaces /etc/network/") 136 | print("sudo cp ./wpa_supplicant.conf /etc/wpa_supplicant/") 137 | print("sudo reboot") 138 | 139 | # if we are on the raspberry pi, ask if we should execute the commands now 140 | if platform.system() == "Linux": 141 | prompt = "Would you like to execute the above commands now? y/N" 142 | ans_response = raw_input(prompt).strip() 143 | if ans_response == "y" or ans_response == "Y": 144 | os.system("sudo rm /var/run/wpa_supplicant/wlan0") 145 | os.system("sudo pkill -f wpa_supplicant.conf") 146 | os.system("sudo cp ./interfaces /etc/network/") 147 | os.system("sudo cp ./wpa_supplicant.conf /etc/wpa_supplicant/") 148 | os.system("sudo rm -f ./interfaces") 149 | os.system("sudo rm -f ./wpa_supplicant.conf") 150 | os.system("sudo reboot") 151 | 152 | 153 | --------------------------------------------------------------------------------