PowerShell 语言 批量配置树莓派 4B 启用 SSH/VNC/ 蓝牙并设置静态 IP

PowerShell阿木 发布于 1 天前 2 次阅读


PowerShell脚本批量配置树莓派4B:启用SSH、VNC、蓝牙并设置静态IP

树莓派4B是一款功能强大的单板计算机,广泛应用于教育、家庭娱乐和工业控制等领域。为了方便远程管理和操作,我们通常需要启用SSH、VNC和蓝牙功能,并设置静态IP地址。本文将使用PowerShell脚本实现这一目标,并通过批量操作提高效率。

环境准备

在开始之前,请确保以下环境已准备就绪:

1. Windows操作系统
2. PowerShell 5.0或更高版本
3. 树莓派4B设备
4. 树莓派操作系统(如Raspbian)

脚本编写

以下是一个PowerShell脚本示例,用于批量配置树莓派4B的SSH、VNC、蓝牙和静态IP地址。

powershell
定义树莓派列表
$raspberryPis = @('192.168.1.101', '192.168.1.102', '192.168.1.103')

定义静态IP地址
$staticIp = '192.168.1.110'

定义子网掩码
$subnetMask = '255.255.255.0'

定义默认网关
$defaultGateway = '192.168.1.1'

定义DNS服务器
$dnsServer = '8.8.8.8'

定义VNC密码
$vncPassword = 'vncpassword'

定义蓝牙名称
$bluetoothName = 'RaspberryPi'

启用SSH
function Enable-Ssh {
param (
[string]$raspberryPi
)
$sshEnableCmd = "ssh pi@$raspberryPi 'sudo raspi-config nonint enable_ssh'"
Invoke-Expression -Command $sshEnableCmd
}

启用VNC
function Enable-Vnc {
param (
[string]$raspberryPi
)
$vncEnableCmd = "ssh pi@$raspberryPi 'sudo raspi-config nonint enable_vnc'"
Invoke-Expression -Command $vncEnableCmd
}

设置静态IP
function Set-StaticIp {
param (
[string]$raspberryPi,
[string]$staticIp,
[string]$subnetMask,
[string]$defaultGateway,
[string]$dnsServer
)
$ipConfigCmd = "ssh pi@$raspberryPi 'sudo nano /etc/dhcpcd.conf'"
$newIpConfig = @"
interface eth0
static ip_address=$staticIp/24
static routers=$defaultGateway
static domain_name_servers=$dnsServer
"@

Invoke-Expression -Command $ipConfigCmd
$newIpConfig | Out-File -FilePath "C:tempipconfig.txt"
$newIpConfig | ssh pi@$raspberryPi 'sudo cat > /etc/dhcpcd.conf'
}

启用蓝牙
function Enable-Bluetooth {
param (
[string]$raspberryPi,
[string]$bluetoothName
)
$bluetoothEnableCmd = "ssh pi@$raspberryPi 'sudo systemctl start bluetooth && sudo systemctl enable bluetooth && sudo bluetoothctl name $bluetoothName'"
Invoke-Expression -Command $bluetoothEnableCmd
}

执行批量配置
foreach ($raspberryPi in $raspberryPis) {
Enable-Ssh -raspberryPi $raspberryPi
Enable-Vnc -raspberryPi $raspberryPi
Set-StaticIp -raspberryPi $raspberryPi -staticIp $staticIp -subnetMask $subnetMask -defaultGateway $defaultGateway -dnsServer $dnsServer
Enable-Bluetooth -raspberryPi $raspberryPi -bluetoothName $bluetoothName
}

脚本说明

1. 定义树莓派列表:`$raspberryPis`变量存储了需要配置的树莓派IP地址。
2. 定义静态IP地址、子网掩码、默认网关和DNS服务器:分别存储在`$staticIp`、`$subnetMask`、`$defaultGateway`和`$dnsServer`变量中。
3. 定义VNC密码和蓝牙名称:分别存储在`$vncPassword`和`$bluetoothName`变量中。
4. 启用SSH:`Enable-Ssh`函数通过SSH连接到树莓派,并执行`raspi-config nonint enable_ssh`命令启用SSH服务。
5. 启用VNC:`Enable-Vnc`函数通过SSH连接到树莓派,并执行`raspi-config nonint enable_vnc`命令启用VNC服务。
6. 设置静态IP:`Set-StaticIp`函数通过SSH连接到树莓派,并使用`nano`编辑器修改`/etc/dhcpcd.conf`文件,设置静态IP地址、子网掩码、默认网关和DNS服务器。
7. 启用蓝牙:`Enable-Bluetooth`函数通过SSH连接到树莓派,并执行相关命令启用蓝牙服务,并设置蓝牙名称。

执行脚本

将以上脚本保存为`.ps1`文件,例如`Configure-RaspberryPi.ps1`。在PowerShell中执行以下命令:

powershell
.Configure-RaspberryPi.ps1

脚本将自动连接到树莓派,并按照预设的配置进行操作。

总结

本文介绍了使用PowerShell脚本批量配置树莓派4B的方法,包括启用SSH、VNC、蓝牙和设置静态IP地址。通过批量操作,我们可以提高配置效率,节省时间和精力。在实际应用中,可以根据需求修改脚本中的参数和配置,以满足不同的需求。