-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaapi.rb
230 lines (164 loc) · 5.45 KB
/
caapi.rb
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# //////////////////////////////////////////////////
# /////
# //
# // clean access api calls, etc...
# //
# /////
# //////////////////////////////////////////////////
require 'rubygems'
require 'curb'
require 'hpricot'
# //////////////////////////////////////////////////
# /////
# //
# // massage api error/count/found results
# //
def error_chk(c)
$oobcount = nil
$ooberror = nil
$oobfound = nil
oobuserinfo = c.body_str.gsub(/<!--/, '')
oobuserinfo = oobuserinfo.gsub(/-->/, '')
oobuserinfo = oobuserinfo.gsub(/,/, "\r\n")
oobuserinfo = oobuserinfo.split(/\n/)
count = Regexp.new(/count=\d+/)
count = count.match(oobuserinfo[1])
count = count.to_s
count = count.split(/count=/)
$oobcount = count[1]
error = Regexp.new(/error=\d+/)
error = error.match(oobuserinfo[0])
error = error.to_s
error = error.split(/error=/)
$ooberror = error[1]
found = Regexp.new(/found=\w+/)
found = found.match(oobuserinfo[1])
found = found.to_s
found = found.split(/found=/)
$oobfound = found[1]
end
# //
# /////
# //////////////////////////////////////////////////
# //////////////////////////////////////////////////
# /////
# //
# // where the magic happens
# //
def regmac(mac,desc)
puts "mac in function"
puts mac
c = Curl::Easy.new("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp") do |curl|
curl.headers["User-Agent"] = "CA API"
curl.http_post("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp",
Curl::PostField.content('admin', CONFIG['setup']['api']['user']),
Curl::PostField.content('passwd', CONFIG['setup']['api']['password']),
Curl::PostField.content('op', 'addmac'),
Curl::PostField.content('type', 'userole'),
Curl::PostField.content('mac', mac),
Curl::PostField.content('role', CONFIG['setup']['api']['role']),
Curl::PostField.content('desc', desc))
end
# /////
# //
# // i'm assuming i have enough prior error checking to just register the device
# //
c.perform
# ///// puts c.body_str
end
# //
# /////
# //////////////////////////////////////////////////
# //////////////////////////////////////////////////
# /////
# //
# // check api to see if device has previously registered
# //
def checkmac(mac)
c = Curl::Easy.new("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp") do |curl|
curl.headers["User-Agent"] = "CA API"
curl.http_post("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp",
Curl::PostField.content('admin', CONFIG['setup']['api']['user']),
Curl::PostField.content('passwd', CONFIG['setup']['api']['password']),
Curl::PostField.content('op', 'checkmac'),
Curl::PostField.content('mac', mac))
end
c.perform
# ///// puts c.body_str
# /////
# //
# // check api for errors or null results
# //
error_chk(c)
# ///// puts "-======================-"
# ///// puts "error check for checkmac"
# ///// puts "ooberror"
# ///// puts $ooberror
# ///// puts "oobfound"
# ///// puts $oobfound
# ///// puts "registered"
# ///// puts $registered
if $ooberror == "0" && $oobfound == "true"
$registered = "yes"
else
$registered = "no"
end
# ///// puts "registered"
# ///// puts $registered
# ///// puts "-======================-"
end
# //
# /////
# //////////////////////////////////////////////////
# //////////////////////////////////////////////////
# /////
# //
# // query api to see if device is logged in
# // if they are logged in they really shouldn't
# // need to register
# //
def checkoobmac(mac)
c = Curl::Easy.new("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp") do |curl|
curl.headers["User-Agent"] = "CA API"
curl.http_post("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp",
Curl::PostField.content('admin', CONFIG['setup']['api']['user']),
Curl::PostField.content('passwd', CONFIG['setup']['api']['password']),
Curl::PostField.content('op', 'getoobuserinfo'),
Curl::PostField.content('qtype', 'mac'),
Curl::PostField.content('qval', mac))
end
# // curl.http_post("https://#{CONFIG['setup']['api']['url']}/admin/cisco_api.jsp",
# // Curl::PostField.content('admin', CONFIG['setup']['api']['user']),
# // Curl::PostField.content('passwd', CONFIG['setup']['api']['password']),
# // Curl::PostField.content('op', 'getoobuserinfo'),
# // Curl::PostField.content('qtype', 'ip'),
# // Curl::PostField.content('qval', ip))
# //
# // end
c.perform
# ///// puts c.body_str
# /////
# //
# // check api for errors or null results
# //
error_chk(c)
# ///// puts "-======================-"
# ///// puts "error check for checkoobip"
# ///// puts "ooberror"
# ///// puts $ooberror
# ///// puts "oobcount"
# ///// puts $oobcount
# ///// puts "loggedin"
# ///// puts $loggedin
if $ooberror == "0" && $oobcount == "1"
$loggedin = "yes"
else
$loggedin = "no"
end
# ///// puts "loggedin"
# ///// puts $loggedin
# ///// puts "-======================-"
end
# //
# /////
# //////////////////////////////////////////////////