Skip to content

Commit

Permalink
Update main.rs
Browse files Browse the repository at this point in the history
Signed-off-by: ArchBlood <[email protected]>
  • Loading branch information
ArchBlood authored Dec 22, 2024
1 parent adc070d commit 04a32b9
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ fn load_config(path: &str) -> io::Result<Config> {

fn resolve_domain(domain: &str) -> io::Result<()> {
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default())?;
let response = resolver.lookup_ip(domain)?;
println!("Resolved DNS for {}: {:?}", domain, response);
Ok(())
match resolver.lookup_ip(domain) {
Ok(response) => {
println!("Resolved DNS for {}: {:?}", domain, response);
Ok(())
}
Err(e) => Err(io::Error::new(io::ErrorKind::NotFound, format!("Failed to resolve domain: {}", e))),
}
}

fn establish_ssh_connection(config: &Config) -> io::Result<Session> {
Expand All @@ -77,7 +81,11 @@ fn establish_ssh_connection(config: &Config) -> io::Result<Session> {

sess.set_tcp_stream(tcp);
sess.handshake()?;
sess.userauth_password(&config.username, &config.password)?;

// Try to authenticate using password
if let Err(e) = sess.userauth_password(&config.username, &config.password) {
return Err(io::Error::new(io::ErrorKind::PermissionDenied, format!("SSH Authentication failed: {}", e)));
}

if !sess.authenticated() {
return Err(io::Error::new(io::ErrorKind::PermissionDenied, "Authentication failed"));
Expand All @@ -87,15 +95,14 @@ fn establish_ssh_connection(config: &Config) -> io::Result<Session> {
}

fn execute_remote_commands(sess: &mut Session, commands: &[&str]) -> io::Result<()> {
for cmd in commands {
println!("Executing: {}", cmd);
let mut channel = sess.channel_session()?;
channel.exec(cmd)?;
let mut output = String::new();
channel.read_to_string(&mut output)?;
println!("{}", output);
channel.wait_close()?;
}
let mut channel = sess.channel_session()?;
let commands_str = commands.join(" && ");
println!("Executing: {}", commands_str);
channel.exec(&commands_str)?;
let mut output = String::new();
channel.read_to_string(&mut output)?;
println!("{}", output);
channel.wait_close()?;
Ok(())
}

Expand Down

0 comments on commit 04a32b9

Please sign in to comment.