feat: Connection is now established to known networks without password
This commit is contained in:
parent
fb68623acb
commit
c6fd190660
1 changed files with 47 additions and 16 deletions
63
src/main.rs
63
src/main.rs
|
|
@ -1,6 +1,6 @@
|
||||||
use wifi_connector::network::Network;
|
|
||||||
use anyhow::*;
|
use anyhow::*;
|
||||||
use std::{process::Command, vec, io::Write};
|
use std::{io::Write, process::Command, };
|
||||||
|
use wifi_connector::network::Network;
|
||||||
|
|
||||||
fn get_available_wifis() -> Result<String> {
|
fn get_available_wifis() -> Result<String> {
|
||||||
let output = Command::new("nmcli")
|
let output = Command::new("nmcli")
|
||||||
|
|
@ -36,7 +36,8 @@ fn get_all_networks() -> Vec<Network> {
|
||||||
let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded");
|
let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded");
|
||||||
let positions = get_descriptor_positions(&nmcli_output);
|
let positions = get_descriptor_positions(&nmcli_output);
|
||||||
|
|
||||||
let mut all_wifi_lines: Vec<String> = nmcli_output.split('\n').map(|s| String::from(s)).collect();
|
let mut all_wifi_lines: Vec<String> =
|
||||||
|
nmcli_output.split('\n').map(|s| String::from(s)).collect();
|
||||||
let mut all_networks: Vec<Network> = vec![];
|
let mut all_networks: Vec<Network> = vec![];
|
||||||
all_wifi_lines.remove(0);
|
all_wifi_lines.remove(0);
|
||||||
|
|
||||||
|
|
@ -61,8 +62,6 @@ fn connect_to_network(network: &Network, password: &str) -> Result<()> {
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed to execute command");
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
println!("{:?}", password);
|
|
||||||
|
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -70,25 +69,57 @@ fn connect_to_network(network: &Network, password: &str) -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn try_connect_to_network_without_password(network: &Network) -> Result<()> {
|
||||||
|
let output = Command::new("nmcli")
|
||||||
|
.arg("device")
|
||||||
|
.arg("wifi")
|
||||||
|
.arg("connect")
|
||||||
|
.arg(&network.ssid.trim())
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
dbg!(output.stderr);
|
||||||
|
Err(anyhow!("Couldn't connect to wifi without password"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let all_networks = get_all_networks();
|
let all_networks = get_all_networks();
|
||||||
|
|
||||||
for (idx, network) in all_networks.iter().enumerate() {
|
for (idx, network) in all_networks.iter().enumerate() {
|
||||||
println!("{} - {}", idx, network);
|
println!("{} - {}", idx, network);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Which network do you choose? (0-{})", all_networks.len() - 1);
|
println!(
|
||||||
|
"Which network do you choose? (0-{})",
|
||||||
|
all_networks.len() - 1
|
||||||
|
);
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
std::io::stdin().read_line(&mut input).unwrap();
|
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 network = all_networks
|
||||||
let mut password = String::new();
|
.get(input.trim().parse::<usize>().unwrap())
|
||||||
print!("Now enter the password for the selected wifi '{}': ", network.ssid);
|
.expect("Given number does not reference a network");
|
||||||
std::io::stdout().flush().unwrap();
|
let connected = try_connect_to_network_without_password(network);
|
||||||
std::io::stdin().read_line(&mut password).unwrap();
|
|
||||||
let result = connect_to_network(&network, password.trim());
|
if !connected.is_ok() {
|
||||||
if result.is_ok() {
|
let mut password = String::new();
|
||||||
println!("Successfully connected to network '{}'", network.ssid);
|
print!(
|
||||||
|
"Now enter the password for the selected wifi '{}': ",
|
||||||
|
network.ssid
|
||||||
|
);
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
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");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("Connection failed");
|
println!("Successfully connected to network '{}'", network.ssid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue