rathbone01.PiNetworkControl
1.0.26
dotnet add package rathbone01.PiNetworkControl --version 1.0.26
NuGet\Install-Package rathbone01.PiNetworkControl -Version 1.0.26
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="rathbone01.PiNetworkControl" Version="1.0.26" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="rathbone01.PiNetworkControl" Version="1.0.26" />
<PackageReference Include="rathbone01.PiNetworkControl" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add rathbone01.PiNetworkControl --version 1.0.26
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: rathbone01.PiNetworkControl, 1.0.26"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package rathbone01.PiNetworkControl@1.0.26
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=rathbone01.PiNetworkControl&version=1.0.26
#tool nuget:?package=rathbone01.PiNetworkControl&version=1.0.26
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
PiNetworkControl
This is a library to control the network interfaces on a raspberry pi from a .NET application. This library works by controlling NMCLI via the command line using CLIWrap. I will turn it into a nuget package when I get around to learning how to do that.
Examples
In these examples assume the following:
public enum NetworkType
{
Ethernet,
Wifi
}
public class NetworkConfiguration
{
public NetworkType? NetworkType { get; set; }
public bool? DhcpEnabled { get; set; }
public string? IpAddress { get; set; }
public string? SubnetMask { get; set; }
public string? Gateway { get; set; }
public string? DnsServerPrimary { get; set; }
public string? DnsServerSecondary { get; set; }
public string? WifiSsid { get; set; }
public string? WifiPassword { get; set; }
}
Example of setting up a network:
private async Task CreateConnection(string connectionName, NetworkConfiguration networkConfiguration)
{
if (networkConfiguration.NetworkType == NetworkType.Wifi)
{
configurationService.SetNetworkConnectionType("wifi");
await networkController.AddWifiConnectionAsync(connectionName, "wlan0", networkConfiguration.WifiSsid!, networkConfiguration.WifiPassword!, "WPA-PSK");
_logger?.LogInformation($"Added wifi connection {connectionName}, SSID: {networkConfiguration.WifiSsid}");
return;
}
configurationService.SetNetworkConnectionType("ethernet");
await networkController.AddEthernetConnectionAsync(connectionName, "eth0");
_logger?.LogInformation($"Added ethernet connection {connectionName}");
}
private async Task SetConnectionProperties(string connectionName, NetworkConfiguration networkConfiguration)
{
_logger?.LogInformation("Setting connection properties");
if ((bool)networkConfiguration.DhcpEnabled!)
{
await networkController.ModifyConnectionPropertyAsync(connectionName, "ipv4.method", "auto");
_logger?.LogInformation("Set ipv4.method to auto");
return;
}
_logger?.LogInformation("Setting manual connection properties");
await networkController.ModifyConnectionPropertyAsync(connectionName, "ipv4.addresses", $"{networkConfiguration.IpAddress}/{ConvertSubnetMaskToBits(networkConfiguration.SubnetMask!)}");
await networkController.ModifyConnectionPropertyAsync(connectionName, "ipv4.gateway", networkConfiguration.Gateway!);
await networkController.ModifyConnectionPropertyAsync(connectionName, "ipv4.dns", $"\"{networkConfiguration.DnsServerPrimary!} {networkConfiguration.DnsServerSecondary!}\"");
await networkController.ModifyConnectionPropertyAsync(connectionName, "ipv4.method", "manual");
await networkController.ModifyConnectionPropertyAsync(connectionName, "connection.autoconnect", "yes");
_logger?.LogInformation($"Set ipv4.addresses to {networkConfiguration.IpAddress}/{ConvertSubnetMaskToBits(networkConfiguration.SubnetMask!)}");
}
Connecting to the network:
private async Task<bool> Connect(string connectionName, NetworkConfiguration networkConfiguration)
{
if (networkConfiguration.NetworkType == NetworkType.Wifi)
{
if (!await networkController.EnableRadioAsync())
{
_logger?.LogError("Error enabling wifi radio");
return false;
}
_logger?.LogInformation("Enabled wifi radio");
}
if (await networkController.EnableConnectionAsync(connectionName))
{
_logger?.LogInformation($"Enabled connection {connectionName}");
return true;
}
_logger?.LogError($"Error enabling connection {connectionName}");
return false;
}
Creating an access point
public async Task CreateAP(string connectionName, string Ssid, string Password)
{
await RemoveConnection(connectionName);
await networkController.AddWifiConnectionAsync(connectionName, "wlan0", Ssid, Password, "WPA-PSK");
_logger?.LogInformation($"Added wifi connection {connectionName}, SSID: {Ssid}");
_logger?.LogInformation($"Setting connection properties for Access Point");
var properties = new Dictionary<string, string>
{
{ "connection.autoconnect", "no" },
{ "802-11-wireless.mode", "ap" },
{ "802-11-wireless.band", "bg" },
{ "ipv4.method", "shared" }
};
await networkController.ModifyConnectionPropertiesAsync(connectionName, properties);
await networkController.EnableConnectionAsync(connectionName);
_logger?.LogInformation($"Created and enabled ap: {connectionName} with ssid {Ssid}");
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- CliWrap (>= 3.6.6)
- Microsoft.Extensions.Logging (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.