Monday, April 21, 2025

Get IPv4 and IPv6 Address of Domain - Python

import socket, time

domain_source = {'google.com':'http', 'facebook.com':'http'}

for x, y in domain_source.items():
    time.sleep(2)    
    print(socket.getaddrinfo(x, y))

This Python code imports the 'socket' and 'time' modules using the 'import' keyword. The 'socket' module provides a way to communicate over a network, while the 'time' module provides access to time-related functions.

The code defines a dictionary called 'domain_source' with two key-value pairs. Each key is a domain name (e.g., 'google.com', 'facebook.com') and each value is a string specifying the network protocol to use when connecting to that domain (e.g., 'http', 'https').

The code then enters a 'for' loop that iterates over each key-value pair in the 'domain_source' dictionary. For each iteration of the loop, the code does the following:

  1. Calls the 'time.sleep()' function, which pauses the execution of the script for 2 seconds.

  2. Calls the 'socket.getaddrinfo()' method with two arguments: the domain name (specified by the 'x' variable) and the network protocol (specified by the 'y' variable). This method returns a list of tuples, with each tuple containing information about a possible network address for the specified domain and protocol.

  3. Prints the result of the 'socket.getaddrinfo()' method to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket, time

This line imports the 'socket' and 'time' modules into the script, which provide access to network communication and time-related functions respectively.

domain_source = {'google.com':'http', 'facebook.com':'http'}

This line defines a dictionary called 'domain_source' with two key-value pairs. The keys are domain names and the values are network protocols.

for x, y in domain_source.items():
    time.sleep(2)
    print(socket.getaddrinfo(x, y))

This 'for' loop iterates over each key-value pair in the 'domain_source' dictionary. For each pair, it calls the 'time.sleep()' function to pause the execution of the script for 2 seconds, then calls the 'socket.getaddrinfo()' method with the domain name and network protocol specified in the key-value pair. Finally, it prints the result of the 'socket.getaddrinfo()' method to the console using the 'print()' function.

The 'socket.getaddrinfo()' method returns a list of tuples, with each tuple containing information about a possible network address for the specified domain and protocol. Each tuple contains the following items:

  1. The address family (e.g., AF_INET for IPv4, AF_INET6 for IPv6)

  2. The socket type (e.g., SOCK_STREAM for TCP, SOCK_DGRAM for UDP)

  3. The protocol number (e.g., IPPROTO_TCP for TCP, IPPROTO_UDP for UDP)

  4. The canonical hostname for the address (if available)

  5. The IP address itself

This python script looks up the network addresses for the specified domains using the specified network protocols, and prints the results to the console. It includes a delay of 2 seconds between each lookup to avoid overwhelming the network.

Get Service by Port - Python getservbyport()

Learn More:

Free Python Programming Course
Python Examples
Python How To
Python Modules 

YouTube WebDevPro Tutorials 

import socket, os

#port_list = [21, 22, 23, 139, 137, 80, 443, 445]

while True:
    port = input('Please enter port: ')

    if port == 'q':
        print('Done')
        break
    
    else:
        os.system('cls')
        print('-' * 50)
        print('Result: ' + socket.getservbyport(int(port)))

This Python code performs the following tasks:

  1. Imports the 'socket' and 'os' modules using the 'import' keyword. The 'socket' module provides a way to communicate over a network, while the 'os' module provides a way to interact with the operating system.

  2. Defines a 'while' loop that runs indefinitely.

  3. Within the loop, prompts the user to input a port number using the 'input()' function.

  4. Checks if the user input is equal to the letter 'q'. If so, it prints a message and exits the loop using the 'break' keyword.

  5. If the user input is not equal to 'q', clears the console using the 'os.system()' method, then prints a separator string and the input value to the console using the 'print()' function.

  6. Calls the 'socket.getservbyport()' method, passing the integer value of the input as an argument. This method returns the name of the service associated with the given port number.

  7. Concatenates the returned service name with a message string, and then prints the resulting string to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket, os

This line imports the 'socket' and 'os' modules into the script, which provide access to network communication and operating system functions respectively.

while True:

This line starts an infinite 'while' loop that will keep running until it is explicitly exited.

port = input('Please enter port: ')

This line prompts the user to enter a port number via the console using the 'input()' function, and stores the resulting value in the variable 'port'.

if port == 'q':
    print('Done')
    break

This 'if' statement checks if the user entered the letter 'q'. If so, it prints a message to the console using the 'print()' function and exits the loop using the 'break' keyword.

else:
    os.system('cls')
    print('-' * 50)
    print('Result: ' + socket.getservbyport(int(port)))

If the user did not enter 'q', then this block of code is executed. The 'os.system()' method is called with the argument 'cls', which clears the console screen. The 'print()' function is then used to print a separator string to the console. Finally, the 'socket.getservbyport()' method is called with the integer value of the 'port' variable as an argument, and the returned service name is concatenated with a message string and printed to the console using the 'print()' function.

This Python code prompts the user to enter a port number via the console, then looks up the name of the service associated with that port using the 'socket.getservbyport()' method. The program will continue to prompt the user for input until the letter 'q' is entered, at which point it will exit.

Protocol to Port Number Conversion - Python

import socket

proto_names = ['ftp', 'telnet', 'http', 'https', 'gopher']

for x in proto_names:
    print(x + '\t runs on port: \t' + str(socket.getservbyname(x)))

This Python code performs the following tasks:

  1. Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.

  2. Defines a list 'proto_names' that contains the names of various network protocols such as 'ftp', 'telnet', 'http', 'https', 'gopher'.

  3. Uses a 'for' loop to iterate over each element in the 'proto_names' list.

  4. Within the loop, calls the 'socket.getservbyname()' method, passing the current element of the 'proto_names' list as an argument. This method returns the port number associated with the given service name.

  5. Concatenates the service name and its associated port number as a string, and then prints it to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket

This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.

proto_names = ['ftp', 'telnet', 'http', 'https', 'gopher']

This line defines a list 'proto_names' that contains the names of various network protocols such as 'ftp', 'telnet', 'http', 'https', 'gopher'.

for x in proto_names:

This line starts a 'for' loop that iterates over each element in the 'proto_names' list. The loop variable 'x' takes on the value of each element in the list in turn.

print(x + '\t runs on port: \t' + str(socket.getservbyname(x)))

This line calls the 'socket.getservbyname()' method, passing the current element of the 'proto_names' list as an argument. This method returns the port number associated with the given service name. The returned port number is then converted to a string and concatenated with the service name and some whitespace characters to form a formatted string that specifies the service name and its associated port number. The formatted string is then printed to the console using the 'print()' function.

This Python code retrieves and displays the port numbers associated with a list of common network protocols. For each protocol, the service name and its associated port number are concatenated into a formatted string and printed to the console.

Get a Host by IP Address - gethostbyaddr() Python

gethostbyname() Example: 

import socket

print(socket.gethostbyname('google.com'))

This Python code performs the following tasks:

  1. Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.

  2. Calls the 'socket.gethostbyname()' method, passing the string 'google.com' as an argument. This method returns the IP address of the machine that is associated with the given hostname 'google.com'.

  3. Prints the returned IP address to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket

This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.

print(socket.gethostbyname('google.com'))

This line calls the 'socket.gethostbyname()' method, passing the string 'google.com' as an argument. This method returns the IP address of the machine that is associated with the given hostname 'google.com'. The returned IP address is then printed to the console using the 'print()' function.

Previous code retrieves and displays the IP address of the machine that is associated with the hostname 'google.com'.

gethostrbyaddr() Example

import socket

print(socket.gethostbyaddr('That.IP.Goes.Here'))

