From c607860052210dcfe24b0e326504f5666b685b0b Mon Sep 17 00:00:00 2001 From: cimryan Date: Wed, 24 Oct 2018 22:33:51 -0700 Subject: [PATCH] Extract common functionality to WpaSupplicantConf.psm1 --- doc/GetShellWithoutMonitorOnWindows.md | 1 + setup/windows/WpaSupplicantConf.psm1 | 91 +++++++++++++++++++++ setup/windows/add-wifi.ps1 | 17 +--- setup/windows/setup-piForHeadlessConfig.ps1 | 25 +----- 4 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 setup/windows/WpaSupplicantConf.psm1 diff --git a/doc/GetShellWithoutMonitorOnWindows.md b/doc/GetShellWithoutMonitorOnWindows.md index 83aa360..0142aeb 100644 --- a/doc/GetShellWithoutMonitorOnWindows.md +++ b/doc/GetShellWithoutMonitorOnWindows.md @@ -15,6 +15,7 @@ ``` cd ~ wget https://raw.githubusercontent.com/cimryan/teslausb/master/setup/windows/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 + wget https://raw.githubusercontent.com/cimryan/teslausb/master/setup/windows/WpaSupplicantConf.psm1 -OutFile WpaSupplicantConf.psm1 ./setup-piForHeadlessConfig.ps1 -Verbose ``` 1. Enter the single letter of the "boot" drive and press Enter. diff --git a/setup/windows/WpaSupplicantConf.psm1 b/setup/windows/WpaSupplicantConf.psm1 new file mode 100644 index 0000000..93b9f7a --- /dev/null +++ b/setup/windows/WpaSupplicantConf.psm1 @@ -0,0 +1,91 @@ +function Write-Header { +param( + [string]$driveLetter + ) +$header=@" +ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev +update_config=1 +"@ + + Set-WpaSupplicantConfContent "$driveLetter" "$header" +} + +function Add-Network { +param( + [string]$driveLetter, + [string]$wifiSSID, + [string]$wifiPSK + ) + + $network=@" + + +network={ + ssid="$wifiSSID" + psk="$wifiPSK" +} +"@ + + Add-WpaSupplicantConfContent "$driveLetter" "$network" +} + +function Set-WpaSupplicantConfContent { +param( + [string]$driveLetter, + [string]$content + ) + + $wpaSupplicantConfPath = Get-WpaSupplicantConfPath $driveLetter + $encodedContent = Encode-Content $content + Set-Content -Value $encodedContent -Encoding Byte -Path "$wpaSupplicantConfPath" +} + +function Add-WpaSupplicantConfContent { +param( + [string]$driveLetter, + [string]$content + ) + + $wpaSupplicantConfPath = Get-WpaSupplicantConfPath $driveLetter + $encodedContent = Encode-Content $content + Add-Content -Value $encodedContent -Encoding Byte -Path "$wpaSupplicantConfPath" +} + +function Verify-WpaSupplicantConfPath { +param( + [string]$driveLetter + ) + + $drivePath="${driveLetter}:" + $configPath = "$drivePath\config.txt" + $cmdlinePath = "$drivePath\cmdline.txt" + $sshPath = "$drivePath\ssh" + + if ((![System.IO.File]::Exists($configPath) -or + (![System.IO.File]::Exists($cmdlinePath)))) { + Write-Error "Didn't find cmdline.txt and config.txt on drive $drivePath." + exit 1 + } +} + +function Encode-Content { +param( + [string]$content + ) + $utf8 = New-Object System.Text.UTF8Encoding $false + + return $utf8.GetBytes($content) +} + +function Get-WpaSupplicantConfPath { +param( + [string]$driveLetter + ) + + Verify-WpaSupplicantConfPath $driveLetter + + return "${driveLetter}:\wpa_supplicant.conf" +} + +Export-ModuleMember -Function Write-Header +Export-ModuleMember -Function Add-Network \ No newline at end of file diff --git a/setup/windows/add-wifi.ps1 b/setup/windows/add-wifi.ps1 index ef2d69f..cd1b06e 100644 --- a/setup/windows/add-wifi.ps1 +++ b/setup/windows/add-wifi.ps1 @@ -11,21 +11,8 @@ Param [string]$wifiPSK ) -$drivePath="${driveLetter}:" +Import-Module -Name ".\WpaSupplicantConf.psm1" -Force -$wpaSupplicantConfPath="$drivePath\wpa_supplicant.conf" - -$wpaSupplicantConfContent=@" - - -network={ - ssid="$wifiSSID" - psk="$wifiPSK" -} -"@ - -$utf8 = New-Object System.Text.UTF8Encoding $false - -Add-Content -Value $utf8.GetBytes($wpaSupplicantConfContent) -Encoding Byte -Path "$wpaSupplicantConfPath" +Add-Network "$driveLetter" "$wifiSSID" "$wifiPSK" Write-Verbose "All done." diff --git a/setup/windows/setup-piForHeadlessConfig.ps1 b/setup/windows/setup-piForHeadlessConfig.ps1 index d2a1eea..a2a802c 100644 --- a/setup/windows/setup-piForHeadlessConfig.ps1 +++ b/setup/windows/setup-piForHeadlessConfig.ps1 @@ -11,6 +11,8 @@ Param [string]$wifiPSK ) +Import-Module -Name ".\WpaSupplicantConf.psm1" -Force + $drivePath="${driveLetter}:" $configPath = "$drivePath\config.txt" $cmdlinePath = "$drivePath\cmdline.txt" @@ -34,28 +36,9 @@ $cmdlinetxtContent.Replace("rootwait", "rootwait modules-load=dwc2,g_ether").Rep Write-Verbose "Enabling SSH ..." [System.IO.File]::CreateText($sshPath).Dispose() -# Sets up wifi credentials so wifi will be -# auto configured on first boot - -$wpaSupplicantConfPath="$drivePath\wpa_supplicant.conf" - Write-Verbose "(Re)creating WiFi configuration file $wpaSupplicantConfPath." -if ([System.IO.File]::Exists("$wpaSupplicantConfPath")) { - del "$wpaSupplicantConfPath" -} -$wpaSupplicantConfContent=@" -ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev -update_config=1 - -network={ - ssid="$wifiSSID" - psk="$wifiPSK" -} -"@ - -$utf8 = New-Object System.Text.UTF8Encoding $false - -Set-Content -Value $utf8.GetBytes($wpaSupplicantConfContent) -Encoding Byte -Path "$wpaSupplicantConfPath" +Write-Header "$driveLetter" +Add-Network "$driveLetter" "$wifiSSID" "$wifiPSK" Write-Verbose "All done." \ No newline at end of file