mirror of
https://github.com/cimryan/teslausb.git
synced 2026-03-01 04:30:33 +00:00
Extract common functionality to WpaSupplicantConf.psm1
This commit is contained in:
@@ -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.
|
||||
|
||||
91
setup/windows/WpaSupplicantConf.psm1
Normal file
91
setup/windows/WpaSupplicantConf.psm1
Normal 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
|
||||
@@ -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."
|
||||
|
||||
@@ -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."
|
||||
Reference in New Issue
Block a user