This Python code performs the following tasks:

  1. Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.

  2. Calls the 'socket.gethostbyaddr()' method, passing a string 'That.IP.Goes.Here' as an argument. This method returns a tuple containing the hostname, alias list, and IP addresses of the machine that is associated with the given IP address 'That.IP.Goes.Here'.

  3. Prints the returned hostname to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket

This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.

print(socket.gethostbyaddr('That.IP.Goes.Here'))

This line calls the 'socket.gethostbyaddr()' method, passing the string 'That.IP.Goes.Here' as an argument. This method returns a tuple containing the hostname, alias list, and IP addresses of the machine that is associated with the given IP address 'That.IP.Goes.Here'. The returned tuple is then printed to the console using the 'print()' function.

The tuple returned by 'socket.gethostbyaddr()' contains the following values:

  1. The primary hostname for the IP address.
  2. A list of alternate hostnames for the IP address.
  3. A list of IP addresses associated with the hostname.

If the IP address provided does not resolve to a valid hostname, then an exception is raised.

This code retrieves and displays the hostname associated with the IP address 'That.IP.Goes.Here'.

Get Real IP Address from Localhost Name - Python

import socket

name_of_this_pc = socket.gethostname()

print("Name: " + name_of_this_pc)

print("IP: " + socket.gethostbyname(name_of_this_pc))

This Python code performs the following tasks:

  1. Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.

  2. Calls the 'socket.gethostname()' method, which returns the hostname of the current machine on which the Python script is running. This hostname is assigned to a variable called 'name_of_this_pc'.

  3. Prints a string to the console, which contains the hostname of the machine. The 'print()' function is used to output the string, which includes the 'Name:' label followed by the value of the 'name_of_this_pc' variable.

  4. Calls the 'socket.gethostbyname()' method, passing the 'name_of_this_pc' variable as an argument. This method returns the IP address of the machine with the given hostname.

  5. Prints a string to the console that includes the IP address of the machine. The 'print()' function is used to output the string, which includes the 'IP:' label followed by the value of the 'socket.gethostbyname(name_of_this_pc)' method.

Let's break down each line of code in more detail:

import socket

This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.

name_of_this_pc = socket.gethostname()

This line calls the 'socket.gethostname()' method, which returns the hostname of the current machine. The returned value is then assigned to the 'name_of_this_pc' variable for later use.

print("Name: " + name_of_this_pc)

This line prints a string to the console, which includes the hostname of the machine. The 'print()' function is used to output the string, which includes the 'Name:' label followed by the value of the 'name_of_this_pc' variable.

print("IP: " + socket.gethostbyname(name_of_this_pc))

This line calls the 'socket.gethostbyname()' method, passing the 'name_of_this_pc' variable as an argument. This method returns the IP address of the machine with the given hostname. The 'print()' function is then used to output a string to the console that includes the IP address of the machine. The string includes the 'IP:' label followed by the value of the 'socket.gethostbyname(name_of_this_pc)' method.

This script retrieves and displays the hostname and IP address of the machine on which it is running.

Doman Bulk Lookup Tool - Python Multi FQDN

import socket, time

list_domains = ['google.com', 'facebook.com', 'bing.com']

file = open('fqdn-bulk-report.txt', 'a')

for x in list_domains:
    #print(socket.getfqdn(x))
    file.write(socket.getfqdn(x) + '\n')
    time.sleep(2)

file.close()

This Python code performs the following tasks:

  1. Imports the modules 'socket' and 'time' using the 'import' keyword.
  2. Defines a list called 'list_domains' containing three domain names: 'google.com', 'facebook.com', and 'bing.com'.
  3. Opens a file called 'fqdn-bulk-report.txt' in 'append' mode and assigns it to a variable called 'file'. This file will be used to store the Fully Qualified Domain Names (FQDNs) of the domains in 'list_domains'.
  4. Iterates over each element in the 'list_domains' using a 'for' loop, and performs the following tasks:
    • Calls the 'socket.getfqdn()' method on each element to retrieve its FQDN.
    • Writes the FQDN to the 'fqdn-bulk-report.txt' file using the 'file.write()' method.
    • Pauses the program for 2 seconds using the 'time.sleep()' method.
  5. Closes the 'fqdn-bulk-report.txt' file using the 'file.close()' method.

Let's go through the code line by line to understand it in greater detail:

import socket, time

This line imports the 'socket' and 'time' modules into the script. The 'socket' module provides a way to communicate over a network, while the 'time' module provides functions to work with time-related tasks.

list_domains = ['google.com', 'facebook.com', 'bing.com']

This line creates a list called 'list_domains' containing three strings representing domain names: 'google.com', 'facebook.com', and 'bing.com'.  

file = open('fqdn-bulk-report.txt', 'a')

This line creates a file object called 'file' using the 'open()' function. The first argument is the name of the file to open, and the second argument specifies the mode in which to open it. In this case, 'a' stands for 'append', which means that new data will be added to the end of the file, rather than overwriting it 

for x in list_domains:

This line begins a 'for' loop that iterates over each element in 'list_domains'. The loop variable 'x' is assigned to each element in turn.

file.write(socket.getfqdn(x) + '\n')

This line calls the 'socket.getfqdn()' method with the current value of 'x' as an argument. This method returns the Fully Qualified Domain Name (FQDN) of the domain name passed to it. The FQDN includes the hostname and the domain name, separated by periods. The resulting string is then written to the 'fqdn-bulk-report.txt' file using the 'file.write()' method. The '\n' character is added to the end of the string to ensure that each FQDN is written on a new line.

time.sleep(2)

This line pauses the program for 2 seconds using the 'time.sleep()' method. This is done to prevent overwhelming the servers being queried with too many requests in a short period of time.

file.close()

This line closes the 'fqdn-bulk-report.txt' file using the 'file.close()' method. This ensures that any data written to the file is saved and that the file is released from memory.

Get Fully Qualified Domain Name - Python FQDN

import os, socket

os.system('cls')
print('FQDN Extractor: v 0.1')
print('-' * 79)

target = input('Enter Domain to Check: ')
print('FQDN: ', socket.getfqdn(target))

This code is a Python script that takes a user input domain and returns the fully qualified domain name (FQDN) of the target domain.

Let's go through the code line by line:

  1. import os, socket - This line imports the os and socket modules for use in the script.

  2. os.system('cls') - This line clears the console screen, so that the user input prompt and output are more visible.

  3. print('FQDN Extractor: v 0.1') - This line prints a title for the script.

  4. print('-' * 79) - This line prints a separator to visually separate the title from the rest of the output.

  5. target = input('Enter Domain to Check: ') - This line prompts the user to enter a domain to check and assigns the input to the variable target.

  6. print('FQDN: ', socket.getfqdn(target)) - This line gets the FQDN of the target domain using the socket.getfqdn() function and prints it to the console.

This code is fairly straightforward and serves its purpose.

Get the System Hostname using Python - gethostname()

import socket

print(socket.gethostname())

This code uses the socket module to print the hostname of the machine it is run on.

Explanation:

  1. import socket imports the socket module.

  2. socket.gethostname() returns a string representing the hostname of the current machine.

  3. print(socket.gethostname()) prints the hostname on the console.

Domain to IP Bulk Lookup Tool - Python

Content of bulk-domains.txt

google.com
bing.com
yahoo.com
facebook.com
import socket

file = open('bulk-domains.txt', 'r')

domain_list = []

for x in file.readlines():
    domain_list.append(x.rstrip())

for y in domain_list:
    print(y + ' -> ' + socket.gethostbyname(y))

