Extract common functionality to WpaSupplicantConf.psm1

This commit is contained in:
cimryan
2018-10-24 22:33:51 -07:00
parent 27ed80059b
commit c607860052
4 changed files with 98 additions and 36 deletions

View File

@@ -15,6 +15,7 @@
``` ```
cd ~ 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/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 ./setup-piForHeadlessConfig.ps1 -Verbose
``` ```
1. Enter the single letter of the "boot" drive and press Enter. 1. Enter the single letter of the "boot" drive and press Enter.

View File

@@ -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

View File

@@ -11,21 +11,8 @@ Param
[string]$wifiPSK [string]$wifiPSK
) )
$drivePath="${driveLetter}:" Import-Module -Name ".\WpaSupplicantConf.psm1" -Force
$wpaSupplicantConfPath="$drivePath\wpa_supplicant.conf" Add-Network "$driveLetter" "$wifiSSID" "$wifiPSK"
$wpaSupplicantConfContent=@"
network={
ssid="$wifiSSID"
psk="$wifiPSK"
}
"@
$utf8 = New-Object System.Text.UTF8Encoding $false
Add-Content -Value $utf8.GetBytes($wpaSupplicantConfContent) -Encoding Byte -Path "$wpaSupplicantConfPath"
Write-Verbose "All done." Write-Verbose "All done."

View File

@@ -11,6 +11,8 @@ Param
[string]$wifiPSK [string]$wifiPSK
) )
Import-Module -Name ".\WpaSupplicantConf.psm1" -Force
$drivePath="${driveLetter}:" $drivePath="${driveLetter}:"
$configPath = "$drivePath\config.txt" $configPath = "$drivePath\config.txt"
$cmdlinePath = "$drivePath\cmdline.txt" $cmdlinePath = "$drivePath\cmdline.txt"
@@ -34,28 +36,9 @@ $cmdlinetxtContent.Replace("rootwait", "rootwait modules-load=dwc2,g_ether").Rep
Write-Verbose "Enabling SSH ..." Write-Verbose "Enabling SSH ..."
[System.IO.File]::CreateText($sshPath).Dispose() [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." Write-Verbose "(Re)creating WiFi configuration file $wpaSupplicantConfPath."
if ([System.IO.File]::Exists("$wpaSupplicantConfPath")) {
del "$wpaSupplicantConfPath"
}
$wpaSupplicantConfContent=@" Write-Header "$driveLetter"
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev Add-Network "$driveLetter" "$wifiSSID" "$wifiPSK"
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-Verbose "All done." Write-Verbose "All done."