Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F4251618
scanport3.py
All Users
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown Object (User)
Mar 28 2024, 2:20 AM
2024-03-28 02:20:29 (UTC+0)
Size
1 KB
Referenced Files
None
Subscribers
None
scanport3.py
View Options
#!/usr/bin/env python3
import
nmap
import
sys
def
read_ports_from_file
(
file_name
):
ports
=
{}
try
:
with
open
(
file_name
,
'r'
)
as
file
:
for
line
in
file
:
line
=
line
.
strip
()
if
line
and
not
line
.
startswith
(
"#"
):
# Skip comment lines
parts
=
line
.
split
(
'#'
)
port_number
=
parts
[
0
]
.
strip
()
port_description
=
parts
[
1
]
.
strip
()
if
len
(
parts
)
>
1
else
""
ports
[
port_number
]
=
port_description
except
FileNotFoundError
:
print
(
f
"File {file_name} not found."
)
return
ports
def
scan_ports
(
remote_host
,
ports
):
if
not
ports
:
print
(
"No ports to scan."
)
return
open_ports
=
[]
closed_ports
=
[]
nm
=
nmap
.
PortScanner
()
arguments
=
f
'-T4 -p{",".join(ports)}'
nm
.
scan
(
hosts
=
remote_host
,
arguments
=
arguments
)
for
host
in
nm
.
all_hosts
():
for
proto
in
nm
[
host
]
.
all_protocols
():
lport
=
nm
[
host
][
proto
]
.
keys
()
for
port
in
lport
:
state
=
nm
[
host
][
proto
][
port
][
'state'
]
if
state
==
'open'
:
open_ports
.
append
((
port
,
ports
.
get
(
str
(
port
),
"No description available"
)))
else
:
closed_ports
.
append
((
port
,
ports
.
get
(
str
(
port
),
"No description available"
)))
print
(
"
\n
Scanning completed."
)
print
(
"Open ports:"
)
print
(
"Port
\t
Description"
)
for
port
,
description
in
open_ports
:
print
(
f
"{port}
\t
{description}"
)
print
(
"
\n
Closed ports:"
)
print
(
"Port
\t
Description"
)
for
port
,
description
in
closed_ports
:
print
(
f
"{port}
\t
{description}"
)
def
main
():
remote_host
=
input
(
"Enter remote host: "
)
ports
=
read_ports_from_file
(
'ports.txt'
)
scan_ports
(
remote_host
,
ports
)
if
__name__
==
"__main__"
:
main
()
File Metadata
Details
Attached
Mime Type
text/x-python
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
454514
Default Alt Text
scanport3.py (1 KB)
Attached To
Mode
T6181: A feature for checking popular ports.
Attached
Detach File
Event Timeline
Log In to Comment