-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpClient.py
93 lines (81 loc) · 2.72 KB
/
dhcpClient.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
import sys
import time
import socket
class dhcpClient:
''' client requests methods for a connection to dhcp Server '''
def __init__(self, mac):
''' initilize the port and open a socket for other end of
communication requesting a connection dhcp Server '''
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clientPort = 6001
self.hostname = socket.gethostname()
self.clientSocket.connect((self.hostname, self.clientPort))
self.mac = mac
def clientDiscover(self):
''' send the request for dhcp server for a connection '''
self.clientSocket.send(self.mac)
fullStatus = int(self.clientSocket.recv(1))
if fullStatus:
print "Subnet is full"
return self.clientDiscover()
self.clientOffer()
def clientOffer(self):
''' take the IP address sent by the dhcp Server '''
# recieve Ip Address
assignedIp = ''
while not assignedIp:
assignedIp = self.clientSocket.recv(100)
self.clientSocket.send(str(1))
self.ip = assignedIp
# recieve Network Address
assignedNetworkAddress = ''
while not assignedNetworkAddress:
assignedNetworkAddress = self.clientSocket.recv(100)
self.clientSocket.send(str(1))
self.networkAddress = assignedNetworkAddress
# recieve BroadCast Address
assignedBroadCastAddress = ''
while not assignedBroadCastAddress:
assignedBroadCastAddress = self.clientSocket.recv(100)
self.clientSocket.send(str(1))
self.broadcastAddress = assignedBroadCastAddress
# recieve DNS & Gateway Address
assignedDNSGateway = ''
while not assignedDNSGateway:
assignedDNSGateway = self.clientSocket.recv(100)
self.clientSocket.send(str(1))
self.DNS = assignedDNSGateway
self.gateWay = assignedDNSGateway
self.clientRequest(assignedIp)
def clientRequest(self, ip):
''' send back the IP address sent by dhcp Server
for verification of proper communication channel '''
self.clientSocket.send(ip)
self.clientAck(ip)
def clientAck(self, ip):
''' receive the acknowledgment sent by dhcp Server '''
ack = self.clientSocket.recv(4)
if ack == "ACK":
print self.ip
print self.networkAddress
print self.broadcastAddress
print self.DNS
print self.gateWay
if "Cannot" in str(self.ip):
print "cannot connect to this Network"
else:
leaseTime = int(self.clientSocket.recv(10))
time.sleep(leaseTime)
self.clientSocket.send(str(1))
else:
self.clientDiscover()
if __name__ == "__main__":
if len(sys.argv) == 3:
if sys.argv[0] == "dhcpClient.py" and sys.argv[1] == "-m":
client = dhcpClient(sys.argv[2])
client.clientDiscover()
else:
print "Give proper number of argument ( eg: ./client.py -m MAC-ADDRESS )"
else:
print "Give proper number of argument ( eg: ./client.py -m MAC-ADDRESS )"