def _reboot(delay): '''This is the actual reboot command. Not recommended you call this directly. Use reboot() instead. ''' # TODO Do I need to loop on delay count and sleep(1) inside? sleep(delay) if testing: # TODO Return or set some value for testing debug("Simulating reboot", level=1) else: cmd('reboot')
def create(target): '''Creates a temp file in the format of /tmp/target.tmp and returns a file name ''' # Ignore any preceding path, we're going to create our file in /tmp. target_basename = target.split('/')[-1] temp_file_name = cmd('mktemp /tmp/' + target_basename + '.XXXXX.tmp')[0] debug("temp_file_name: '" + str(temp_file_name) + "'", level=1) return temp_file_name
def status(): '''Returns the status of NTP synchronization''' synched = False for row in cmd('timedatectl status')[0].split('\n'): debug("row: " + repr(row), level=1) match = re_search(r'^NTP synchronized: (.*)', row) if match: if match.group(1) == 'yes': synched = True break return synched
def get_psk(ssid, password): psk = None # Try to encrypt the password raw, stderr = cmd("sudo wpa_passphrase \"" + ssid + "\" \"" + password + "\"")[0:2] debug("wifi config() wpa_cli wpa_passphrase stderr: " + repr(stderr), level=1) for row in raw.split('\n'): match = re_search(r'^\s*psk=(.*)$', row) if match: psk = match.group(1) break return psk
def defaults(): '''Backup the config and set defaults''' cmd("wpa_cli remove_network 0") cmd("sudo wpa_cli save_config") timestamp = datetime.now().strftime('%Y-%m-%d.%H-%M') command = "cp " + conf_file + " " + conf_file + "." + timestamp + ".before" stderr = cmd(command)[1] debug("defaults() backup config command: " + repr(command), level=1) debug("defaults() backup config stderr: " + repr(stderr), level=1) # FIXME Change file name on GitHub command = "cp " + conf_file + ".defaults " + conf_file stderr = cmd(command)[1] debug("defaults() cat default config command: " + repr(command), level=1) debug("defaults() cat default config stderr: " + repr(stderr), level=1)
#!/usr/bin/env python3.5 import test_suite import lib.wifi import lib.debugging from json import loads from lib.cmd import cmd from lib.config import config from sys import argv, exit, stderr good = test_suite.good debug = lib.debugging.printmsg lib.wifi.defaults() check = "defaults()" conf_md5 = cmd("md5sum " + lib.wifi.conf_file)[0].split()[0] debug("conf_md5: " + repr(conf_md5), level=0) defaults_md5 = cmd("md5sum " + lib.wifi.conf_file + ".defaults")[0].split()[0] debug("defaults_md5: " + repr(defaults_md5), level=0) assert conf_md5 == defaults_md5, check good(check) try: wifi_config == argv[1] except KeyError: stderr.write('Usage: ' + __file__ + ' /path/to/wifi_config.json') exit(1) with open(wifi_config) as f: ssid, password = loads(f.read())
def config(ssid, passphrase = None, enc_type = None, hidden = False): '''Sets up our Wi-Fi network in /etc/wpa_supplicant/wpa_supplicant.conf''' # Backup and set defaults timestamp = datetime.now().strftime('%Y-%m-%d.%H-%M') cmd("cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf." + timestamp + ".before") cmd("cat /etc/wpa_supplicant/wpa_supplicant.empty.conf > /etc/wpa_supplicant/wpa_supplicant.conf") network = cmd("wpa_cli -i wlan0 add_network")[0] # Repeated command set_network = "wpa_cli -i wlan0 set_network " + network cmd(set_network + " ssid '\"" + ssid + "\"'") if hidden: cmd(set_network + " scan_ssid 1") if not enc_type: cmd(set_network + " key_mgmt NONE") cmd(set_network + " priority 100") if enc_type == 'WEP': # Don't use WEP boys and girls. We can't even encrypt the passphrase. cmd(set_network + " key_mgmt NONE") cmd(set_network + " wep_key0 " + psk) cmd(set_network + " wep_tx_keyidx 0") if enc_type == 'WPA': # Encrypted passphrase raw = cmd("wpa_passphrase \"" + ssid + "\" \"" + passphrase + "\"")[0] for row in # FINISH
def conn_strength(): '''The strength of our wlan0 connection''' return cmd('lib/wifi/get_conn_strength.sh')[0]
def ip(iface = 'wlan0'): '''Returns the IP of the interface''' return cmd('lib/wifi/get_IP.sh ' + iface)[0]
def isconnected(): '''See if we are connected to the wlan0 Wi-Fi network''' return cmd('lib/wifi/get_wifi_connected.sh')[1] == 0
def ssid(iface = 'wlan0'): '''Returns the ssid''' return cmd('sudo lib/wifi/get_SSID.sh ' + iface)[0]
for row in # FINISH root@raspberrypi:/SmartBird/lib# wpa_passphrase "Be:my:guest" "Guest:my:be" network={ ssid="Be:my:guest" #psk="Guest:my:be" psk=1f9a05de335e74e3edcc5950e6f8cc349fecbb3d1f44bb2db8862567f4db24ec } root@raspberrypi:/SmartBird/lib# wpa_passphrase "Be:my:guest" "Guest:my:be" | grep "^ psk=" root@raspberrypi:/SmartBird/lib# wpa_passphrase "Be:my:guest" "Guest:my:be" 2>&1 | grep "^ psk=" root@raspberrypi:/SmartBird/lib# wpa_passphrase "Be:my:guest" "Guest:my:be" 2>&1 | grep "^\tpsk=" root@raspberrypi:/SmartBird/lib# wpa_passphrase "Be:my:guest" "Guest:my:be" 2>&1 | grep "^\s*psk=" psk=1f9a05de335e74e3edcc5950e6f8cc349fecbb3d1f44bb2db8862567f4db24ec root@raspberrypi:/SmartBird/lib# cmd(set_network + " psk " + psk) # TODO Also set the country code. Currently US. cmd("wpa_cli enable_network " + network) cmd("wpa_cli save_config") cmd("cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf." + timestamp + ".after") def connect(): '''Connects to the network wlan0 is configured for''' cmd("/etc/init.d/networking restart") def ssid(iface = 'wlan0'): '''Returns the ssid'''
def setupLocal(): os.chdir(local_git) return cmd()
def config(ssid, password=None, enc_type=None, hidden=False): '''Sets up our Wi-Fi network in /etc/wpa_supplicant/wpa_supplicant.conf''' timestamp = datetime.now().strftime('%Y-%m-%d.%H-%M') defaults() stderr = cmd("sudo wpa_cli ap_scan 1")[1] debug("wifi config() wpa_cli ap_scan 1 stderr: " + repr(stderr), level=1) stderr = cmd("sudo wpa_cli add_network")[1] debug("wifi config() wpa_cli add_network stderr: " + repr(stderr), level=1) # Frequently-repeated command set_network = "sudo wpa_cli -i wlan0 set_network 0 " stderr = cmd(set_network + " ssid '\"" + ssid + "\"'") if hidden: stderr = cmd(set_network + " scan_ssid 1")[1] debug("wifi config() wpa_cli scan_ssid 1 stderr: " + repr(stderr), level=1) if not enc_type: stderr = cmd(set_network + " key_mgmt NONE")[1] debug("wifi config() wpa_cli key_mgmt NONE 1 stderr: " + repr(stderr), level=1) if enc_type == 'wep': # Don't use WEP boys and girls. We can't even encrypt the password. # FIXME Can't we? stderr = cmd(set_network + " key_mgmt NONE")[1] debug("wifi config() wpa_cli key_mgmt NONE stderr: " + repr(stderr), level=1) stderr = cmd(set_network + " wep_key0 " + get_psk(ssid, password))[1] debug("wifi config() wpa_cli wep_key0 stderr: " + repr(stderr), level=1) stderr = cmd(set_network + " wep_tx_keyidx 0")[1] debug("wifi config() wpa_cli wep_tx_keyidx stderr: " + repr(stderr), level=1) if enc_type in ['wpa', 'wpa2']: # TODO What if it is None stderr = cmd(set_network + " psk " + get_psk(ssid, password))[1] debug("wifi config() wpa_cli psk stderr: " + repr(stderr), level=1) # TODO Also set the country code. Currently US. stderr = cmd("sudo wpa_cli enable_network 0")[1] debug("wifi config() wpa_cli enable_network stderr: " + repr(stderr), level=1) stderr = cmd("sudo wpa_cli select_network 0")[1] debug("wifi config() wpa_cli select_network stderr: " + repr(stderr), level=1) stderr = cmd("sudo wpa_cli save_config")[1] debug("wifi config() wpa_cli save_config stderr: " + repr(stderr), level=1) # Do a .after backup as well stderr = cmd("sudo cp " + conf_file + " " + conf_file + "." + timestamp + ".after")[1] debug("wifi config() Backup config stderr: " + repr(stderr), level=1)
def connect(): '''Connects to the network wlan0 is configured for''' stderr = cmd("/etc/init.d/networking restart")[1] debug("connect(): " + repr(stderr), level=1)
def setupLocal(): os.chdir(local_git) return cmd()
def connect(): '''Connects to the network wlan0 is configured for''' cmd("/etc/init.d/networking restart")
import lib.debugging from json import loads from lib.cmd import cmd from lib.system import SystemCls system = SystemCls() good = test_suite.good debug = lib.debugging.printmsg with open('/SmartBird/config/version.json') as f: version = loads(f.read()) check = "system.version correct" assert system.version == version, check good(check) check = "system.attached_devices" assert len(system.attached_devices) >= 1, check good(check) check = "door in devices" assert 'door' in system.attached_devices, check good(check) debug("cmd('lib/get_serial.sh'): " + repr(cmd('lib/get_serial.sh'))) debug("system.serial: " + repr(system.serial)) check = "serial" assert cmd('lib/get_serial.sh')[0] == system.serial, check good(check)