forked from dot1q/calix-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices-ont.py
executable file
·128 lines (114 loc) · 4.33 KB
/
services-ont.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
#######################################################################
# Program Filename: login.py
# Author: Gregory Brewster
# Date: 4/9/2015
# Description: Disables or restores service to an ONT
#######################################################################
#add configuration module
import config
import login
import logout
import sys
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.client, re
import xml.etree.ElementTree as ET
from xml.dom.minidom import parse, parseString #for debugging
def connect():
print("This program will suspend or restore service to an ONT")
print("Opening session with CMS...")
sessionID = login.call()
print(("Session ID for your session is "+str(sessionID)+"..."))
return sessionID
def disconnect(sessionID):
print(("Clossing session "+str(sessionID)+"..."))
logout.call(sessionID)
print("Session terminated!")
def main(gpon_type, gpon_fsan, gpon_state):
#print ("running main!\n")
session = connect() # connect to CMS
pulldata(session, gpon_type, gpon_fsan, gpon_state)
disconnect(session) # disconnect from CMS
def pulldata(sessionID, gpon_type, gpon_fsan, gpon_state):
target_url = str(config.protocol)+'://'+str(config.host)+':'+str(config.port) +str(config.extension)
xml_request = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<rpc message-id="1" nodename="%s" username="%s" sessionid="%s">
<action>
<action-type>show-ont</action-type>
<action-args><serno>%s</serno></action-args>
</action>
</rpc>
</soapenv:Body>
</soapenv:Envelope>
""" % (config.nodename, config.username, sessionID, gpon_fsan)
request = urllib.request.Request(target_url, xml_request)
request.add_header('Content-Type','text/plain;charset=UTF-8')
resultRead = urllib.request.urlopen(request).read()
#uncommet these to print debug info
#result = urllib2.urlopen(request)
#print parse( result ).toprettyxml()
#result.close()
ont = parseOntId(resultRead)
disableRG(sessionID, gpon_type, gpon_fsan, gpon_state, ont) #disable Residential Gateway (modify for ge, pots, etc)
def parseOntId(result):
#store passed XML data at var data
data = ET.fromstring(result)
ont = "NULL ONT ID, MAY NOT EXIST!" #make sure the session is le null
for elem in data.iter(tag='ont'):
ont = elem.text
print(("operation completed! results, ont:",ont))
return ont
def disableRG(sessionID, gpon_type, gpon_fsan, gpon_state, ont):
target_url = str(config.protocol)+'://'+str(config.host)+':'+str(config.port) +str(config.extension)
if (gpon_state == '1'):
state = "enabled"
elif (gpon_state == '0'):
state = "disabled"
else:
state = "enabled"
print("status is ",state)
xml_request = """
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<rpc message-id="180" nodename="%s" username="%s" sessionid="%s">
<edit-config>
<target>
<running/>
</target>
<config>
<top>
<object operation="merge">
<type>Ont</type>
<id>
<ont>%s</ont>
</id>
<admin>%s</admin>
</object>
</top>
</config>
</edit-config>
</rpc>
</soapenv:Body>
</soapenv:Envelope>
""" % (config.nodename, config.username, sessionID, ont, state)
request = urllib.request.Request(target_url, xml_request)
request.add_header('Content-Type','text/plain;charset=UTF-8')
resultRead = urllib.request.urlopen(request).read()
#uncommet these to print debug info
result = urllib.request.urlopen(request)
print(parse( result ).toprettyxml())
result.close()
if __name__== "__main__":
if len(sys.argv) != 4:
print("Usage:", sys.argv[0]," <type> <fsan> <status>")
print("Type options - Usually 'Ont'")
print("Fsan or serial, 6 digits base 16")
print("Status - 0=suspended, 1=active")
sys.exit(1)
gpon_type = sys.argv[1]
gpon_fsan = sys.argv[2]
gpon_state = sys.argv[3]
#arguments are expected to be correct at this point
#print("Processing type: "+str(gpon_type)+" serial "+str(gpon_fsan)+" state "+str(gpon_state)+"\n"); #Debuging for inputs
main(gpon_type, gpon_fsan, gpon_state)