Advertisement
EdmundC

pingsend

Oct 6th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import socket
  2. import os
  3.  
  4. def send_image(image_path, ip):
  5.     """Send image bytes to a specific IP address."""
  6.     # Create a UDP socket
  7.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  8.  
  9.     # Read the image file in binary mode
  10.     with open(image_path, 'rb') as file:
  11.         image_data = file.read()
  12.  
  13.     try:
  14.         # Send the image data
  15.         sock.sendto(image_data, (ip, 12345))  # Use a port of your choice
  16.         print(f"Sent image to {ip}")
  17.     except Exception as e:
  18.         print(f"Failed to send image to {ip}: {e}")
  19.     finally:
  20.         sock.close()
  21.  
  22. def ping_image(image_path, ip_range):
  23.     """Ping specified IPs with the given image."""
  24.     # Split the IP range into start and end
  25.     start_ip, end_ip = ip_range.split('-')
  26.    
  27.     # Convert the start and end IPs to integers for iteration
  28.     start = list(map(int, start_ip.split('.')))
  29.     end = list(map(int, end_ip.split('.')))
  30.    
  31.     for i in range(start[2], end[2] + 1):  # Modify based on subnet
  32.         for j in range(start[3], end[3] + 1):
  33.             ip = f"{start[0]}.{start[1]}.{start[2]}.{j}"
  34.             send_image(image_path, ip)
  35.  
  36. def main():
  37.     """Main function to execute commands."""
  38.     while True:
  39.         command = input("Enter command: ")
  40.         if command.startswith('/ping'):
  41.             try:
  42.                 _, image_file, ip_range = command.split()
  43.                 if not os.path.isfile(image_file):
  44.                     print("Image file does not exist.")
  45.                     continue
  46.                 ping_image(image_file, ip_range)
  47.             except ValueError:
  48.                 print("Invalid command format. Use: /ping image.filetype ip-ip")
  49.         elif command.lower() == 'exit':
  50.             print("Exiting the program.")
  51.             break
  52.         else:
  53.             print("Unknown command. Type '/ping image.filetype ip-ip'.")
  54.  
  55. if __name__ == "__main__":
  56.     main()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement