FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_reference/useful_scripts/ping.py at master · wavelets/python_reference · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
wavelets
/
python_reference
Public
forked from
rasbt/python_reference
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python_reference
/
useful_scripts
/
ping.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (28 loc) · 1.12 KB
Breadcrumbs
python_reference
/
useful_scripts
/
ping.py
Copy path
File metadata and controls
39 lines (28 loc) · 1.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Example run:
# ~/Desktop >sudo python3 ping.py
# 0.017251014709472656
# Original source: https://gist.github.com/pyos/10980172
import
time
import
random
import
select
import
socket
def
chk
(
data
):
x
=
sum
(
a
+
b
*
256
for
a
,
b
in
zip
(
data
[::
2
],
data
[
1
::
2
]
+
b'
\x00
'
))
&
0xFFFFFFFF
x
=
(
x
>>
16
)
+
(
x
&
0xFFFF
)
x
=
(
x
>>
16
)
+
(
x
&
0xFFFF
)
return
(
~
x
&
0xFFFF
).
to_bytes
(
2
,
'little'
)
def
ping
(
addr
,
timeout
=
1
):
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_RAW
,
socket
.
IPPROTO_ICMP
)
as
conn
:
payload
=
random
.
randrange
(
0
,
65536
).
to_bytes
(
2
,
'big'
)
+
b'
\x01
\x00
'
packet
=
b'
\x08
\x00
'
+
b'
\x00
\x00
'
+
payload
packet
=
b'
\x08
\x00
'
+
chk
(
packet
)
+
payload
conn
.
connect
((
addr
,
80
))
conn
.
sendall
(
packet
)
start
=
time
.
time
()
while
select
.
select
([
conn
], [], [],
max
(
0
,
start
+
timeout
-
time
.
time
()))[
0
]:
packet
=
conn
.
recv
(
1024
)[
20
:]
unchecked
=
packet
[:
2
]
+
b'
\0
\0
'
+
packet
[
4
:]
if
packet
==
b'
\0
\0
'
+
chk
(
unchecked
)
+
payload
:
return
time
.
time
()
-
start
if
__name__
==
'__main__'
:
print
(
ping
(
'google.com'
))
Back
|
FazBrowse Home
|
New Git URL