Last active
July 8, 2021 14:25
-
-
Save J-Cake/2370878f0df77b82960c4fb90cd67a4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import getopt | |
opts, args = getopt.getopt(sys.argv[1:], '', ['mask=', 'ip=']) | |
mask_input = list(filter(lambda x: '--mask' in x, opts)) | |
ip_address_input = list(filter(lambda x: '--ip' in x, opts)) | |
if len(mask_input) > 0: | |
maskNum = max(min(int(mask_input[-1][1]), 32), 0) | |
mask = [1] * maskNum + [0] * (32 - maskNum) | |
maskVal = sum(j << i for i, j in enumerate(reversed(mask))) | |
if len(ip_address_input) > 0: | |
ip = bytearray([int(i) for i in ip_address_input[-1][1].split('.')]) | |
ipVal = int.from_bytes(ip, byteorder='big') | |
wildcard = (maskVal ^ (2 ** 32 - 1)) & ipVal | |
network = maskVal & ipVal | |
broadcast = maskVal ^ (2 ** 32 - 1) | network | |
print ('Network: ', '.'.join([str(int(i)) for i in network.to_bytes(4, 'big')])) | |
print ('Broadcast:', '.'.join([str(int(i)) for i in broadcast.to_bytes(4, 'big')])) | |
# print ([int(i) for i in network.to_bytes(4, 'big')], [int(i) for i in broadcast.to_bytes(4, 'big')]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use bit_vec::BitVec; | |
fn and(vec1: &BitVec, vec2: &BitVec) -> BitVec { | |
if vec1.len() == vec2.len() { | |
let mut out: BitVec = BitVec::from_elem(vec1.len(), false); | |
for (a, i) in vec1.iter().enumerate() { | |
out.set(a, i && vec2.get(a).unwrap()); | |
} | |
return out; | |
} else { | |
panic!("Size mismatch"); | |
} | |
} | |
fn print_vec(vec: &BitVec) -> String { | |
let bytes = vec.to_bytes(); | |
if bytes.len() != 4 { | |
panic!("Expected 4 bytes, got {}", bytes.len()); | |
} else { | |
return format!("{}.{}.{}.{}", bytes[0], bytes[1], bytes[2], bytes[3]); | |
} | |
} | |
fn main() { | |
let mut args: Vec<String> = env::args().collect(); | |
let mask_marker: usize = args.iter().position(|str: &String| { str == "-m" }).unwrap(); | |
if args.len() > mask_marker + 1 { | |
let mask: &String = &args[mask_marker + 1]; | |
let mask_num = mask.parse::<u32>().unwrap(); | |
args.remove(mask_marker); | |
args.remove(mask_marker); | |
let ip: &Vec<u8> = &args[1].split('.').collect::<Vec<&str>>().iter().map(|i: &&str| { | |
i.parse::<u8>().unwrap() | |
}).collect::<Vec<u8>>(); | |
if args.len() > 2 { | |
panic!("Unexpected remaining parameters"); | |
} | |
if mask_num <= 32 { | |
let ip_vec = BitVec::from_bytes(&[ | |
*ip.get(0).unwrap(), | |
*ip.get(1).unwrap(), | |
*ip.get(2).unwrap(), | |
*ip.get(3).unwrap(), | |
]); | |
let mut network = BitVec::from_elem(mask_num as usize, true); | |
network.append(&mut BitVec::from_elem((32 - mask_num) as usize, false)); | |
let mut broadcast: BitVec<u32> = BitVec::from(network.clone()); | |
broadcast.negate(); | |
println!("IP: {}", print_vec(&ip_vec)); | |
println!("network: {}\nbroadcast: {}", | |
print_vec(&and(&ip_vec, &network)), | |
print_vec(&and(&ip_vec, &broadcast))); | |
} else { | |
panic!("Expected number (0-32) for mask"); | |
} | |
} else { | |
panic!("Mask expected"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment