Skip to content

Instantly share code, notes, and snippets.

@dot3dash4dot
Last active February 9, 2025 09:31
Show Gist options
  • Save dot3dash4dot/0fb7e747074993e0e806aeb03a8cc4fc to your computer and use it in GitHub Desktop.
Save dot3dash4dot/0fb7e747074993e0e806aeb03a8cc4fc to your computer and use it in GitHub Desktop.
Connecting to a Virgin Media Hub 5 Router in C#
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public static class VirginRouter
{
public static async Task<IEnumerable<(string Name, string IP)>> GetWifiConnectedDevices(string routerIP, string routerPassword)
{
HttpClient httpClient = new HttpClient();
//Log in to router
var loginContent = new StringContent($"{{\"password\":\"{routerPassword}\"}}");
loginContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var loginResult = await RunQuery<LoginResult>(httpClient.PostAsync($"{routerIP}/rest/v1/user/login", loginContent));
string token = loginResult.Created.Token;
//Use token for future requests
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//Get Connected Hosts
var getHostsResult = await RunQuery<GetHostsResult>(httpClient.GetAsync($"{routerIP}/rest/v1/network/hosts?connectedOnly=true"));
var deviceNames = getHostsResult.Hosts1.Hosts2.Select(x => (x.Config.Hostname, x.Config.Ipv4.Address));
//Log out of router
await httpClient.DeleteAsync($"{routerIP}/rest/v1/user/3/token/{token}");
return deviceNames;
}
private static async Task<T> RunQuery<T>(Task<HttpResponseMessage> query)
{
var response = await query;
var result = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(result);
}
}
//The following classes were generated using https://json2csharp.com/
#region Classes for Login call
internal class Created
{
public string Token { get; set; }
public string UserLevel { get; set; }
public int UserId { get; set; }
}
internal class LoginResult
{
public Created Created { get; set; }
}
#endregion
#region Classes for Get Hosts call
public class Config
{
[JsonProperty("connected")]
public bool Connected { get; set; }
[JsonProperty("deviceName")]
public string DeviceName { get; set; }
[JsonProperty("deviceType")]
public string DeviceType { get; set; }
[JsonProperty("hostname")]
public string Hostname { get; set; }
[JsonProperty("interface")]
public string Interface { get; set; }
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("ethernet")]
public Ethernet Ethernet { get; set; }
[JsonProperty("ipv4")]
public Ipv4 Ipv4 { get; set; }
[JsonProperty("wifi")]
public Wifi Wifi { get; set; }
[JsonProperty("unknown")]
public Unknown Unknown { get; set; }
}
public class Ethernet
{
[JsonProperty("port")]
public int Port { get; set; }
}
public class Hosts1
{
[JsonProperty("hosts")]
public List<Hosts2> Hosts2 { get; set; }
}
public class Hosts2
{
[JsonProperty("macAddress")]
public string MacAddress { get; set; }
[JsonProperty("config")]
public Config Config { get; set; }
}
public class Ipv4
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("leaseTimeRemaining")]
public int LeaseTimeRemaining { get; set; }
}
public class GetHostsResult
{
[JsonProperty("hosts")]
public Hosts1 Hosts1 { get; set; }
}
public class Unknown
{
}
public class Wifi
{
[JsonProperty("band")]
public string Band { get; set; }
[JsonProperty("ssid")]
public string Ssid { get; set; }
[JsonProperty("rssi")]
public int Rssi { get; set; }
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment