Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
netscan
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hamid
netscan
Commits
94e6dce9
Commit
94e6dce9
authored
1 year ago
by
Hamid
Browse files
Options
Downloads
Patches
Plain Diff
Delete netscan.py
parent
e1d06770
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
netscan.py
+0
-162
0 additions, 162 deletions
netscan.py
with
0 additions
and
162 deletions
netscan.py
deleted
100644 → 0
+
0
−
162
View file @
e1d06770
import
sys
import
getopt
import
socket
,
struct
from
netaddr
import
*
from
datetime
import
date
import
json
,
csv
import
subprocess
,
platform
import
logging
today
=
date
.
today
()
today_dot
=
today
.
strftime
(
'
%d.%m.%Y
'
)
logging
.
basicConfig
(
filename
=
'
logs/log_%s
'
%
today
,
filemode
=
'
w+
'
,
format
=
'
%(asctime)s %(levelname)s %(message)s
'
,
datefmt
=
'
%Y-%m-%d %H:%M:%S
'
,
level
=
logging
.
DEBUG
)
version
=
'
1.0
'
verbose
=
False
ip_range
=
[]
ip_ranges
=
[]
alive_devices
=
[]
hostname
=
{}
def
help
():
print
(
'''
Scans the specified network to find and list alive nodes.
Syntax: /usr/bin/python netscan.py [-n Network -p Port -f FilePath ] ...
example: /usr/bin/python netscan.py -n 192.168.1.0/24 -p 22 -f /etc/output.json
/usr/bin/python netscan.py -n 192.168.1.16/30,192.168.2.2/32 -p 22 -f /etc/output.json
args:
-h --help Help.
-v --version Shows version.
-n --network Network(S) to scan.
-f --file-path File path containing file name to save the output.
You can write the output in .csv, .txt or .json file formats.
© 2023 Hamid Sadeghian
'''
)
def
portscan
(
ips
,
port
):
for
ip
in
ips
:
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
.
settimeout
(
1
)
result
=
sock
.
connect_ex
((
ip
,
int
(
port
)))
if
result
==
0
:
alive_devices
.
append
(
ip
)
print
(
ip
,
'
port {} is open.
'
.
format
(
port
))
logging
.
info
(
'
{} port {} is open.
'
.
format
(
ip
,
port
))
sock
.
close
()
return
alive_devices
def
pingscan
(
ips
):
ping_str
=
'
-n 2
'
if
platform
.
system
().
lower
()
==
'
windows
'
else
'
-c 2
'
need_sh
=
False
if
platform
.
system
().
lower
()
==
'
windows
'
else
True
for
ip
in
ips
:
args
=
'
ping
'
+
'
'
+
ping_str
+
'
'
+
ip
# Ping
if
subprocess
.
call
(
args
,
shell
=
need_sh
)
==
0
:
alive_devices
.
append
(
ip
)
return
alive_devices
def
ips
(
start
,
end
):
start
=
struct
.
unpack
(
'
>I
'
,
socket
.
inet_aton
(
start
))[
0
]
end
=
struct
.
unpack
(
'
>I
'
,
socket
.
inet_aton
(
end
))[
0
]
return
[
socket
.
inet_ntoa
(
struct
.
pack
(
'
>I
'
,
i
))
for
i
in
range
(
start
,
end
)]
def
nslook
(
alive_devices
):
hostname
=
{}
for
i
in
alive_devices
:
try
:
p
=
(
socket
.
getnameinfo
((
i
,
0
),
socket
.
NI_NAMEREQD
))
hostname
[
i
]
=
p
[
0
]
except
:
hostname
[
i
]
=
'
no dns record
'
pass
return
hostname
def
main
(
argv
):
file
=
None
networks
=
[]
network
=
''
logging
.
info
(
'
START
'
)
opts
,
args
=
getopt
.
getopt
(
argv
[
1
:],
'
n:f:p:hv
'
,[
'
help
'
,
'
version
'
,
'
network=
'
,
'
file-path=
'
,
'
port=
'
])
for
opt
,
arg
in
opts
:
if
opt
in
(
'
-h
'
,
'
--help
'
):
help
()
sys
.
exit
()
elif
opt
in
(
'
-v
'
,
'
--version
'
):
print
(
'
Version
'
,
version
)
sys
.
exit
()
elif
opt
in
(
'
-n
'
,
'
--network
'
):
network
=
arg
networks
=
network
.
split
(
'
,
'
)
elif
opt
in
(
'
-f
'
,
'
--file-path
'
):
file
=
arg
elif
opt
in
(
'
-p
'
,
'
--port
'
):
port
=
arg
if
any
((
not
network
,
not
file
,
not
port
)):
help
()
print
(
'
\n
ERROR: Mandatory parameter is missing.
\n
'
)
logging
.
error
(
'
Mandatory parameters are missing.
'
)
sys
.
exit
()
# find out ip ranges
for
n
in
networks
:
ip_ranges
=
IPNetwork
(
str
(
n
))
for
ip
in
ip_ranges
:
ip_range
.
append
(
str
(
ip
))
print
(
networks
,
'
will be scaned.
'
)
logging
.
info
(
'
%s will be scaned.
'
%
networks
)
#input('Press Enter to Continue...')
alive_devices
=
portscan
(
ip_range
,
port
)
print
(
len
(
ip_range
),
'
IPs scaned.
'
)
logging
.
info
(
'
%s IPs scaned.
'
%
len
(
ip_range
))
hosts
=
nslook
(
alive_devices
)
header
=
[
'
IP-Address
'
,
'
Hostname
'
]
try
:
with
open
(
file
,
'
w
'
,
encoding
=
'
UTF8
'
,
newline
=
''
)
as
f
:
if
'
.json
'
in
file
:
json
.
dump
(
hosts
,
f
,
indent
=
2
)
elif
'
.csv
'
in
file
:
writer
=
csv
.
DictWriter
(
f
,
fieldnames
=
header
)
writer
.
writeheader
()
for
key
in
hosts
.
keys
():
f
.
write
(
'
%s,%s
\n
'
%
(
key
,
hosts
[
key
]))
elif
'
.txt
'
in
file
:
for
key
in
hosts
.
keys
():
f
.
write
(
'
%s %s
\n
'
%
(
key
,
hosts
[
key
]))
else
:
print
(
'
Unsupported file extension!
'
)
logging
.
error
(
'
Unsupported file extension!
'
)
logging
.
info
(
'
List of hosts inclusive DNS-Name saved in {}
'
.
format
(
file
))
except
IOError
:
print
(
'
Error occurred while exporting to file: {0}
'
.
format
(
file
))
logging
.
error
(
'
Error occurred while exporting to file: {0}
'
.
format
(
file
))
print
(
'
\n
'
,
len
(
hosts
),
'
IP(s) with open port 22 found:
'
,
hosts
,
'
\n
'
)
logging
.
info
(
'
{} IP(s) with open port 22 found: {}
'
.
format
(
len
(
hosts
),
hosts
))
if
__name__
==
'
__main__
'
:
main
(
sys
.
argv
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment