blob: 71de94cb08d520684b6c799a0d90001deac25b91 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlabedcff052015-01-16 12:53:55 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Oct-2012
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004Copyright 2012 Open Networking Foundation (ONF)
adminbae64d82013-08-01 10:50:15 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
kelvin-onlabedcff052015-01-16 12:53:55 -080013 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
Jon Hallfbc828e2015-01-06 17:30:19 -080021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
24MininetCliDriver is the basic driver which will handle the Mininet functions
kelvin-onlabedcff052015-01-16 12:53:55 -080025"""
adminbae64d82013-08-01 10:50:15 -070026import pexpect
adminbae64d82013-08-01 10:50:15 -070027import re
28import sys
kelvin-onlaba4074292015-07-09 15:19:49 -070029import os
adminbae64d82013-08-01 10:50:15 -070030from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070031
kelvin-onlabedcff052015-01-16 12:53:55 -080032
33class RemoteMininetDriver( Emulator ):
34
35 """
kelvin-onlabd3b64892015-01-20 13:26:24 -080036 RemoteMininetCliDriver is the basic driver which will handle the Mininet
37 functions. The main different between this and the MininetCliDriver is that
38 this one does not build the mininet. It assumes that there is already a
39 mininet running on the target.
kelvin-onlabedcff052015-01-16 12:53:55 -080040 """
41 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070042 super( RemoteMininetDriver, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070043 self.handle = self
Jon Hallefbd9792015-03-05 16:11:36 -080044 self.name = None
kelvin-onlabedcff052015-01-16 12:53:55 -080045 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070046 self.flag = 0
47
kelvin-onlabedcff052015-01-16 12:53:55 -080048 def connect( self, **connectargs ):
kelvin-onlab08679eb2015-01-21 16:11:48 -080049 """,user_name, ip_address, pwd,options ):
kelvin-onlabd3b64892015-01-20 13:26:24 -080050 Here the main is the TestON instance after creating all the log
51 handles."""
kelvin-onlaba4074292015-07-09 15:19:49 -070052 try:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070053 for key in connectargs:
54 vars( self )[ key ] = connectargs[ key ]
55
56 self.name = self.options[ 'name' ]
57
58 try:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000059 if os.getenv( str( self.ip_address ) ) != None:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070060 self.ip_address = os.getenv( str( self.ip_address ) )
61 else:
62 main.log.info( self.name +
63 ": Trying to connect to " +
64 self.ip_address )
65
66 except KeyError:
67 main.log.info( "Invalid host name," +
68 " connecting to local host instead" )
69 self.ip_address = 'localhost'
70 except Exception as inst:
71 main.log.error( "Uncaught exception: " + str( inst ) )
72
73 self.handle = super(
74 RemoteMininetDriver,
75 self ).connect(
76 user_name=self.user_name,
77 ip_address=self.ip_address,
78 port=None,
79 pwd=self.pwd )
80
81 # Copying the readme file to process the
82 if self.handle:
83 return main.TRUE
84
kelvin-onlaba4074292015-07-09 15:19:49 -070085 else:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070086 main.log.error(
87 "Connection failed to the host " +
88 self.user_name +
89 "@" +
90 self.ip_address )
91 main.log.error( "Failed to connect to the Mininet" )
92 return main.FALSE
93 except Exception:
94 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -070095 main.cleanAndExit()
admin98ad0092014-07-23 16:51:07 -070096
kelvin-onlabedcff052015-01-16 12:53:55 -080097 def checkForLoss( self, pingList ):
98 """
Jon Hall6c794f32014-08-14 13:33:13 -070099 Returns main.FALSE for 0% packet loss and
100 Returns main.ERROR if "found multiple mininet" is found and
101 Returns main.TRUE else
kelvin-onlabedcff052015-01-16 12:53:55 -0800102 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700103 try:
104 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700105 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700106 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700107 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700108 self.handle.sendline( "cat " + pingList )
109 self.handle.expect( pingList )
Devin Limdc78e202017-06-09 18:30:07 -0700110 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700111 outputs = self.handle.before + self.handle.after
112 if re.search( " 0% packet loss", outputs ):
113 return main.FALSE
114 elif re.search( "found multiple mininet", outputs ):
115 return main.ERROR
116 else:
117 # TODO: Parse for failed pings, give some truncated output
118 main.log.error( "Error, unexpected output in the ping file" )
119 main.log.warn( outputs )
120 return main.TRUE
121 except pexpect.TIMEOUT:
122 main.log.exception( self.name + ": TIMEOUT exception found in checkForLoss" )
123 main.log.error( self.name + ": " + self.handle.before )
admin98ad0092014-07-23 16:51:07 -0700124 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700125 except pexpect.EOF:
126 main.log.error( self.name + ": EOF exception found" )
127 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700128 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700129 except Exception:
130 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700131 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700132
kelvin-onlabedcff052015-01-16 12:53:55 -0800133 def pingLong( self, **pingParams ):
134 """
Jon Hallefbd9792015-03-05 16:11:36 -0800135 Starts a continuous ping on the mininet host outputting
kelvin-onlabd3b64892015-01-20 13:26:24 -0800136 to a file in the /tmp dir.
kelvin-onlabedcff052015-01-16 12:53:55 -0800137 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700138 try:
139 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700140 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700141 args = utilities.parse_args(
142 [ "SRC", "TARGET", "PINGTIME" ], **pingParams )
143 precmd = "sudo rm /tmp/ping." + args[ "SRC" ]
144 self.execute( cmd=precmd, prompt="(.*)", timeout=10 )
145 command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " +\
146 args[ "TARGET" ] + " -i .2 -w " +\
147 str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." +\
148 args[ "SRC" ] + " &"
149 main.log.info( command )
150 self.execute( cmd=command, prompt="(.*)", timeout=10 )
151 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700152 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700153 return main.TRUE
154 except TypeError:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000155 main.log.exception(self.name + ": Object not as expected")
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700156 return main.FALSE
157 except pexpect.TIMEOUT:
158 main.log.exception( self.name + ": TIMEOUT exception found in pingLong" )
159 main.log.error( self.name + ": " + self.handle.before )
160 return main.FALSE
161 except pexpect.EOF:
162 main.log.error( self.name + ": EOF exception found" )
163 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700164 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700165 except Exception:
166 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700167 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700168
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000169
kelvin-onlabedcff052015-01-16 12:53:55 -0800170 def pingstatus( self, **pingParams ):
171 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 Tails the respective ping output file and check that
173 there is a moving "64 bytes"
kelvin-onlabedcff052015-01-16 12:53:55 -0800174 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700175 try:
176 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700177 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700178 args = utilities.parse_args( [ "SRC" ], **pingParams )
179 self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] )
180 self.handle.expect( "tail" )
Devin Limdc78e202017-06-09 18:30:07 -0700181 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700182 result = self.handle.before + self.handle.after
183 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700184 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700185 if re.search( 'Unreachable', result ):
186 main.log.info( "Unreachable found in ping logs..." )
187 return main.FALSE
188 elif re.search( '64\sbytes', result ):
189 main.log.info( "Pings look good" )
190 return main.TRUE
191 else:
192 main.log.info( "No, or faulty ping data..." )
193 return main.FALSE
194 except TypeError:
195 main.log.exception( self.name + ": Object not as expected" )
adminbae64d82013-08-01 10:50:15 -0700196 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700197 except pexpect.TIMEOUT:
198 main.log.exception( self.name + ": TIMEOUT exception found in pingstatus" )
199 main.log.error( self.name + ": " + self.handle.before )
adminbae64d82013-08-01 10:50:15 -0700200 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700201 except pexpect.EOF:
202 main.log.error( self.name + ": EOF exception found" )
203 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700204 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700205 except Exception:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000206 main.log.exception(self.name + ": Uncaught exception!")
Devin Lim44075962017-08-11 10:56:37 -0700207 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800208
kelvin-onlabedcff052015-01-16 12:53:55 -0800209 def pingKill( self, testONUser, testONIP ):
210 """
adminaeedddd2013-08-02 15:14:15 -0700211 Kills all continuous ping processes.
212 Then copies all the ping files to the TestStation.
kelvin-onlabedcff052015-01-16 12:53:55 -0800213 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700214 try:
215 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700216 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700217 command = "sudo kill -SIGINT `pgrep ping`"
218 main.log.info( command )
219 self.execute( cmd=command, prompt="(.*)", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800220
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700221 main.log.info( "Transferring ping files to TestStation" )
222 command = "scp /tmp/ping.* " + \
223 str( testONUser ) + "@" + str( testONIP ) + ":/tmp/"
224 self.execute( cmd=command, prompt="100%", timeout=20 )
225 # Make sure the output is cleared
226 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700227 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700228 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700229 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700230 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700231 i = self.handle.expect( [ "password", self.prompt ] )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700232 if i == 0:
233 main.log.error( "Error, sudo asking for password" )
234 main.log.error( self.handle.before )
235 return main.FALSE
236 else:
237 return main.TRUE
238 except pexpect.TIMEOUT:
239 main.log.error( self.name + ": TIMEOUT exception found in pingKill" )
240 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700241 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700242 except pexpect.EOF:
243 main.log.error( self.name + ": EOF exception found" )
244 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700245 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700246 except Exception:
247 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700248 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800249
kelvin-onlabedcff052015-01-16 12:53:55 -0800250 def pingLongKill( self ):
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700251 try:
252 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700253 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700254 command = "sudo kill -SIGING `pgrep ping`"
255 main.log.info( command )
256 self.execute( cmd=command, prompt="(.*)", timeout=10 )
257 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700258 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700259 return main.TRUE
260 except pexpect.TIMEOUT:
261 main.log.exception( self.name + ": TIMEOUT exception found in pingLongKill" )
262 main.log.error( self.name + ": " + self.handle.before )
263 return main.FALSE
264 except pexpect.EOF:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000265 main.log.error(self.name + ": EOF exception found")
266 main.log.error(self.name + ": " + self.handle.before)
Devin Lim44075962017-08-11 10:56:37 -0700267 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700268 except Exception:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000269 main.log.exception(self.name + ": Uncaught exception!")
Devin Lim44075962017-08-11 10:56:37 -0700270 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800271
kelvin-onlabedcff052015-01-16 12:53:55 -0800272 def pingHostOptical( self, **pingParams ):
273 """
Jon Hallefbd9792015-03-05 16:11:36 -0800274 This function is only for Packet Optical related ping
kelvin-onlabedcff052015-01-16 12:53:55 -0800275 Use the next pingHost() function for all normal scenarios )
shahshreya28bb18e2014-11-17 10:26:23 -0800276 Ping from one mininet host to another
277 Currently the only supported Params: SRC and TARGET
kelvin-onlabedcff052015-01-16 12:53:55 -0800278 """
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800279 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
kelvin-onlabedcff052015-01-16 12:53:55 -0800280 command = args[ "SRC" ] + " ping " + \
281 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
shahshreya28bb18e2014-11-17 10:26:23 -0800282 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800283 main.log.warn( "Sending: " + command )
kelvin-onlabedcff052015-01-16 12:53:55 -0800284 self.handle.sendline( command )
285 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
shahshreya28bb18e2014-11-17 10:26:23 -0800286 if i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800287 main.log.error(
288 self.name +
289 ": timeout when waiting for response from mininet" )
290 main.log.error( "response: " + str( self.handle.before ) )
291 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
shahshreya28bb18e2014-11-17 10:26:23 -0800292 if i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800293 main.log.error(
294 self.name +
295 ": timeout when waiting for response from mininet" )
296 main.log.error( "response: " + str( self.handle.before ) )
shahshreya28bb18e2014-11-17 10:26:23 -0800297 response = self.handle.before
298 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800299 main.log.error( self.name + ": EOF exception found" )
300 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700301 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 main.log.info( self.name + ": Ping Response: " + response )
kelvin-onlabedcff052015-01-16 12:53:55 -0800303 if re.search( ',\s0\%\spacket\sloss', response ):
304 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800305 main.lastResult = main.TRUE
shahshreya28bb18e2014-11-17 10:26:23 -0800306 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -0800307 else:
alisone4121a92016-11-22 16:31:36 -0800308 main.log.info(
kelvin-onlabedcff052015-01-16 12:53:55 -0800309 self.name +
310 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800311 main.lastResult = main.FALSE
shahshreya28bb18e2014-11-17 10:26:23 -0800312 return main.FALSE
313
kelvin-onlabedcff052015-01-16 12:53:55 -0800314 def pingHost( self, **pingParams ):
315 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800316 Pings between two hosts on remote mininet
kelvin-onlabedcff052015-01-16 12:53:55 -0800317 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700318 try:
319 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700320 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700321 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
322 command = "mininet/util/m " + \
323 args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2"
324 main.log.info( command )
325 response = self.execute( cmd=command, prompt="rtt", timeout=10 )
326 if utilities.assert_matches(
327 expect=',\s0\%\spacket\sloss',
328 actual=response,
329 onpass="No Packet loss",
330 onfail="Host is not reachable" ):
331 main.log.info( "NO PACKET LOSS, HOST IS REACHABLE" )
332 main.lastResult = main.TRUE
333 return main.TRUE
334 else:
alisone4121a92016-11-22 16:31:36 -0800335 main.log.info( "PACKET LOST, HOST IS NOT REACHABLE" )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700336 main.lastResult = main.FALSE
337 return main.FALSE
338 except pexpect.EOF:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000339 main.log.error(self.name + ": EOF exception found")
340 main.log.error(self.name + ": " + self.handle.before)
Devin Lim44075962017-08-11 10:56:37 -0700341 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700342 except Exception:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000343 main.log.exception(self.name + ": Uncaught exception!")
Devin Lim44075962017-08-11 10:56:37 -0700344 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800345
kelvin-onlabedcff052015-01-16 12:53:55 -0800346 def checknum( self, num ):
347 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800348 Verifies the correct number of switches are running
kelvin-onlabedcff052015-01-16 12:53:55 -0800349 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700350 try:
351 if self.handle:
352 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700353 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700354 self.handle.sendline( 'ifconfig -a | grep "sw.. " | wc -l' )
355 self.handle.expect( "wc" )
Devin Limdc78e202017-06-09 18:30:07 -0700356 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700357 response = self.handle.before
358 self.handle.sendline(
359 'ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l' )
360 self.handle.expect( "color" )
Devin Limdc78e202017-06-09 18:30:07 -0700361 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700362 response2 = self.handle.before
adminbae64d82013-08-01 10:50:15 -0700363
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700364 if re.search( num, response ):
365 if re.search( num, response2 ):
366 return main.TRUE
367 else:
368 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700369 else:
370 return main.FALSE
371 else:
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700372 main.log.error( "Connection failed to the host" )
373 except pexpect.TIMEOUT:
374 main.log.exception( self.name + ": TIMEOUT exception found in checknum" )
375 main.log.error( self.name + ": " + self.handle.before )
376 return main.FALSE
377 except pexpect.EOF:
378 main.log.error( self.name + ": EOF exception found" )
379 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700380 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700381 except Exception:
382 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700383 main.cleanAndExit()
adminbae64d82013-08-01 10:50:15 -0700384
kelvin-onlabd3b64892015-01-20 13:26:24 -0800385 def startTcpdump(
kelvin-onlabedcff052015-01-16 12:53:55 -0800386 self,
387 filename,
388 intf="eth0",
Charles Chan029be652015-08-24 01:46:10 +0800389 port="port 6653",
Jon Hall53c5e662016-04-13 16:06:56 -0700390 user="sdn" ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800391 """
Jon Hallefbd9792015-03-05 16:11:36 -0800392 Runs tcpdump on an interface and saves the file
admin2a9548d2014-06-17 14:08:07 -0700393 intf can be specified, or the default eth0 is used
kelvin-onlabedcff052015-01-16 12:53:55 -0800394 """
admin2a9548d2014-06-17 14:08:07 -0700395 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800396 self.handle.sendline( "" )
397 self.handle.sendline(
398 "sudo tcpdump -n -i " +
399 intf +
400 " " +
401 port +
402 " -w " +
403 filename.strip() +
404 " -Z " +
405 user +
406 " &" )
407 self.handle.sendline( "" )
408 self.handle.sendline( "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 i = self.handle.expect( [ 'No\ssuch\device', 'listening\son',
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000410 pexpect.TIMEOUT, self.prompt ], timeout=10 )
Jon Hall61282e32015-03-19 11:34:11 -0700411 main.log.info( self.handle.before + self.handle.after )
admin2a9548d2014-06-17 14:08:07 -0700412 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800413 main.log.error( self.name + ": tcpdump - No such device exists.\
414 tcpdump attempted on: " + intf )
admin2a9548d2014-06-17 14:08:07 -0700415 return main.FALSE
416 elif i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800417 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -0700418 return main.TRUE
419 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800420 main.log.error( self.name + ": tcpdump command timed out!\
421 Check interface name, given interface was: " + intf )
admin2a9548d2014-06-17 14:08:07 -0700422 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -0800423 elif i == 3:
424 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700425 return main.TRUE
426 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800427 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -0700428 return main.FALSE
429 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800430 main.log.error( self.name + ": EOF exception found" )
431 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700432 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800433 except Exception:
434 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700435 main.cleanAndExit()
admin2a9548d2014-06-17 14:08:07 -0700436
kelvin-onlabd3b64892015-01-20 13:26:24 -0800437 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -0800438 """
439 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -0700440 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800441 self.handle.sendline( "sudo pkill tcpdump" )
442 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700443 self.handle.expect( self.prompt )
admin2a9548d2014-06-17 14:08:07 -0700444 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800445 main.log.error( self.name + ": EOF exception found" )
446 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700447 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800448 except Exception:
449 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700450 main.cleanAndExit()
admin2a9548d2014-06-17 14:08:07 -0700451
Jon Hallca319892017-06-15 15:25:22 -0700452 def runOpticalMnScript( self, name='onos', ctrllerIP=None ):
shahshreya2c57c902015-04-06 20:40:20 -0700453 import time
shahshreya1f119da2015-04-21 17:16:46 -0700454 import types
kelvin-onlabedcff052015-01-16 12:53:55 -0800455 """
shahshreya1f119da2015-04-21 17:16:46 -0700456 Description:
457 This function is only meant for Packet Optical.
458 It runs python script "opticalTest.py" to create the
459 packet layer( mn ) and optical topology
460 Optional:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000461 name - Name of onos directory. (ONOS | onos)
shahshreya1f119da2015-04-21 17:16:46 -0700462 Required:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000463 ctrllerIP = Controller(s) IP address
shahshreya1f119da2015-04-21 17:16:46 -0700464 TODO: If no ctrllerIP is provided, a default
shahshreya2c57c902015-04-06 20:40:20 -0700465 $OC1 can be accepted
kelvin-onlabedcff052015-01-16 12:53:55 -0800466 """
shahshreya28bb18e2014-11-17 10:26:23 -0800467 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800468 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700469 self.handle.expect( self.prompt )
shahshreya1f119da2015-04-21 17:16:46 -0700470 self.handle.sendline( "cd ~/" + name + "/tools/test/topos" )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000471 self.handle.expect( "topos"+ self.prompt )
472 if ctrllerIP == None:
shahshreya2c57c902015-04-06 20:40:20 -0700473 main.log.info( "You need to specify the IP" )
474 return main.FALSE
475 else:
shahshreya1f119da2015-04-21 17:16:46 -0700476 controller = ''
477 if isinstance( ctrllerIP, types.ListType ):
478 for i in xrange( len( ctrllerIP ) ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000479 controller += ctrllerIP[i] + ' '
shahshreya1f119da2015-04-21 17:16:46 -0700480 main.log.info( "Mininet topology is being loaded with " +
481 "controllers: " + controller )
482 elif isinstance( ctrllerIP, types.StringType ):
483 controller = ctrllerIP
484 main.log.info( "Mininet topology is being loaded with " +
485 "controller: " + controller )
486 else:
487 main.log.info( "You need to specify a valid IP" )
488 return main.FALSE
489 cmd = "sudo -E python opticalTest.py " + controller
490 main.log.info( self.name + ": cmd = " + cmd )
shahshreya2c57c902015-04-06 20:40:20 -0700491 self.handle.sendline( cmd )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000492 time.sleep(30)
shahshreya2c57c902015-04-06 20:40:20 -0700493 self.handle.sendline( "" )
shahshreya3140b1a2015-04-28 14:22:15 -0700494 self.handle.sendline( "" )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000495 self.handle.expect("mininet>")
shahshreya2c57c902015-04-06 20:40:20 -0700496 return main.TRUE
497 except pexpect.EOF:
498 main.log.error( self.name + ": EOF exception found" )
499 main.log.error( self.name + ": " + self.handle.before )
500 return main.FALSE
501
502 def attachLincOESession( self ):
503 """
504 Since executing opticalTest.py will give you mininet
505 prompt, you would at some point require to get onto
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000506 console of LincOE ((linc@onosTestBench)1>) to execute
shahshreya2c57c902015-04-06 20:40:20 -0700507 commands like bring a optical port up or down on a ROADM
508 You can attach to console of Linc-OE session by a cmd:
509 sudo ~/linc-oe/rel/linc/bin/linc attach
510 """
511 try:
512 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700513 self.handle.expect( self.prompt )
shahshreya2c57c902015-04-06 20:40:20 -0700514 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
kelvin-onlabedcff052015-01-16 12:53:55 -0800515 self.handle.expect( ">" )
shahshreya28bb18e2014-11-17 10:26:23 -0800516 return main.TRUE
517 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800518 main.log.error( self.name + ": EOF exception found" )
519 main.log.error( self.name + ": " + self.handle.before )
shahshreya28bb18e2014-11-17 10:26:23 -0800520 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700521
kelvin-onlabedcff052015-01-16 12:53:55 -0800522 def disconnect( self ):
523 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800524 Called at the end of the test to disconnect the handle.
kelvin-onlabedcff052015-01-16 12:53:55 -0800525 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700526 try:
527 if self.handle:
528 # Close the ssh connection
529 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700530 # self.handle.expect( self.prompt )
531 i = self.handle.expect( [ self.prompt, 'mininet>', pexpect.TIMEOUT,
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700532 pexpect.EOF ], timeout=2 )
533 if i == 0:
534 self.handle.sendline( "exit" )
535 self.handle.expect( "closed" )
536 elif i == 1:
537 self.handle.sendline( "exit" )
538 self.handle.expect( "exit" )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000539 self.handle.expect(self.prompt)
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700540 self.handle.sendline( "exit" )
541 self.handle.expect( "exit" )
542 self.handle.expect( "closed" )
543 else:
544 main.log.error( "Connection failed to the host" )
545 return main.TRUE
546 except pexpect.TIMEOUT:
547 main.log.exception( self.name + ": TIMEOUT exception found in disconnect" )
548 main.log.error( self.name + ": " + self.handle.before )
549 return main.FALSE
550 except pexpect.EOF:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000551 main.log.error(self.name + ": EOF exception found")
552 main.log.error(self.name + ": " + self.handle.before)
Devin Lim44075962017-08-11 10:56:37 -0700553 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700554 except Exception:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000555 main.log.exception(self.name + ": Uncaught exception!")
Devin Lim44075962017-08-11 10:56:37 -0700556 main.cleanAndExit()
adminbae64d82013-08-01 10:50:15 -0700557
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 def setIpTablesOUTPUT( self, dstIp, dstPort, action='add',
559 packetType='tcp', rule='DROP' ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800560 """
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700561 Description:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800562 add or remove iptables rule to DROP ( default )
563 packets from specific IP and PORT
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700564 Usage:
kelvin-onlabedcff052015-01-16 12:53:55 -0800565 * specify action ( 'add' or 'remove' )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700566 when removing, pass in the same argument as you would add. It will
Jon Hallfbc828e2015-01-06 17:30:19 -0800567 delete that specific rule.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800568 * specify the destination ip to block with dstIp
569 * specify destination port to block to dstPort
kelvin-onlabedcff052015-01-16 12:53:55 -0800570 * optional packet type to block ( default tcp )
571 * optional iptables rule ( default DROP )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700572 WARNING:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800573 * This function uses root privilege iptables command which may result
574 in unwanted network errors. USE WITH CAUTION
kelvin-onlabedcff052015-01-16 12:53:55 -0800575 """
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700576 import re
577 import time
578
kelvin-onlabedcff052015-01-16 12:53:55 -0800579 # NOTE*********
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700580 # The strict checking methods of this driver function is intentional
581 # to discourage any misuse or error of iptables, which can cause
582 # severe network errors
kelvin-onlabd3b64892015-01-20 13:26:24 -0800583 # *************
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700584
kelvin-onlabd3b64892015-01-20 13:26:24 -0800585 # NOTE: Sleep needed to give some time
586 # for rule to be added and registered
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700587 # to the instance
kelvin-onlabedcff052015-01-16 12:53:55 -0800588 time.sleep( 5 )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700589
kelvin-onlabd3b64892015-01-20 13:26:24 -0800590 actionType = action.lower()
591 if actionType != 'add' and actionType != 'remove':
kelvin-onlabedcff052015-01-16 12:53:55 -0800592 main.log.error(
593 "Invalid action type. 'add' or 'remove' table rule" )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700594 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
kelvin-onlabedcff052015-01-16 12:53:55 -0800595 # NOTE: Currently only supports rules DROP, ACCEPT, and LOG
596 main.log.error(
597 "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700598 return
599 return
600 else:
601
kelvin-onlabedcff052015-01-16 12:53:55 -0800602 # If there is no existing rule in the iptables, we will see an
kelvin-onlabd3b64892015-01-20 13:26:24 -0800603 # 'iptables:'... message. We expect to see this message.
kelvin-onlabedcff052015-01-16 12:53:55 -0800604 # Otherwise, if there IS an existing rule, we will get the prompt
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700605 # back, hence why we expect $ for remove type. We want to remove
606 # an already existing rule
607
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 if actionType == 'add':
609 # NOTE: "iptables:" expect is a result of
610 # return from the command
611 # iptables -C ...
612 # Any changes by the iptables command return string
613 # will result in failure of the function. ( deemed unlikely
614 # at the time of writing this function )
kelvin-onlabedcff052015-01-16 12:53:55 -0800615 # Check for existing rules on current input
616 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700617 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800618 self.handle.sendline(
619 "sudo iptables -C OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800620 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800621 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800622 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800623 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800624 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800625 " -j " +
626 str( rule ) )
Devin Limdc78e202017-06-09 18:30:07 -0700627 i = self.handle.expect( [ "iptables:", self.prompt ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700628 print i
629 print self.handle.before
630 print "after: "
631 print self.handle.after
632
kelvin-onlabd3b64892015-01-20 13:26:24 -0800633 elif actionType == 'remove':
kelvin-onlabedcff052015-01-16 12:53:55 -0800634 # Check for existing rules on current input
635 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700636 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800637 self.handle.sendline(
638 "sudo iptables -C OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800639 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800640 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800641 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800642 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800643 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800644 " -j " +
645 str( rule ) )
Devin Limdc78e202017-06-09 18:30:07 -0700646 self.handle.expect( self.prompt )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700647 print "before: "
648 print self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800649 actualString = self.handle.after
650 expectString = "iptables:"
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700651 print "Actual String:"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 print actualString
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700653
kelvin-onlabd3b64892015-01-20 13:26:24 -0800654 if re.search( expectString, actualString ):
655 matchResult = main.TRUE
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700656 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800657 matchResult = main.FALSE
658 # If matchResult is main.TRUE, it means there is no matching rule.
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700659
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 # If tables does not exist and expected prompt is returned,
661 # go ahead and add iptables rule
662 if matchResult == main.TRUE:
kelvin-onlabedcff052015-01-16 12:53:55 -0800663 # Ensure action type is add
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 if actionType == 'add':
665 # -A is the 'append' action of iptables
666 actionAdd = '-A'
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700667 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800668 self.handle.sendline( "" )
669 self.handle.sendline(
670 "sudo iptables " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 actionAdd +
kelvin-onlabedcff052015-01-16 12:53:55 -0800672 " OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800673 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800674 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800675 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800676 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800678 " -j " +
679 str( rule ) )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700680
kelvin-onlabd3b64892015-01-20 13:26:24 -0800681 infoString = "Rules added to " + str( self.name )
Jon Hallefbd9792015-03-05 16:11:36 -0800682 infoString += "iptables rule added to block IP: " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 str( dstIp )
684 infoString += "Port: " + \
685 str( dstPort ) + " Rule: " + str( rule )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700686
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 main.log.info( infoString )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700688
kelvin-onlabedcff052015-01-16 12:53:55 -0800689 self.handle.expect(
Devin Limdc78e202017-06-09 18:30:07 -0700690 [ self.prompt, pexpect.EOF, pexpect.TIMEOUT ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700691 except pexpect.TIMEOUT:
kelvin-onlabedcff052015-01-16 12:53:55 -0800692 main.log.error(
693 self.name +
694 ": Timeout exception in setIpTables function" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800695 except Exception:
696 main.log.exception( self.name +
697 ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700698 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700699 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800700 main.log.error(
701 "Given rule already exists, but attempted to add it" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800702 # If matchResult is 0, it means there IS a matching rule provided
703 elif matchResult == main.FALSE:
kelvin-onlabedcff052015-01-16 12:53:55 -0800704 # Ensure action type is remove
kelvin-onlabd3b64892015-01-20 13:26:24 -0800705 if actionType == 'remove':
706 # -D is the 'delete' rule of iptables
707 actionRemove = '-D'
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700708 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800709 self.handle.sendline( "" )
710 # Delete a specific rule specified into the function
711 self.handle.sendline(
712 "sudo iptables " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800713 actionRemove +
kelvin-onlabedcff052015-01-16 12:53:55 -0800714 " OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800716 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800718 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800720 " -j " +
721 str( rule ) )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700722
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 infoString = "Rules removed from " + str( self.name )
724 infoString += " iptables rule removed \
725 from blocking IP: " + \
726 str( dstIp )
727 infoString += " Port: " + \
728 str( dstPort ) + " Rule: " + str( rule )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700729
kelvin-onlabd3b64892015-01-20 13:26:24 -0800730 main.log.info( infoString )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700731
kelvin-onlabedcff052015-01-16 12:53:55 -0800732 self.handle.expect(
Devin Limdc78e202017-06-09 18:30:07 -0700733 [ self.prompt, pexpect.EOF, pexpect.TIMEOUT ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700734 except pexpect.TIMEOUT:
kelvin-onlabedcff052015-01-16 12:53:55 -0800735 main.log.error(
736 self.name +
737 ": Timeout exception in setIpTables function" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800738 except Exception:
739 main.log.exception( self.name +
740 ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700741 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700742 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800743 main.log.error(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 "Given rule does not exist,\
745 but attempted to remove it" )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700746 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800747 # NOTE: If a bad usage of this function occurs, exit the entire
748 # test
749 main.log.error( "Bad rule given for iptables. Exiting..." )
Devin Lim44075962017-08-11 10:56:37 -0700750 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700751
752
adminbae64d82013-08-01 10:50:15 -0700753if __name__ != "__main__":
754 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800755 sys.modules[ __name__ ] = RemoteMininetDriver()