Detailed explanation of the code:

  1. import socket - The socket module is imported to enable socket programming in Python.
  2. file = open('bulk-domains.txt', 'r') - This line of code opens a file named bulk-domains.txt in read mode and assigns it to the variable file.
  3. domain_list = [] - An empty list named domain_list is created to hold the domain names from the bulk-domains.txt file.
  4. for x in file.readlines(): - The readlines() method reads each line of the bulk-domains.txt file and returns a list of strings. The for loop iterates over each string in the list.
  5. domain_list.append(x.rstrip()) - The rstrip() method removes the newline character at the end of each string, and the resulting string is appended to the domain_list.
  6. for y in domain_list: - The for loop iterates over each item in the domain_list.
  7. socket.gethostbyname(y) - The gethostbyname() method takes a domain name as an argument and returns its corresponding IP address.
  8. print(y + ' -> ' + socket.gethostbyname(y)) - The domain name and its corresponding IP address are printed to the console with an arrow symbol between them.

This code reads a list of domain names from a file, obtains their IP addresses using the socket module, and prints the domain name and IP address pairs to the console.

Continous Ping with User Input - Python

import os

while True:
    os.system('cls')
    print('-' * 79)
    print('Custom Constant Pinger v0.1')

    target = input('Please, enter IP to ping: ')

    if target == 'quit':
        print('Script terminad')
        break
    else:
        os.system('ping ' + target)

This Python code allows a user to enter an IP address and then uses the command prompt to continuously ping that IP address until the user enters "quit" to end the program. Here's a step-by-step explanation of the code:

  1. The code imports the 'os' module, which provides a way of using operating system dependent functionality, such as executing command prompt commands.

  2. The code uses a while loop with the condition 'True' which means the loop will continue to run until a 'break' statement is encountered.

  3. Inside the while loop, the code first clears the command prompt screen using the 'os.system' method to execute the 'cls' command, which clears the console screen on Windows systems.

  4. Then, it prints a separator line of 79 characters and the name of the program 'Custom Constant Pinger v0.1'.

  5. The code prompts the user to enter an IP address to ping by using the 'input' function and storing the user input in the 'target' variable.

  6. The code then checks if the user entered the string 'quit'. If so, it prints 'Script terminated' and breaks out of the loop.

  7. If the user did not enter 'quit', the code uses the 'os.system' method to execute the 'ping' command followed by the IP address entered by the user. This will ping the target IP address continuously until the user manually stops the program.

Overall, this code provides a simple way to ping a specified IP address continuously in the command prompt. However, it is important to note that continuous pinging can potentially cause network congestion and other issues, so it should be used with caution.

Domain to IP Converter - Python

import socket, os

list_dom = ['google.com', 'yahoo.com', 'bing.com']

file = open('host-to-ip.txt', 'a')

for x in list_dom:
    #print('IP of domain: ' + x + socket.gethostbyname(x))
    file.write('IP of domain: ' + x + ' -> ' + socket.gethostbyname(x) + '\n')

file.close()

  1. Import the necessary modules socket and os.
  2. Create a list of domains to resolve their IP addresses and store it in the variable list_dom.
  3. Open a new file host-to-ip.txt in append mode and assign it to the variable file.
  4. Iterate over each domain in the list_dom using a for loop: a. Use socket.gethostbyname(x) to resolve the IP address of the current domain x. b. Write the domain name and its IP address to the host-to-ip.txt file.
  5. Close the file.

Explanation of the code: The purpose of this code is to resolve the IP addresses of a list of domains and store them in a file.

The first line of code imports the necessary modules socket and os.

In the next line, a list of domains to resolve their IP addresses is created and stored in the variable list_dom.

The next line opens a new file host-to-ip.txt in append mode and assigns it to the variable file.

The for loop iterates over each domain in the list_dom using the variable x. Inside the for loop, socket.gethostbyname(x) is used to resolve the IP address of the current domain x. The resolved IP address is then written to the file host-to-ip.txt using the write() method of the file object. The string written to the file includes the domain name and its IP address separated by '->'.

Finally, the file is closed using the close() method of the file object. 

Hostname to IP Address Lookup - gethostbyname() Python

import socket, os

list_dom = ['google.com', 'yahoo.com', 'bing.com']

