Skip to content

Commit 900c92c

Browse files
committed
Add port scanning script.
1 parent a8715c9 commit 900c92c

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Package/Script Name
2+
Port Scanner
3+
## Short description of package/script
4+
5+
- Use nmap to scan and detect the specified server port
6+
- You can also use this script to customize it to achieve the effect you want.
7+
8+
## Setup instructions
9+
1. First you need to install nmap, centos as an example.
10+
11+
```
12+
yum install nmap
13+
```
14+
15+
2. Installing NAMP's Python library
16+
17+
```
18+
pip3 install python-nmap
19+
```
20+
21+
## Output
22+
images link :
23+
Images/output.png
24+
![preview]()
25+
26+
27+
## Author(s)
28+
Ethan
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import nmap
3+
4+
def main():
5+
input_data = input("Please input hosts and ports (e.g., '192.168.1.1 80,443'): ")
6+
scan_row = input_data.strip().split()
7+
8+
if len(scan_row) != 2:
9+
print("Error! Please provide both hosts and ports.")
10+
sys.exit(1)
11+
12+
hosts = scan_row[0]
13+
ports = scan_row[1]
14+
15+
try:
16+
nm = nmap.PortScanner()
17+
except nmap.PortScannerError as e:
18+
print(e)
19+
print("Nmap not found! Ensure that Nmap is installed.")
20+
sys.exit(1)
21+
except Exception as e:
22+
print(f"Unexpected error: {str(e)}")
23+
sys.exit(1)
24+
25+
try:
26+
print(f"Scanning {hosts} on ports {ports}...")
27+
nm.scan(hosts=hosts, arguments=f'-v -sS -p {ports}')
28+
except Exception as e:
29+
print(f"Scan error: {str(e)}")
30+
sys.exit(1)
31+
32+
for host in nm.all_hosts():
33+
print('-------------------------------------------')
34+
print(f'Host : {host} ({nm[host].hostname()})')
35+
print(f'State : {nm[host].state()}')
36+
37+
for proto in nm[host].all_protocols():
38+
print('-------------------')
39+
print(f'Protocol : {proto}')
40+
ports = sorted(nm[host][proto].keys())
41+
for port in ports:
42+
state = nm[host][proto][port]['state']
43+
print(f'Port : {port}\tState : {state}')
44+
45+
if __name__ == "__main__":
46+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-nmap=>0.7.1

0 commit comments

Comments
 (0)