-
Notifications
You must be signed in to change notification settings - Fork 16
/
ping.py
58 lines (42 loc) · 1.33 KB
/
ping.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import socket
import sys
import os
from struct import *
import time
AUTH = 0xDEC0ADDE
#src_ip = sys.argv[2]
dst_ip = sys.argv[1]
def checksum(str):
csum = 0
countTo = (len(str) / 2) * 2
count = 0
while count < countTo:
thisVal = ord(str[count+1]) * 256 + ord(str[count])
csum = csum + thisVal
csum = csum & 0xffffffffL #
count = count + 2
if countTo < len(str):
csum = csum + ord(str[len(str) - 1])
csum = csum & 0xffffffffL #
csum = (csum >> 16) + (csum & 0xffff)
csum = csum + (csum >> 16)
answer = ~csum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
try:
sd = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
except socket.error, msg:
print 'Socket could not be created. Err code: ' + str(msg[0]) + ' Message ' + msg[1]
exit()
packet = '';
dst_ip = socket.gethostbyname(dst_ip)
ID = os.getpid() & 0xFFFF
my_checksum = 0
header = pack("bbHHh", 8, 0, my_checksum, ID, 1)
data = pack("!I", AUTH);
my_checksum = checksum(header + data)
header = pack("bbHHh", 8, 0, socket.htons(my_checksum), ID, 1)
packet = header + data
sd.sendto(packet, (dst_ip, 0))