Python program for network automation
import os
import socket import subprocess def ping(host): """ Ping the given host to check if it is reachable. """ try: subprocess.check_output(['ping', '-c', '1', host]) print(f'{host} is reachable') except subprocess.CalledProcessError: print(f'{host} is unreachable') def scan_ports(host, start_port, end_port): """ Scan the given host for open ports within the specified range. """ for port in range(start_port, end_port + 1): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) result = sock.connect_ex((host, port)) if result == 0: print(f'Port {port} is open on {host}') sock.close() def get_ip_info(domain): """ Retrieve IP address information for the given domain. """ ip = socket.gethostbyname(domain) print(f'IP address: {ip}') try: name = socket.gethostbyaddr(ip)[0] print(f'Hostname: {name}') except socket.herror: print('Hostname not found') # Example usage host = 'google.com' ping(host) scan_ports(host, 1, 1000) get_ip_info(host) Python program that automates networking tasks such as pinging a remote server, checking for open ports, and retrieving IP address information
Comments
Post a Comment