-
Notifications
You must be signed in to change notification settings - Fork 0
/
2test.py
executable file
·321 lines (305 loc) · 9.82 KB
/
2test.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#! /usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import argparse
import paramiko
import socket
from xml.dom.minidom import parse,parseString
from time import sleep
Hostname = "Host"
Flag = 0
#значение тэга
value = ""
#значение тэгов
valuem = ""
#количесво значений в пременной value
step=1
#Количество <value> xxx </value> / step
stepv=15
i=0
vendor=""
# Считываем url-list
file = open('/home/dieul/Документы/Selectel/url-abuse_test.txt', 'r')
Urls=file.read()
file.close()
Urls=Urls.replace('\n', ' ').rstrip()
#print("Url_List is",Urls)
count=Urls.count(' ')
#отладка
print("Count space is ",count)
print("Length is", len(Urls))
# задаем управляющие команды
#hello for cisco
HELLOC="""
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability><capability>urn:ietf:params:netconf:capability:writeable-running:1.0</capability>
<capability>urn:ietf:params:netconf:capability:startup:1.0</capability><capability>urn:ietf:params:netconf:capability:url:1.0</capability>
<capability>urn:cisco:params:netconf:capability:pi-data-model:1.0</capability><capability>urn:cisco:params:netconf:capability:notification:1.0</capability>
</capabilities>
</hello>
]]>]]>
"""
#hello for jun
HELLOJ =""" <hello>
<capabilities>
<capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:candidate:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:url:1.0?protocol=http,ftp,file</capability>
<capability>http://xml.juniper.net/netconf/junos/1.0</capability>
<capability>http://xml.juniper.net/dmi/system/1.0</capability>
</capabilities>
</hello>
]]>]]>"""
#как для cisco так и для jun
CLOSE = """
<rpc>
<close-session/>
</rpc>
]]>]]>"""
#JUN
INFO="""
<rpc>
<get-software-information/>
</rpc>
]]>]]>"""
CONF="""
<rpc>
<get-configuration format="xml">
</get-configuration>
</rpc>
]]>]]>"""
#CISCO
CONFC2="""
<?xml version="1.0" encoding="UTF-8"?>
<rpc>
<get-config>
<source>
<startup/>
</source>
<filter>
<config-format-xml>
</config-format-xml>
</filter>
</get-config>
</rpc>]]>]]>
"""
def msetc(Hostname,pattern):
SETC="""
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="netconf.mini.edit.3">
<edit-config>
<target>
<startup/>
</target>
<config>
<cli-config-data-block>
hostname """+Hostname+"""
parameter-map type regex BadSitesRegex \n"""+pattern+""" </cli-config-data-block>
</config>
</edit-config>
</rpc>]]>]]> """
return SETC
def msetj(Hostname, Urls):
SET="""
<rpc>
<edit-config>
<target>
<candidate/>
</target>
<config>
<configuration>
<system>
<host-name>"""+Hostname+"""</host-name>
</system>
<security>
<utm>
<custom-objects>
<url-pattern>
<name>BadSites</name>
<value>"""+Urls+"""</value>
</url-pattern>
</custom-objects>
</utm>
</security>
</configuration>
</config>
</edit-config>
</rpc>
]]>]]>
"""
return SET
COMMIT="""
<rpc>
<commit-configuration/>
</rpc>
"""
def createParser():
parser = argparse.ArgumentParser(description = '--> Connect to vSRX or 2851 <--')
parser.add_argument("--host", default="10.230.242.11", action="store", help="hostname or IP ")
parser.add_argument("-u", default="admin", action="store" , help = "username")
parser.add_argument("-p", default="QaZxSw!23", action="store",help = "password")
parser.add_argument("-P", default=830, action="store",help = "port")
parser.add_argument("-d", action="store_true", help = "debug")
parser.add_argument("-v", action="store_true", help = "verbose")
return parser
parser = createParser()
namespace = parser.parse_args(sys.argv[1:])
print (namespace)
if namespace.d == False:
#create socket
socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.connect((namespace.host , int(namespace.P)))
# шифруем через ssh
client = paramiko.SSHClient()
# добавляем ключ сервера в список известных хостов — файл .ssh/known_hosts.
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client = paramiko.Transport(socket)
client.connect( username=namespace.u, password=namespace.p)
#Create channel
channel = client.open_session()
name = channel.set_name('netconf')
#Invoke NETCON
channel.invoke_subsystem('netconf')
#Прием начальной информации.
data =1
data_hello=""
#для теста
i = 0
while data:
#для дебага
print("номер итерации %i \n Значение пременной data_hello " %i)
i=i+1
data = channel.recv(4096)
data_hello=data_hello+data
#для дебага
print(data_hello)
#ищем конец Hello сообщения и определяем вендора
if data_hello.find('</hello>') != -1:
if (data_hello.find("cisco") != -1):
vendor="cisco"
mset=msetc
channel.send(HELLOC)
#сделаем запрос на кофиг
#channel.send(CONFC)
#стереть потом
data_hello=""
elif (data_hello.find("juniper") != -1):
vendor="juniper"
mset=msetj
channel.send(HELLOJ)
else:
print("not supported")
if namespace.v == True:
print(data_hello)
print("End of Hello message is found")
print("gnerate hello message...and send it")
if vendor == "juniper":
print(HELLOJ)
elif vendor == "cisco":
print(HELLOC)
break
else:
mset=msetc
vendor="cisco"
#выдергиваем тэг <session-id>
# dom=parseString(data_hello)
#выдерает вместе с тэгом
# session_id=dom.getElementsByTagName('session-id')[0].toxml()
# выдерает только значение, можно было бы сделать цикл for node in
# session-id: print node.childNodes[0].nodeValue
# session_id=dom.getElementsByTagName('session-id')[0].childNodes[0].nodeValue
# ENABLE="""
# <rpc><notification-on/>
# </rpc>
# """
# channel.send(ENABLE)
# while 1:
# print("in while")
# data = channel.recv(4096)
# print(data)
# if data.find('</rpc-reply>') != -1:
# channel.send(CLOSE)
# break
decision=raw_input("are u vendnor %s? is it right? Ready to go?" %vendor)
#заполняем <value>
for i in range(1,count+1):
position=Urls.rfind(' ')
print(i,position,count)
if (i)%step !=0:
value=value+" "+Urls[position+1:len(Urls)+1]
Urls=Urls[0:position]
else:
if vendor == "juniper":
# создаем новую строчку <value>
valuem=valuem+value+"</value><value>"
else:
value="pattern "+value+" \n"
valuem=valuem+value
# print("Value in IF behin is %d %s" %(i, value))
value=Urls[position+1:len(Urls)+1]
# print("Value in IF end is",value)
# print("Valuem in IF end is",valuem)
Urls=Urls[0:position]
#отправка
if i%stepv ==0:
if namespace.d == False:
channel.send(mset(Hostname, valuem))
if namespace.v == True:
print(mset(Hostname, valuem))
else:
print(mset(Hostname, valuem))
sleep(0.1)
print("send %d request successes!!! count is %d " %(i, count))
valuem = ""
elif i == count:
if vendor=="cisco":
value="pattern "+value
if namespace.d == False:
channel.send(mset(Hostname, valuem))
channel.send(mset(Hostname, value))
if namespace.v == True:
print(mset(Hostname, valuem))
print(mset(Hostname, value))
else:
print(mset(Hostname, valuem))
print(mset(Hostname, value))
print("Value in IF behin is %d %s" %(i, value))
print("Valuem in IF behin is %d %s" %(i, valuem))
if vendor == "juniper":
value=Urls[0:position-1]
else:
value="patern "+Urls[0:position-1]
if namespace.d == False:
channel.send(mset(Hostname, value))
if namespace.v == True:
print(mset(Hostname, value))
else:
print(mset(Hostname, value))
sleep(0.1)
print("send %d request successes!!! count is %d " %(i, count))
valuem = ""
channel.send(INFO)
#channel.send(CONF)
#channel.send(SET)
if namespace.d == False:
channel.send(COMMIT)
if namespace.v == True:
print(COMMIT)
Flag=1
#Recieve data returned
data = channel.recv(2048)
while data:
data = channel.recv(1024)
print(data)
if (data.find('</rpc-reply>') != -1) and (Flag == 1):
#We have reached the end of reply
channel.send(CLOSE)
channel.close()
client.close()
socket.close()
else:
print(COMMIT)