|
1 | 1 | import socket |
2 | 2 | import os |
3 | 3 | import requests |
4 | | -import nmap3 # pip install python3-nmap |
| 4 | +import nmap3 # pip install python3-nmap |
5 | 5 |
|
6 | 6 | """ |
7 | | - First Python Program |
8 | | - Just to see if we can run python3 successfully |
9 | | - and modules are installed or not ;) |
| 7 | + Python Script for Testing Environment |
| 8 | + Checks system information, IPs, and nmap functionality. |
10 | 9 | """ |
11 | 10 |
|
12 | 11 | name = "Sanjeev" |
13 | | -print("Hello "+name+"\n") |
14 | | -print("O.S. is: " + os.name) |
15 | | - |
16 | | -hostname = socket.gethostname() |
17 | | -local_ip = socket.gethostbyname(hostname) |
18 | | -public_ip = requests.get('https://checkip.amazonaws.com').text.strip() |
19 | | -print("Your Computer Name is:" + hostname) |
20 | | -print("Your Computer Local IP Address is:" + local_ip) |
21 | | -print("Your Computer Public IP Address is:" + public_ip) |
| 12 | +print(f"Hello {name}\n") |
| 13 | +print(f"O.S. is: {os.name}") |
| 14 | + |
| 15 | +# Retrieve host information |
| 16 | +try: |
| 17 | + hostname = socket.gethostname() |
| 18 | + local_ip = socket.gethostbyname(hostname) |
| 19 | + print(f"Your Computer Name is: {hostname}") |
| 20 | + print(f"Your Computer Local IP Address is: {local_ip}") |
| 21 | +except Exception as e: |
| 22 | + print(f"Error retrieving local IP: {e}") |
| 23 | + |
| 24 | +# Retrieve public IP |
| 25 | +try: |
| 26 | + public_ip = requests.get('https://checkip.amazonaws.com').text.strip() |
| 27 | + print(f"Your Computer Public IP Address is: {public_ip}") |
| 28 | +except Exception as e: |
| 29 | + print(f"Error retrieving public IP: {e}") |
| 30 | + |
22 | 31 | print("\nChecking if nmap works:\nStarting nmap scan now...\n") |
23 | 32 |
|
24 | | -# make nmap object |
| 33 | +# Initialize nmap object |
25 | 34 | nmap = nmap3.Nmap() |
26 | 35 |
|
27 | | -#nmap version |
28 | | -nmap_version = nmap.nmap_version() |
29 | | -print (f"Nmap version: {nmap_version}") |
| 36 | +# Get nmap version |
| 37 | +try: |
| 38 | + nmap_version = nmap.nmap_version() |
| 39 | + print(f"Nmap version: {nmap_version}") |
| 40 | +except Exception as e: |
| 41 | + print(f"Error retrieving nmap version: {e}") |
30 | 42 |
|
31 | | -# scanning practical-devsecops.com |
| 43 | +# Perform a top ports scan |
32 | 44 | url = 'aliencoders.org' |
33 | | -"""" |
34 | | -print("Starting nmap scan for url: " + url) |
35 | | -results = nmap.nmap_version_detection(url) |
36 | | -print(f"Here is the result for {url}: {results}") |
37 | | -""" |
38 | | -top_ports = nmap.scan_top_ports(url, args="-sV") |
39 | | -print(f"These are the top ports: {top_ports}") |
| 45 | +try: |
| 46 | + print(f"Starting nmap scan for URL: {url}") |
| 47 | + top_ports = nmap.scan_top_ports(url, args="-sV") |
| 48 | + print(f"Top ports for {url}: {top_ports}") |
| 49 | +except Exception as e: |
| 50 | + print(f"Error during nmap scan: {e}") |
0 commit comments