for x in list_dom:
    print('IP of domain: ' + x + socket.gethostbyname(x))

This Python code imports two modules, socket and os. The socket module provides low-level network communications functionality, while os provides a way to interact with the operating system.

The code defines a list of domains called list_dom, which includes google.com, yahoo.com, and bing.com.

The code then iterates over each domain in the list using a for loop. Within the loop, the IP address associated with the current domain is obtained using the socket.gethostbyname() method. This method takes a domain name as an argument and returns the IP address that corresponds to that domain.

The code then concatenates the string "IP of domain:" with the current domain name and its corresponding IP address, which is printed to the console.

This code retrieves the IP addresses associated with a list of domains and prints them to the console.

import socket, os

os.system('cls')

while True:
    dom = input('Enter domain: ')
    print(socket.gethostbyname(dom))

    if dom == 'q':
        print('Done ')
        break

This Python code imports the 'socket' and 'os' modules. The 'socket' module provides a way to access the network and communicate with other computers through various socket types such as TCP and UDP. The 'os' module provides a way to interact with the underlying operating system, for example by calling system functions or clearing the console.

The code clears the console using the 'os' module. Then it enters an infinite loop, prompting the user to enter a domain name to be resolved to an IP address. The 'input' function reads the user's input as a string and stores it in the 'dom' variable.

The 'socket.gethostbyname' function is called with the 'dom' variable as an argument. This function returns the IP address associated with the specified domain name. The IP address is printed to the console using the 'print' function.

The code then checks if the user entered 'q' to quit. If the user entered 'q', the loop is broken and the code exits. Otherwise, the loop continues and prompts the user for another domain name to resolve.

Overall, this code allows the user to easily resolve domain names to IP addresses using the 'socket' module and provides a simple user interface for repeated lookups.

IP Address Validity Check - Python

import os, ipaddress

os.system('cls')

while True:
    ip = input('IP Validity Check: ')
    try:
        print(ipaddress.ip_address(ip))
        print('IP Valid')
    except:
        print('-' * 79)
        print('IP Not Valid !!!!')
    finally:
        if ip == 'q':
            print('Script Finished')
            break

  1. The first line of code imports the operating system and ipaddress modules.
  2. The os.system('cls') line clears the console screen.
  3. The while True: statement initiates an infinite loop.
  4. The ip = input('IP Validity Check: ') line prompts the user to enter an IP address to be checked for validity and stores it in the ip variable.
  5. The try: statement initiates a try-except block, which is used to handle errors in the code.
  6. The ipaddress.ip_address(ip) line checks whether the IP address entered by the user is valid or not by passing it to the ip_address() method of the ipaddress module. If it is valid, the method returns the IP address object, and the code inside the try block is executed.
  7. The print('IP Valid') line prints "IP Valid" to the console if the IP address is valid.
  8. The except: statement catches any exception that occurs in the try block.
  9. The print('-' * 79) line prints a line of 79 dashes to the console to visually separate the error message from the rest of the output.
  10. The print('IP Not Valid !!!!') line prints "IP Not Valid !!!!" to the console if the IP address entered by the user is not valid.
  11. The finally: statement is executed whether or not an exception is caught in the try block.
  12. The if ip == 'q': statement checks if the user has entered "q" to quit the program. If so, the break statement is executed, which exits the while loop and terminates the program.

Simple version (for one IP) - no error checking

import os, ipaddress

os.system('cls')

ip = input('IP Validity Check: ')

if ipaddress.ip_address(ip):
    print('OK')

The given code is used to check the validity of an IP address. Here's how it works:

  1. The code begins by importing the 'os' and 'ipaddress' modules.

  2. It then clears the console screen using the 'os.system' function with the 'cls' argument.

  3. The user is prompted to enter an IP address to check its validity using the 'input' function.

  4. The 'ipaddress.ip_address' function is used to check if the entered IP address is valid.

  5. If the IP address is valid, the program prints 'OK', indicating that the IP address is valid.

  6. If the IP address is not valid, the program raises an exception, indicating that the IP address is not valid.

In summary, the program checks the validity of the entered IP address and prints 'OK' if the IP address is valid. If the IP address is not valid, an exception is raised.

Creating Custom URL Query Strings in Python

se = 'testengine.com/?p='

list_el = ['telnet', 'ftp', 'usenet', 'gopher']

file = open('report.txt', 'w')

for x in list_el:
    file.write(se + x + '\n')
    print(se + x)

file.close()

The given code is used to write URLs for different internet protocols in a file named "report.txt". Here's a detailed explanation of the code:

  1. se = 'testengine.com/?p=': This line initializes a variable se with a string value of testengine.com/?p=. This will be used as the base URL for each protocol.

  2. list_el = ['telnet', 'ftp', 'usenet', 'gopher']: This line initializes a list named list_el with four different string values, representing different internet protocols.

  3. file = open('report.txt', 'w'): This line opens a new file named "report.txt" in write mode and assigns it to the variable file. The 'w' mode specifies that the file is opened in write mode, which allows new data to be written to the file.

  4. for x in list_el:: This line starts a for loop that iterates over each element in the list_el list. On each iteration, the variable x is assigned to the current element in the list.

  5. file.write(se + x + '\n'): This line writes the base URL (se), the current protocol (x), and a newline character (\n) to the file. This creates a new line in the file for each protocol.

  6. print(se + x): This line prints the same string to the console as is being written to the file. This allows the user to see the progress of the loop in real-time.

  7. file.close(): This line closes the file that was opened in the second step. This is important to ensure that all data is written to the file before it is closed, and to prevent any data loss.

Crafting Custom Ping in Python

import os

ip_dest = input("Enter IP addr to Check: ")
icmp_num = input("ICMP Echo Number: ")

os.system("ping -n {} {}".format(icmp_num, ip_dest))

This Python code demonstrates how to use the os module to execute a ping command and check the response from a specific IP address. Below is a detailed explanation of how the code works:

  1. First, the code prompts the user to enter the IP address to check and the number of ICMP echo requests to send to the target host.
  2. The os.system() function is then used to execute a ping command to the specified IP address with the number of ICMP echo requests specified by the user.
  3. The ping command sends ICMP echo requests to the specified IP address and waits for a response. If the target host responds, it sends an ICMP echo reply message back to the sender.
  4. The response from the ping command is then printed to the console. If the target host responds to the ICMP echo requests, the output will show the number of packets sent, received, lost, and the response time.
  5. If the target host does not respond to the ICMP echo requests, the output will show that all packets were lost.

In summary, this code demonstrates a simple way to check if a specific IP address is reachable on the network using ICMP echo requests.

Ping multiple IPs from list:

import os

ip_list = ['127.0.0.1', '192.168.0.103']

icmp_num = input("ICMP Echo Number: ")

for x in ip_list:
    os.system("ping -n {} {}".format(icmp_num, x))

This Python code is a simple script that allows the user to enter a list of IP addresses and an ICMP echo number, and then pings each IP address in the list using the specified number of ICMP echoes. Here's a step-by-step explanation of what the code does:

  1. The code imports the "os" module, which provides a way to interact with the operating system.

  2. The code creates a list called "ip_list" containing two IP addresses - '127.0.0.1' and '192.168.0.103'.

  3. The code prompts the user to enter an ICMP echo number using the "input()" function and stores the result in the variable "icmp_num".

  4. The code uses a "for" loop to iterate over each IP address in the "ip_list".

  5. Inside the loop, the code calls the "os.system()" function to execute the "ping" command with the specified number of ICMP echoes and IP address using string formatting.

  6. The output of each "ping" command is printed to the console.

  7. The loop repeats for each IP address in the "ip_list".

In summary, this code allows the user to quickly ping multiple IP addresses with a specified number of ICMP echoes, which can be useful for testing network connectivity or troubleshooting network issues.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...