Skip to content

Commit

Permalink
Add pingsweep for CIDR subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
sevro committed Oct 29, 2017
1 parent d537a63 commit a2f15f0
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 77 deletions.
15 changes: 14 additions & 1 deletion rust/pingsweep/resources/cli.yml
Expand Up @@ -24,7 +24,7 @@ args:
help: Number of threads to use
subcommands:
- list:
about: Ping a given set of addresses
about: Ping a given list of addresses
args:
- sockets:
required: true
Expand All @@ -42,3 +42,16 @@ subcommands:
- FILE:
required: true
help: Input file with a list of socket addresses
- cidr:
about: Ping a given set of addresses
args:
- subnet:
required: true
multiple: true
help: List of subnets in CIDR format
- ports:
short: p
long: ports
multiple: true
takes_value: true
help: Port to be checked across all IP addresses
161 changes: 161 additions & 0 deletions rust/pingsweep/resources/test_class_c.dat
@@ -0,0 +1,161 @@
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.1.1:1
192.168.1.1:2
192.168.1.1:3
192.168.1.1:4
192.168.1.1:5
192.168.1.1:6
192.168.1.1:7
192.168.1.1:8
192.168.1.1:9
192.168.1.1:10
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
192.168.15.1:80
108 changes: 108 additions & 0 deletions rust/pingsweep/src/config.rs
@@ -0,0 +1,108 @@
use std::io::{BufReader, BufRead};
use std::fs::File;
use std::str::FromStr;
use clap::ArgMatches;
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use subnet::*;


// TODO:
// * verbosity to Enum
// * Add -q --quiet
//
pub struct Config {
pub verbosity: u8,
pub threads: u64,
pub sockets: Vec<SocketAddr>,
}

impl Config {
pub fn new(matches: ArgMatches) -> Result<Config, &'static str> {
let verbosity = Config::parse_verbosity(&matches);

let threads = match matches.value_of("threads").unwrap_or("1").parse() {
Ok(num) => num,
Err(_) => return Err("Threads must be an integer"),
};

let mut sockets = vec![];
match matches.subcommand_name() {
Some("list") => {
if let Some(socket_addrs) = matches.subcommand_matches("list") {
for socket_str in socket_addrs.values_of("sockets").unwrap() {
let socket_addr = match SocketAddr::from_str(socket_str) {
Ok(socket_addr) => socket_addr,
Err(_) => return Err("Invalid address provided"),
};
sockets.push(socket_addr);
}
};
},
Some("file") => {
if let Some(input_file) = matches.subcommand_matches("file") {
let f = match File::open(input_file.value_of("FILE").unwrap()) {
Ok(f) => f,
Err(_) => return Err("Unable to open file"),
};

let file = BufReader::new(f);

for line in file.lines() {
let socket_addr = match SocketAddr::from_str(line.unwrap().trim()) {
Ok(socket_addr) => socket_addr,
Err(e) => {
println!("{}", e);
return Err("Invalid address provided")
},
};
sockets.push(socket_addr);
}
};
},
Some("cidr") => {
if let Some(subnet_matches) = matches.subcommand_matches("cidr") {
for subnet_str in subnet_matches.values_of("subnet").unwrap() {
let subnet = match Subnet::from_str(subnet_str) {
Ok(snet) => snet,
Err(_) => return Err("Invalid subnet provided"),
};

let mut ports: Vec<u16> = vec![];
if let Some(port_strs) = subnet_matches.values_of("ports") {
for port in port_strs {
match port.parse() {
Ok(port) => ports.push(port),
Err(_) => return Err("Invalid port provided"),
}
}
}

match subnet {
Subnet::V4(subnet) => {
for (ip, port) in subnet.into_iter().zip(ports.into_iter().cycle()) {
sockets.push(SocketAddr::V4(SocketAddrV4::new(ip, port)));
}
}
Subnet::V6(subnet) => {
for (ip, port) in subnet.into_iter().zip(ports.into_iter()) {
sockets.push(SocketAddr::V6(SocketAddrV6::new(ip, port, 0, 0)));
}
}
}
}
}
},
Some(&_) | None => return Err("See usage"),
}

Ok(Config { verbosity, sockets, threads })
}

fn parse_verbosity(matches: &ArgMatches) -> u8 {
if matches.is_present("verbosity") {
matches.occurrences_of("verbosity") as u8
} else {
1
}
}
}

0 comments on commit a2f15f0

Please sign in to comment.