Skip to content
Snippets Groups Projects
Commit e505d5b0 authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

connexion ftp avec ip

parent 258a20a8
Branches
No related tags found
No related merge requests found
...@@ -18,3 +18,8 @@ Cargo.lock ...@@ -18,3 +18,8 @@ Cargo.lock
*.pdb *.pdb
# End of https://www.toptal.com/developers/gitignore/api/rust # End of https://www.toptal.com/developers/gitignore/api/rust
# Added by cargo
/target
127.0.0.1
::1
192.168.100.40
Obviously malformed ligne
10.0.278.1
10.0.0.1
Hello, world!
Parsing ips file
Connecting to 127.0.0.1...
\ No newline at end of file
use std::{io, net::IpAddr}; use std::{io, net::IpAddr};
use std::net::{TcpStream, SocketAddr};
use std::time::Duration;
pub enum PasswordError { pub enum PasswordError {
Empty, Empty,
...@@ -28,7 +30,7 @@ impl PasswordChecker { ...@@ -28,7 +30,7 @@ impl PasswordChecker {
} }
} }
fn main() { fn main() -> Result<(), io::Error>{
println!("Hello, world!"); println!("Hello, world!");
// let x: i8 = 42; // let x: i8 = 42;
...@@ -43,33 +45,79 @@ fn main() { ...@@ -43,33 +45,79 @@ fn main() {
// check_password(); // check_password();
println!("Enter password: "); // println!("Enter password: ");
let mut input = String::new(); // let mut input = String::new();
let _ = io::stdin().read_line(&mut input); // let _ = io::stdin().read_line(&mut input);
let checker = PasswordChecker::new("Rust"); // let checker = PasswordChecker::new("Rust");
// Call the check function on the checker. // // Call the check function on the checker.
// This function should have the same return type as // // This function should have the same return type as
// the check_password function of the previous exercise // // the check_password function of the previous exercise
match checker.check(&input) { // match checker.check(&input) {
Ok(()) => println!("ok"), // Ok(()) => println!("ok"),
Err(PasswordError::Empty) => println!("Empty Password"), // Err(PasswordError::Empty) => println!("Empty Password"),
Err(PasswordError::Wrong) => println!("Wrong password"), // Err(PasswordError::Wrong) => println!("Wrong password"),
// }
let ports = vec![20, 80, 443];
println!("Parsing ips file");
match get_addrs("ips.txt") {
Ok(ips) => {
for ip in &ips {
println!("Connecting to {} ....", *ip);
for port in &ports {
let _ = try_connect(*ip, *port);
}
}
}
Err(e) => eprintln!("Erreur lors de la lecture du fichier : {}", e),
} }
Ok(())
} }
/// Reads a file that contains an address per line and returns a Vector with all well-formed /// Reads a file that contains an address per line and returns a Vector with all well-formed
/// addresses and prints a warning on the standard error output for every malformed lines. /// addresses and prints a warning on the standard error output for every malformed lines.
/// ///
/// Addresses should be ipv4 or ipv6 addresses. /// Addresses should be ipv4 or ipv6 addresses.
fn get_addrs(path: &str) -> Result<Vec<IpAddr>, std::io::Error> { todo!{} } fn get_addrs(path: &str) -> Result<Vec<IpAddr>, std::io::Error> {
let content = std::fs::read_to_string(path)?;
let mut addresses = Vec::new();
for line in content.lines() {
let line = line.trim(); // Supprimer les espaces inutiles
if line.is_empty() {
continue; // Ignorer les lignes vides
}
match line.parse::<IpAddr>() {
Ok(addr) => addresses.push(addr), // Ajouter l'adresse seulement si elle est valide
Err(_) => eprintln!("'{}' : is not a correct IP address invalid IP address syntax", line),
}
}
Ok(addresses) // Retourner uniquement les adresses valides
}
/// Tries to connect to a given host with a TCP Connection with a one second timeout. /// Tries to connect to a given host with a TCP Connection with a one second timeout.
/// ///
/// Returns Ok(()) or a corresponding error if connection fails. /// Returns Ok(()) or a corresponding error if connection fails.
fn try_connect(addr: IpAddr, port: u16) -> Result<(), std::io::Error> { todo!() } ///
fn try_connect(addr: IpAddr, port: u16) -> Result<(), std::io::Error> {
let socket = SocketAddr::new(addr, port);
let timeout = Duration::from_secs(1);
match TcpStream::connect_timeout(&socket, timeout) {
Ok(_) => {
println!(" port {port}: alive");
Ok(())
}
Err(e) => {
eprintln!(" port {port}: down (Connection refused ({e}))");
Err(e)
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment