admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Oct-2012 |
| 4 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 5 | author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 11 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 23 | """ |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 24 | import traceback |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | import re |
| 27 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 28 | sys.path.append( "../" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 31 | |
| 32 | class RemoteMininetDriver( Emulator ): |
| 33 | |
| 34 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 35 | RemoteMininetCliDriver is the basic driver which will handle the Mininet functions |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 36 | The main different between this and the MininetCliDriver is that this one does not build the mininet. |
| 37 | It assumes that there is already a mininet running on the target. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 38 | """ |
| 39 | def __init__( self ): |
| 40 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | self.handle = self |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 42 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | self.flag = 0 |
| 44 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 45 | def connect( self, **connectargs ): |
| 46 | #,user_name, ip_address, pwd,options ): |
| 47 | # Here the main is the TestON instance after creating all the log |
| 48 | # handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | for key in connectargs: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 50 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 51 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 52 | self.name = self.options[ 'name' ] |
| 53 | self.handle = super( |
| 54 | RemoteMininetDriver, |
| 55 | self ).connect( |
| 56 | user_name=self.user_name, |
| 57 | ip_address=self.ip_address, |
| 58 | port=None, |
| 59 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 60 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | self.ssh_handle = self.handle |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 62 | |
| 63 | # Copying the readme file to process the |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 64 | if self.handle: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | return main.TRUE |
| 66 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 67 | else: |
| 68 | main.log.error( |
| 69 | "Connection failed to the host " + |
| 70 | self.user_name + |
| 71 | "@" + |
| 72 | self.ip_address ) |
| 73 | main.log.error( "Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | return main.FALSE |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 75 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 76 | #************************************************************************* |
| 77 | #************************************************************************* |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 78 | # checkForLoss will determine if any of the pings had any packets lost during the course of |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 79 | # the pingLong. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 80 | #************************************************************************* |
| 81 | #************************************************************************* |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 82 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 83 | def checkForLoss( self, pingList ): |
| 84 | """ |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 85 | Returns main.FALSE for 0% packet loss and |
| 86 | Returns main.ERROR if "found multiple mininet" is found and |
| 87 | Returns main.TRUE else |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 88 | """ |
| 89 | # TODO: maybe we want to return the % loss instead? This way we can set an acceptible loss %. |
| 90 | # EX: 393 packets transmitted, 380 received, 3% packet loss, time 78519ms |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 91 | # we may need to return a float to get around rounding errors |
| 92 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 93 | self.handle.sendline( "" ) |
| 94 | self.handle.expect( "\$" ) |
| 95 | # Clear any output waiting in the bg from killing pings |
| 96 | self.handle.sendline( "" ) |
| 97 | self.handle.expect( "\$" ) |
| 98 | self.handle.sendline( "cat " + pingList ) |
| 99 | self.handle.expect( pingList ) |
| 100 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 101 | outputs = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 102 | if re.search( " 0% packet loss", outputs ): |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 103 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 104 | elif re.search( "found multiple mininet", outputs ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 105 | return main.ERROR |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 106 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 107 | main.log.error( "Error, unexpected output in the ping file" ) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 108 | main.log.warn( outputs ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 109 | return main.TRUE |
| 110 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 111 | def pingLong( self, **pingParams ): |
| 112 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 113 | Starts a continuous ping on the mininet host outputing to a file in the /tmp dir. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 114 | """ |
| 115 | self.handle.sendline( "" ) |
| 116 | self.handle.expect( "\$" ) |
| 117 | args = utilities.parse_args( |
| 118 | [ "SRC", "TARGET", "PINGTIME" ], **pingParams ) |
| 119 | precmd = "sudo rm /tmp/ping." + args[ "SRC" ] |
| 120 | self.execute( cmd=precmd, prompt="(.*)", timeout=10 ) |
| 121 | command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " + args[ |
| 122 | "TARGET" ] + " -i .2 -w " + str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." + args[ "SRC" ] + " &" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 123 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 124 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 125 | self.handle.sendline( "" ) |
| 126 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | return main.TRUE |
| 128 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 129 | def pingstatus( self, **pingParams ): |
| 130 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 131 | Tails the respective ping output file and check that there is a moving "64 bytes" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 132 | """ |
| 133 | self.handle.sendline( "" ) |
| 134 | self.handle.expect( "\$" ) |
| 135 | args = utilities.parse_args( [ "SRC" ], **pingParams ) |
| 136 | self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] ) |
| 137 | self.handle.expect( "tail" ) |
| 138 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 139 | result = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 140 | self.handle.sendline( "" ) |
| 141 | self.handle.expect( "\$" ) |
| 142 | if re.search( 'Unreachable', result ): |
| 143 | main.log.info( "Unreachable found in ping logs..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 145 | elif re.search( '64\sbytes', result ): |
| 146 | main.log.info( "Pings look good" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 148 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 149 | main.log.info( "No, or faulty ping data..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 151 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 152 | def pingKill( self, testONUser, testONIP ): |
| 153 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 154 | Kills all continuous ping processes. |
| 155 | Then copies all the ping files to the TestStation. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 156 | """ |
| 157 | self.handle.sendline( "" ) |
| 158 | self.handle.expect( "\$" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 159 | command = "sudo kill -SIGINT `pgrep ping`" |
| 160 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 161 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 162 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | main.log.info( "Transferring ping files to TestStation" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 164 | command = "scp /tmp/ping.* " + \ |
| 165 | str( testONUser ) + "@" + str( testONIP ) + ":/tmp/" |
| 166 | self.execute( cmd=command, prompt="100%", timeout=20 ) |
| 167 | # Make sure the output is cleared |
| 168 | self.handle.sendline( "" ) |
| 169 | self.handle.expect( "\$" ) |
| 170 | self.handle.sendline( "" ) |
| 171 | self.handle.expect( "\$" ) |
| 172 | self.handle.sendline( "" ) |
| 173 | i = self.handle.expect( [ "password", "\$" ] ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 174 | if i == 0: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 175 | main.log.error( "Error, sudo asking for password" ) |
| 176 | main.log.error( self.handle.before ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 177 | return main.FALSE |
| 178 | else: |
| 179 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 180 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 181 | def pingLongKill( self ): |
| 182 | self.handle.sendline( "" ) |
| 183 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 184 | command = "sudo kill -SIGING `pgrep ping`" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 185 | main.log.info( command ) |
| 186 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 187 | self.handle.sendline( "" ) |
| 188 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 190 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 191 | def pingHostOptical( self, **pingParams ): |
| 192 | """ |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 193 | This function is only for Packey Optical related ping |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 194 | Use the next pingHost() function for all normal scenarios ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 195 | Ping from one mininet host to another |
| 196 | Currently the only supported Params: SRC and TARGET |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 197 | """ |
| 198 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 199 | #command = args[ "SRC" ] + " ping -" + args[ "CONTROLLER" ] + " " +args [ "TARGET" ] |
| 200 | command = args[ "SRC" ] + " ping " + \ |
| 201 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 202 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 203 | main.log.warn( "Sending: " + command ) |
| 204 | #response = self.execute( cmd=command,prompt="mininet",timeout=10 ) |
| 205 | self.handle.sendline( command ) |
| 206 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 207 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 208 | main.log.error( |
| 209 | self.name + |
| 210 | ": timeout when waiting for response from mininet" ) |
| 211 | main.log.error( "response: " + str( self.handle.before ) ) |
| 212 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 213 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 214 | main.log.error( |
| 215 | self.name + |
| 216 | ": timeout when waiting for response from mininet" ) |
| 217 | main.log.error( "response: " + str( self.handle.before ) ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 218 | response = self.handle.before |
| 219 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 220 | main.log.error( self.name + ": EOF exception found" ) |
| 221 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 222 | main.cleanup() |
| 223 | main.exit() |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 224 | main.log.info( self.name + ": Ping Response: " + response ) |
| 225 | # if utilities.assert_matches( |
| 226 | # expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet |
| 227 | # loss",onfail="Host is not reachable" ): |
| 228 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 229 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 230 | main.last_result = main.TRUE |
| 231 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 232 | else: |
| 233 | main.log.error( |
| 234 | self.name + |
| 235 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 236 | main.last_result = main.FALSE |
| 237 | return main.FALSE |
| 238 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 239 | def pingHost( self, **pingParams ): |
| 240 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 241 | Pings between two hosts on remote mininet |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 242 | """ |
| 243 | self.handle.sendline( "" ) |
| 244 | self.handle.expect( "\$" ) |
| 245 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 246 | #command = "mininet/util/m " + args[ "SRC" ] + " ping "+args [ "TARGET" ]+" -c 4 -W 1 -i .2" |
| 247 | command = "mininet/util/m " + \ |
| 248 | args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2" |
| 249 | main.log.info( command ) |
| 250 | response = self.execute( cmd=command, prompt="rtt", timeout=10 ) |
| 251 | # self.handle.sendline( "" ) |
| 252 | # self.handle.expect( "\$" ) |
| 253 | if utilities.assert_matches( |
| 254 | expect=',\s0\%\spacket\sloss', |
| 255 | actual=response, |
| 256 | onpass="No Packet loss", |
| 257 | onfail="Host is not reachable" ): |
| 258 | main.log.info( "NO PACKET LOSS, HOST IS REACHABLE" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 259 | main.last_result = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 260 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 261 | else: |
| 262 | main.log.error( "PACKET LOST, HOST IS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 263 | main.last_result = main.FALSE |
| 264 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 265 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 266 | def checknum( self, num ): |
| 267 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 268 | Verifies the correct number of switches are running |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 269 | """ |
| 270 | if self.handle: |
| 271 | self.handle.sendline( "" ) |
| 272 | self.handle.expect( "\$" ) |
| 273 | self.handle.sendline( 'ifconfig -a | grep "sw.. " | wc -l' ) |
| 274 | self.handle.expect( "wc" ) |
| 275 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 276 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 277 | self.handle.sendline( |
| 278 | 'ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l' ) |
| 279 | self.handle.expect( "color" ) |
| 280 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 281 | response2 = self.handle.before |
| 282 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 283 | if re.search( num, response ): |
| 284 | if re.search( num, response2 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 285 | return main.TRUE |
| 286 | else: |
| 287 | return main.FALSE |
| 288 | else: |
| 289 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 290 | else: |
| 291 | main.log.error( "Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 292 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 293 | def start_tcpdump( |
| 294 | self, |
| 295 | filename, |
| 296 | intf="eth0", |
| 297 | port="port 6633", |
| 298 | user="admin" ): |
| 299 | """ |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 300 | Runs tpdump on an intferface and saves the file |
| 301 | intf can be specified, or the default eth0 is used |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 302 | """ |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 303 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 304 | self.handle.sendline( "" ) |
| 305 | self.handle.sendline( |
| 306 | "sudo tcpdump -n -i " + |
| 307 | intf + |
| 308 | " " + |
| 309 | port + |
| 310 | " -w " + |
| 311 | filename.strip() + |
| 312 | " -Z " + |
| 313 | user + |
| 314 | " &" ) |
| 315 | self.handle.sendline( "" ) |
| 316 | self.handle.sendline( "" ) |
| 317 | i = self.handle.expect( |
| 318 | [ 'No\ssuch\device', 'listening\son', pexpect.TIMEOUT, "\$" ], timeout=10 ) |
| 319 | main.log.warn( self.handle.before + self.handle.after ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 320 | if i == 0: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 321 | main.log.error( |
| 322 | self.name + |
| 323 | ": tcpdump - No such device exists. tcpdump attempted on: " + |
| 324 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 325 | return main.FALSE |
| 326 | elif i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 327 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 328 | return main.TRUE |
| 329 | elif i == 2: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 330 | main.log.error( |
| 331 | self.name + |
| 332 | ": tcpdump command timed out! Check interface name, given interface was: " + |
| 333 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 334 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 335 | elif i == 3: |
| 336 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 337 | return main.TRUE |
| 338 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 339 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 340 | return main.FALSE |
| 341 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 342 | main.log.error( self.name + ": EOF exception found" ) |
| 343 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 344 | main.cleanup() |
| 345 | main.exit() |
| 346 | except: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 347 | main.log.info( |
| 348 | self.name + |
| 349 | ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 350 | main.log.error( traceback.print_exc() ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 351 | main.log.info( |
| 352 | ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 353 | main.cleanup() |
| 354 | main.exit() |
| 355 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 356 | def stop_tcpdump( self ): |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 357 | "pkills tcpdump" |
| 358 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 359 | self.handle.sendline( "sudo pkill tcpdump" ) |
| 360 | self.handle.sendline( "" ) |
| 361 | self.handle.sendline( "" ) |
| 362 | self.handle.expect( "\$" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 363 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 364 | main.log.error( self.name + ": EOF exception found" ) |
| 365 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 366 | main.cleanup() |
| 367 | main.exit() |
| 368 | except: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 369 | main.log.info( |
| 370 | self.name + |
| 371 | ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 372 | main.log.error( traceback.print_exc() ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 373 | main.log.info( |
| 374 | ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 375 | main.cleanup() |
| 376 | main.exit() |
| 377 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 378 | def run_optical_mn_script( self ): |
| 379 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 380 | This function is only meant for Packet Optical. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 381 | It runs the python script "optical.py" to create the packet layer( mn ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 382 | topology |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 383 | """ |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 384 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 385 | self.handle.sendline( "" ) |
| 386 | self.handle.expect( "\$" ) |
| 387 | self.handle.sendline( "cd ~" ) |
| 388 | self.handle.expect( "\$" ) |
| 389 | self.handle.sendline( "sudo python optical.py" ) |
| 390 | self.handle.expect( ">" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 391 | return main.TRUE |
| 392 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 393 | main.log.error( self.name + ": EOF exception found" ) |
| 394 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 395 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 396 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 397 | def disconnect( self ): |
| 398 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 399 | Called at the end of the test to disconnect the handle. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 400 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 401 | response = '' |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 402 | # print "Disconnecting Mininet" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 403 | if self.handle: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 404 | self.handle.sendline( "exit" ) |
| 405 | self.handle.expect( "exit" ) |
| 406 | self.handle.expect( "(.*)" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 407 | response = self.handle.before |
| 408 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 409 | else: |
| 410 | main.log.error( "Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 411 | response = main.FALSE |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 412 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 413 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 414 | def get_flowTable( self, protoVersion, sw ): |
| 415 | # TODO document usage |
| 416 | # TODO add option to look at cookies. ignoreing them for now |
| 417 | self.handle.sendline( "cd" ) |
| 418 | self.handle.expect( [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 419 | # print "get_flowTable(" + str( protoVersion ) +" " + str( sw ) +")" |
| 420 | # NOTE: Use format to force consistent flow table output across |
| 421 | # versions |
| 422 | if protoVersion == 1.0: |
| 423 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
| 424 | " -F OpenFlow10-table_id | awk '{OFS=\",\" ; print $1 $3 $6 $7 $8}' | cut -d ',' -f 2- | sort -n -k1 -r" |
| 425 | self.handle.sendline( command ) |
| 426 | self.handle.expect( [ "k1 -r", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 427 | self.handle.expect( |
| 428 | [ "OFPST_FLOW", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 429 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 430 | # print "response=", response |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 431 | return response |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 432 | elif protoVersion == 1.3: |
| 433 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
| 434 | " -O OpenFlow13 | awk '{OFS=\",\" ; print $1 $3 $6 $7}' | cut -d ',' -f 2- | sort -n -k1 -r" |
| 435 | self.handle.sendline( command ) |
| 436 | self.handle.expect( [ "k1 -r", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 437 | self.handle.expect( |
| 438 | [ "OFPST_FLOW", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 439 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 440 | # print "response=", response |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 441 | return response |
| 442 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 443 | main.log.error( |
| 444 | "Unknown protoVersion in get_flowTable(). given: (" + |
| 445 | str( |
| 446 | type( protoVersion ) ) + |
| 447 | ") '" + |
| 448 | str(protoVersion) + |
| 449 | "'" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 450 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 451 | def flow_comp( self, flow1, flow2 ): |
| 452 | if flow1 == flow2: |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 453 | return main.TRUE |
| 454 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 455 | main.log.info( "Flow tables do not match, printing tables:" ) |
| 456 | main.log.info( "Flow Table 1:" ) |
| 457 | main.log.info( flow1 ) |
| 458 | main.log.info( "Flow Table 2:" ) |
| 459 | main.log.info( flow2 ) |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 460 | return main.FALSE |
| 461 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 462 | def setIpTablesOUTPUT( |
| 463 | self, |
| 464 | dst_ip, |
| 465 | dst_port, |
| 466 | action='add', |
| 467 | packet_type='tcp', |
| 468 | rule='DROP' ): |
| 469 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 470 | Description: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 471 | add or remove iptables rule to DROP ( default ) packets from specific IP and PORT |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 472 | Usage: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 473 | * specify action ( 'add' or 'remove' ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 474 | when removing, pass in the same argument as you would add. It will |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 475 | delete that specific rule. |
| 476 | * specify the destination ip to block with dst_ip |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 477 | * specify destination port to block to dst_port |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 478 | * optional packet type to block ( default tcp ) |
| 479 | * optional iptables rule ( default DROP ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 480 | WARNING: |
| 481 | * This function uses root privilege iptables command which may result in |
| 482 | unwanted network errors. USE WITH CAUTION |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 483 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 484 | import re |
| 485 | import time |
| 486 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 487 | # NOTE********* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 488 | # The strict checking methods of this driver function is intentional |
| 489 | # to discourage any misuse or error of iptables, which can cause |
| 490 | # severe network errors |
| 491 | #************* |
| 492 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 493 | # NOTE: Sleep needed to give some time for rule to be added and registered |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 494 | # to the instance |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 495 | time.sleep( 5 ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 496 | |
| 497 | action_type = action.lower() |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 498 | if action_type != 'add' and action_type != 'remove': |
| 499 | main.log.error( |
| 500 | "Invalid action type. 'add' or 'remove' table rule" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 501 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 502 | # NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 503 | main.log.error( |
| 504 | "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 505 | return |
| 506 | return |
| 507 | else: |
| 508 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 509 | # If there is no existing rule in the iptables, we will see an |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 510 | #'iptables:'... message. We expect to see this message. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 511 | # Otherwise, if there IS an existing rule, we will get the prompt |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 512 | # back, hence why we expect $ for remove type. We want to remove |
| 513 | # an already existing rule |
| 514 | |
| 515 | if action_type == 'add': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 516 | # NOTE: "iptables:" expect is a result of return from the command |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 517 | # iptables -C ... |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 518 | # Any changes by the iptables command return string |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 519 | # will result in failure of the function. ( deemed unlikely |
| 520 | # at the time of writing this function ) |
| 521 | # Check for existing rules on current input |
| 522 | self.handle.sendline( "" ) |
| 523 | self.handle.expect( "\$" ) |
| 524 | self.handle.sendline( |
| 525 | "sudo iptables -C OUTPUT -p " + |
| 526 | str( packet_type ) + |
| 527 | " -d " + |
| 528 | str( dst_ip ) + |
| 529 | " --dport " + |
| 530 | str( dst_port ) + |
| 531 | " -j " + |
| 532 | str( rule ) ) |
| 533 | i = self.handle.expect( [ "iptables:", "\$" ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 534 | print i |
| 535 | print self.handle.before |
| 536 | print "after: " |
| 537 | print self.handle.after |
| 538 | |
| 539 | elif action_type == 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 540 | # Check for existing rules on current input |
| 541 | self.handle.sendline( "" ) |
| 542 | self.handle.expect( "\$" ) |
| 543 | self.handle.sendline( |
| 544 | "sudo iptables -C OUTPUT -p " + |
| 545 | str( packet_type ) + |
| 546 | " -d " + |
| 547 | str( dst_ip ) + |
| 548 | " --dport " + |
| 549 | str( dst_port ) + |
| 550 | " -j " + |
| 551 | str( rule ) ) |
| 552 | self.handle.expect( "\$" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 553 | print "before: " |
| 554 | print self.handle.before |
| 555 | actual_string = self.handle.after |
| 556 | expect_string = "iptables:" |
| 557 | print "Actual String:" |
| 558 | print actual_string |
| 559 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 560 | if re.search( expect_string, actual_string ): |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 561 | match_result = main.TRUE |
| 562 | else: |
| 563 | match_result = main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 564 | # If match_result is main.TRUE, it means there is no matching rule. |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 565 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 566 | # If tables does not exist and expected prompt is returned, go ahead and |
| 567 | # add iptables rule |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 568 | if match_result == main.TRUE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 569 | # Ensure action type is add |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 570 | if action_type == 'add': |
| 571 | #-A is the 'append' action of iptables |
| 572 | action_add = '-A' |
| 573 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 574 | self.handle.sendline( "" ) |
| 575 | self.handle.sendline( |
| 576 | "sudo iptables " + |
| 577 | action_add + |
| 578 | " OUTPUT -p " + |
| 579 | str( packet_type ) + |
| 580 | " -d " + |
| 581 | str( dst_ip ) + |
| 582 | " --dport " + |
| 583 | str( dst_port ) + |
| 584 | " -j " + |
| 585 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 586 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 587 | info_string = "Rules added to " + str( self.name ) |
| 588 | info_string += "iptable rule added to block IP: " + \ |
| 589 | str( dst_ip ) |
| 590 | info_string += "Port: " + \ |
| 591 | str( dst_port ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 592 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 593 | main.log.info( info_string ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 594 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 595 | self.handle.expect( |
| 596 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 597 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 598 | main.log.error( |
| 599 | self.name + |
| 600 | ": Timeout exception in setIpTables function" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 601 | except: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 602 | main.log.error( traceback.print_exc() ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 603 | main.cleanup() |
| 604 | main.exit() |
| 605 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 606 | main.log.error( |
| 607 | "Given rule already exists, but attempted to add it" ) |
| 608 | # If match_result is 0, it means there IS a matching rule provided |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 609 | elif match_result == main.FALSE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 610 | # Ensure action type is remove |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 611 | if action_type == 'remove': |
| 612 | #-D is the 'delete' rule of iptables |
| 613 | action_remove = '-D' |
| 614 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 615 | self.handle.sendline( "" ) |
| 616 | # Delete a specific rule specified into the function |
| 617 | self.handle.sendline( |
| 618 | "sudo iptables " + |
| 619 | action_remove + |
| 620 | " OUTPUT -p " + |
| 621 | str( packet_type ) + |
| 622 | " -d " + |
| 623 | str( dst_ip ) + |
| 624 | " --dport " + |
| 625 | str( dst_port ) + |
| 626 | " -j " + |
| 627 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 628 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 629 | info_string = "Rules removed from " + str( self.name ) |
| 630 | info_string += " iptables rule removed from blocking IP: " + \ |
| 631 | str( dst_ip ) |
| 632 | info_string += " Port: " + \ |
| 633 | str( dst_port ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 634 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 635 | main.log.info( info_string ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 636 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 637 | self.handle.expect( |
| 638 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 639 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 640 | main.log.error( |
| 641 | self.name + |
| 642 | ": Timeout exception in setIpTables function" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 643 | except: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 644 | main.log.error( traceback.print_exc() ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 645 | main.cleanup() |
| 646 | main.exit() |
| 647 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 648 | main.log.error( |
| 649 | "Given rule does not exist, but attempted to remove it" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 650 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 651 | # NOTE: If a bad usage of this function occurs, exit the entire |
| 652 | # test |
| 653 | main.log.error( "Bad rule given for iptables. Exiting..." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 654 | main.cleanup() |
| 655 | main.exit() |
| 656 | |
| 657 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 658 | if __name__ != "__main__": |
| 659 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 660 | sys.modules[ __name__ ] = RemoteMininetDriver() |