feat: Networks can be connected via console now
This commit is contained in:
parent
e30ecd06e6
commit
0ce0705e19
2 changed files with 56 additions and 7 deletions
49
src/main.rs
49
src/main.rs
|
|
@ -1,6 +1,6 @@
|
|||
use wifi_connector::network::Network;
|
||||
use anyhow::*;
|
||||
use std::{process::Command, vec};
|
||||
use std::{process::Command, vec, io::Write};
|
||||
|
||||
fn get_available_wifis() -> Result<String> {
|
||||
let output = Command::new("nmcli")
|
||||
|
|
@ -32,7 +32,7 @@ fn get_descriptor_positions(header_line: &String) -> Vec<u8> {
|
|||
return positions;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn get_all_networks() -> Vec<Network> {
|
||||
let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded");
|
||||
let positions = get_descriptor_positions(&nmcli_output);
|
||||
|
||||
|
|
@ -47,7 +47,48 @@ fn main() {
|
|||
all_networks.push(Network::from_nmcli_stdout(line.to_owned(), &positions));
|
||||
}
|
||||
|
||||
for network in all_networks {
|
||||
dbg!("{}", network);
|
||||
return all_networks;
|
||||
}
|
||||
|
||||
fn connect_to_network(network: &Network, password: &str) -> Result<()> {
|
||||
let output = Command::new("nmcli")
|
||||
.arg("device")
|
||||
.arg("wifi")
|
||||
.arg("connect")
|
||||
.arg(&network.ssid.trim())
|
||||
.arg("password")
|
||||
.arg(password)
|
||||
.output()
|
||||
.expect("Failed to execute command");
|
||||
|
||||
println!("{:?}", password);
|
||||
|
||||
if output.status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Failed to connect to network"))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let all_networks = get_all_networks();
|
||||
|
||||
for (idx, network) in all_networks.iter().enumerate() {
|
||||
println!("{} - {}", idx, network);
|
||||
}
|
||||
|
||||
println!("Which network do you choose? (0-{})", all_networks.len() - 1);
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).unwrap();
|
||||
let network = all_networks.get(input.trim().parse::<usize>().unwrap()).expect("Given number does not reference a network");
|
||||
let mut password = String::new();
|
||||
print!("Now enter the password for the selected wifi '{}': ", network.ssid);
|
||||
std::io::stdout().flush();
|
||||
std::io::stdin().read_line(&mut password).unwrap();
|
||||
let result = connect_to_network(&network, password.trim());
|
||||
if result.is_ok() {
|
||||
println!("Successfully connected to network '{}'", network.ssid);
|
||||
} else {
|
||||
println!("Connection failed");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Network {
|
||||
in_use: bool,
|
||||
ssid: String,
|
||||
signal: u8,
|
||||
pub in_use: bool,
|
||||
pub ssid: String,
|
||||
pub signal: u8,
|
||||
}
|
||||
|
||||
impl Network {
|
||||
|
|
@ -71,3 +73,9 @@ impl Network {
|
|||
return network;
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Network {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
return write!(f, "{} - {} - {} ", self.in_use, self.ssid, self.signal);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue