Created
July 30, 2025 15:13
-
-
Save k98kurz/d8ad7494c5acd2fc2e90ca868af979d7 to your computer and use it in GitHub Desktop.
CIDR tool: check if an IP address belongs to a CIDR block
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
| license = '''Copyright (c) 2024 Jonathan Voss (k98kurz) | |
| Permission to use, copy, modify, and/or distribute this software | |
| for any purpose with or without fee is hereby granted, provided | |
| that the above copyleft notice and this permission notice appear in | |
| all copies. | |
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL | |
| WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED | |
| WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE | |
| AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR | |
| CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | |
| OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | |
| NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | |
| CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| ''' | |
| import customtkinter as ctk | |
| def get_good_color(): | |
| return "white" if ctk.get_appearance_mode().lower() == "dark" else "black" | |
| class App(ctk.CTk): | |
| def __init__(self, license): | |
| super().__init__() | |
| self.license = license | |
| # Window configuration | |
| self.title("CIDR/IP Checker") | |
| self.geometry("550x380") | |
| # Create scrollable frame instead of regular frame | |
| self.main_frame = ctk.CTkFrame(self, fg_color="transparent") | |
| self.main_frame.pack(pady=20, padx=20, fill="both", expand=True) | |
| # CIDR input | |
| self.cidr_label = ctk.CTkLabel(self.main_frame, text="CIDR Block:") | |
| self.cidr_label.pack(pady=(20, 5)) | |
| self.cidr_entry = ctk.CTkEntry(self.main_frame, placeholder_text="e.g., 192.168.1.0/24") | |
| self.cidr_entry.pack(pady=(0, 10)) | |
| self.cidr_entry.bind("<Return>", lambda event: self.check_ip()) | |
| self.cidr_entry.bind("<KP_Enter>", lambda event: self.check_ip()) | |
| # IP input | |
| self.ip_label = ctk.CTkLabel(self.main_frame, text="IP Address:") | |
| self.ip_label.pack(pady=(10, 5)) | |
| self.ip_entry = ctk.CTkEntry(self.main_frame, placeholder_text="e.g., 192.168.1.100") | |
| self.ip_entry.pack(pady=(0, 20)) | |
| self.ip_entry.bind("<Return>", lambda event: self.check_ip()) | |
| self.ip_entry.bind("<KP_Enter>", lambda event: self.check_ip()) | |
| # Check button | |
| self.check_button = ctk.CTkButton(self.main_frame, text="Check", command=self.check_ip) | |
| self.check_button.pack(pady=10) | |
| # Result label | |
| self.result_label = ctk.CTkLabel(self.main_frame, text="result will be here", font=("Arial", 18)) | |
| self.result_label.pack(pady=20) | |
| # License button | |
| self.license_button = ctk.CTkButton(self.main_frame, text="ISC License", command=self.show_license) | |
| self.license_button.pack(pady=10) | |
| def show_license(self): | |
| # resolution | |
| width = 600 | |
| height = 350 | |
| # open a new window with the license | |
| license_window = ctk.CTkToplevel(self) | |
| license_window.title("ISC License") | |
| license_window.geometry(f"{width}x{height}") | |
| # add a text widget with the license | |
| license_text = ctk.CTkTextbox(license_window, wrap="none", height=height, width=width, text_color=get_good_color(), fg_color="transparent") | |
| license_text.pack(pady=20, padx=20, fill="both", expand=True) | |
| license_text.insert("0.0", self.license) | |
| license_text.configure(state="disabled") | |
| license_window.mainloop() | |
| def check_ip(self): | |
| try: | |
| # Get the network from CIDR notation | |
| cidr_txt = self.cidr_entry.get() | |
| # Get the IP address to check | |
| addr_txt = self.ip_entry.get() | |
| # Determine the bitmask as preceding bits of a 32 bit integer | |
| bits = int(cidr_txt.split('/')[1]) | |
| bitmask = ((1 << 32)) - (1 << (32 - bits)) | |
| # shift the bits in the cidr and combine into a single integer | |
| cidr = [int(c) for c in cidr_txt.split('/')[0].split('.')] | |
| cidr = (cidr[0] << 24) + (cidr[1] << 16) + (cidr[2] << 8) + cidr[3] | |
| # shift the bits in the IP address and combine into a single integer | |
| ip = [int(c) for c in addr_txt.split('.')] | |
| ip = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3] | |
| # check if the IP prefix matches the CIDR prefix | |
| if (cidr & bitmask) == (ip & bitmask): | |
| self.result_label.configure(text=f"IP {addr_txt} is in the network {cidr_txt}") | |
| self.result_label.configure(text_color=get_good_color()) | |
| else: | |
| self.result_label.configure(text=f"IP {addr_txt} is NOT in the network {cidr_txt}") | |
| self.result_label.configure(text_color="orange") | |
| except BaseException as e: | |
| self.result_label.configure(text=f"Error: {e}") | |
| self.result_label.configure(text_color="orange") | |
| def main(): | |
| app = App(license) | |
| app.mainloop() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment