this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2 from mbStavola/cidrcheck

Add cidrcheck

authored by

Matt Freitas-Stavola and committed by
GitHub
ca15b7d9 3c130e51

+59
+59
cidrcheck
··· 1 + #!/usr/bin/env -S cargo eval -- 2 + 3 + // cargo-deps: argh = "0.1", cidr = "0.2.1" 4 + 5 + use argh::FromArgs; 6 + use cidr::IpCidr; 7 + 8 + use std::net::IpAddr; 9 + use std::str::FromStr; 10 + 11 + #[derive(FromArgs)] 12 + #[argh(description = "Display the ip range for a CIDR and check if an ip falls within.")] 13 + struct App { 14 + #[argh( 15 + option, 16 + description = "ip that will be checked against the provided CIDR.", 17 + from_str_fn(parse_ip) 18 + )] 19 + ip: Option<IpAddr>, 20 + 21 + #[argh( 22 + positional, 23 + description = "CIDR to inspect", 24 + from_str_fn(parse_cidr) 25 + )] 26 + cidr: IpCidr, 27 + } 28 + 29 + fn main() { 30 + let App { ip, cidr } = argh::from_env(); 31 + println!( 32 + "CIDR {} has a range of: {} to {}", 33 + cidr, 34 + cidr.first_address(), 35 + cidr.last_address() 36 + ); 37 + 38 + println!("CIDR {} has a netmask of {}", cidr, cidr.mask()); 39 + 40 + if let Some(ip) = ip { 41 + print!("IP {}", ip); 42 + let falls_within = cidr.contains(&ip); 43 + if falls_within { 44 + print!(" falls "); 45 + } else { 46 + print!(" does not fall "); 47 + } 48 + 49 + println!("within {}", cidr); 50 + } 51 + } 52 + 53 + fn parse_ip(value: &str) -> Result<IpAddr, String> { 54 + IpAddr::from_str(value).map_err(|e| format!("{}", e)) 55 + } 56 + 57 + fn parse_cidr(value: &str) -> Result<IpCidr, String> { 58 + IpCidr::from_str(value).map_err(|e| format!("{}", e)) 59 + }