switch to iw

This commit is contained in:
Robin Appelman 2024-12-11 19:26:04 +01:00
commit 8cd4806dc2
4 changed files with 19 additions and 13 deletions

View file

@ -8,7 +8,7 @@ with lib; let
format = pkgs.formats.toml { }; format = pkgs.formats.toml { };
configFile = format.generate "wifi-prometheus-exporter-config.toml" { configFile = format.generate "wifi-prometheus-exporter-config.toml" {
ssh = { ssh = {
inherit (cfg.ssh) address; inherit (cfg.ssh) address user;
key_file = "$CREDENTIALS_DIRECTORY/ssh_key"; key_file = "$CREDENTIALS_DIRECTORY/ssh_key";
pubkey_file = "$CREDENTIALS_DIRECTORY/ssh_pub_key"; pubkey_file = "$CREDENTIALS_DIRECTORY/ssh_pub_key";
}; };
@ -33,6 +33,10 @@ in
type = types.str; type = types.str;
description = "ssh address of the access point"; description = "ssh address of the access point";
}; };
user = mkOption {
type = types.str;
description = "ssh user";
};
keyFile = mkOption { keyFile = mkOption {
type = types.str; type = types.str;
description = "path to ssh key file"; description = "path to ssh key file";

View file

@ -41,6 +41,7 @@ impl MqttConfig {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SshConfig { pub struct SshConfig {
pub address: String, pub address: String,
pub user: String,
pubkey_file: String, pubkey_file: String,
key_file: String, key_file: String,
} }

View file

@ -13,6 +13,7 @@ pub struct WifiLister {
impl WifiLister { impl WifiLister {
pub fn new<A: ToSocketAddrs + Debug>( pub fn new<A: ToSocketAddrs + Debug>(
addr: A, addr: A,
user: &str,
key: &str, key: &str,
pubkey: &str, pubkey: &str,
interfaces: &[String], interfaces: &[String],
@ -23,18 +24,14 @@ impl WifiLister {
session.set_tcp_stream(tcp); session.set_tcp_stream(tcp);
session.handshake().map_err(Error::SshSession)?; session.handshake().map_err(Error::SshSession)?;
session session
.userauth_pubkey_memory("admin", Some(pubkey), key, None) .userauth_pubkey_memory(user, Some(pubkey), key, None)
.map_err(Error::SshAuth)?; .map_err(Error::SshAuth)?;
let command = if interfaces.is_empty() { let commands: Vec<String> = interfaces
"wl assoclist".to_string() .iter()
} else { .map(|interface| format!("iw dev {} station dump", interface))
let commands: Vec<String> = interfaces .collect();
.iter() let command = commands.join(" && ");
.map(|interface| format!("wl -a {} assoclist", interface))
.collect();
commands.join(" && ")
};
info!("ssh connected"); info!("ssh connected");
@ -55,7 +52,9 @@ impl WifiLister {
channel.wait_close()?; channel.wait_close()?;
Ok(s.lines() Ok(s.lines()
.map(|s| s.trim_start_matches("assoclist ").to_string()) .filter(|s| s.starts_with("Station"))
.filter_map(|s| s.split(' ').nth(1))
.map(String::from)
.collect()) .collect())
} }
} }

View file

@ -43,7 +43,8 @@ async fn main() -> Result<(), MainError> {
}; };
if config.exporter.interfaces.is_empty() { if config.exporter.interfaces.is_empty() {
info!("Listening on default interface"); error!("No interfaces specified");
return Ok(());
} else { } else {
info!( info!(
"Listening on interfaces: {}", "Listening on interfaces: {}",
@ -54,6 +55,7 @@ async fn main() -> Result<(), MainError> {
let connected: Arc<Mutex<DeviceStates>> = Default::default(); let connected: Arc<Mutex<DeviceStates>> = Default::default();
let wifi_listener = WifiLister::new( let wifi_listener = WifiLister::new(
&config.ssh.address, &config.ssh.address,
&config.ssh.user,
&config.ssh.key()?, &config.ssh.key()?,
&config.ssh.pubkey()?, &config.ssh.pubkey()?,
&config.exporter.interfaces, &config.exporter.interfaces,