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 | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 27 | import os |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 30 | |
| 31 | class RemoteMininetDriver( Emulator ): |
| 32 | |
| 33 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 34 | RemoteMininetCliDriver is the basic driver which will handle the Mininet |
| 35 | functions. The main different between this and the MininetCliDriver is that |
| 36 | this one does not build the mininet. It assumes that there is already a |
| 37 | 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 |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 42 | self.name = None |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 43 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | self.flag = 0 |
| 45 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 46 | def connect( self, **connectargs ): |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 47 | """,user_name, ip_address, pwd,options ): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 48 | Here the main is the TestON instance after creating all the log |
| 49 | handles.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | for key in connectargs: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 51 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 52 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 53 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 54 | |
| 55 | try: |
| 56 | if os.getenv( str( self.ip_address ) ) != None: |
| 57 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 58 | else: |
| 59 | main.log.info( self.name + |
| 60 | ": Trying to connect to " + |
| 61 | self.ip_address ) |
| 62 | |
| 63 | except KeyError: |
| 64 | main.log.info( "Invalid host name," + |
| 65 | " connecting to local host instead" ) |
| 66 | self.ip_address = 'localhost' |
| 67 | except Exception as inst: |
| 68 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 69 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 70 | self.handle = super( |
| 71 | RemoteMininetDriver, |
| 72 | self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 73 | user_name=self.user_name, |
| 74 | ip_address=self.ip_address, |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 75 | port=None, |
| 76 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 77 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 78 | # Copying the readme file to process the |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 79 | if self.handle: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 80 | return main.TRUE |
| 81 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 82 | else: |
| 83 | main.log.error( |
| 84 | "Connection failed to the host " + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 85 | self.user_name + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 86 | "@" + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 87 | self.ip_address ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 88 | main.log.error( "Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | return main.FALSE |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 90 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 91 | def checkForLoss( self, pingList ): |
| 92 | """ |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 93 | Returns main.FALSE for 0% packet loss and |
| 94 | Returns main.ERROR if "found multiple mininet" is found and |
| 95 | Returns main.TRUE else |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 96 | """ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 97 | self.handle.sendline( "" ) |
| 98 | self.handle.expect( "\$" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 99 | self.handle.sendline( "" ) |
| 100 | self.handle.expect( "\$" ) |
| 101 | self.handle.sendline( "cat " + pingList ) |
| 102 | self.handle.expect( pingList ) |
| 103 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 104 | outputs = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 105 | if re.search( " 0% packet loss", outputs ): |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 106 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 107 | elif re.search( "found multiple mininet", outputs ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 108 | return main.ERROR |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 109 | else: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 110 | # TODO: Parse for failed pings, give some truncated output |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 111 | main.log.error( "Error, unexpected output in the ping file" ) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 112 | main.log.warn( outputs ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 113 | return main.TRUE |
| 114 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 115 | def pingLong( self, **pingParams ): |
| 116 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 117 | Starts a continuous ping on the mininet host outputting |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 118 | to a file in the /tmp dir. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 119 | """ |
| 120 | self.handle.sendline( "" ) |
| 121 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 122 | args = utilities.parse_args( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 123 | [ "SRC", "TARGET", "PINGTIME" ], **pingParams ) |
| 124 | precmd = "sudo rm /tmp/ping." + args[ "SRC" ] |
| 125 | self.execute( cmd=precmd, prompt="(.*)", timeout=10 ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 126 | command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 127 | args[ "TARGET" ] + " -i .2 -w " +\ |
| 128 | str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." +\ |
| 129 | args[ "SRC" ] + " &" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 130 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 131 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 132 | self.handle.sendline( "" ) |
| 133 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 134 | return main.TRUE |
| 135 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 136 | def pingstatus( self, **pingParams ): |
| 137 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 138 | Tails the respective ping output file and check that |
| 139 | there is a moving "64 bytes" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 140 | """ |
| 141 | self.handle.sendline( "" ) |
| 142 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 143 | args = utilities.parse_args( [ "SRC" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 144 | self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] ) |
| 145 | self.handle.expect( "tail" ) |
| 146 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | result = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 148 | self.handle.sendline( "" ) |
| 149 | self.handle.expect( "\$" ) |
| 150 | if re.search( 'Unreachable', result ): |
| 151 | main.log.info( "Unreachable found in ping logs..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 152 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 153 | elif re.search( '64\sbytes', result ): |
| 154 | main.log.info( "Pings look good" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 156 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 157 | main.log.info( "No, or faulty ping data..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 158 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 159 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 160 | def pingKill( self, testONUser, testONIP ): |
| 161 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 162 | Kills all continuous ping processes. |
| 163 | Then copies all the ping files to the TestStation. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 164 | """ |
| 165 | self.handle.sendline( "" ) |
| 166 | self.handle.expect( "\$" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 167 | command = "sudo kill -SIGINT `pgrep ping`" |
| 168 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 169 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 170 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 171 | main.log.info( "Transferring ping files to TestStation" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 172 | command = "scp /tmp/ping.* " + \ |
| 173 | str( testONUser ) + "@" + str( testONIP ) + ":/tmp/" |
| 174 | self.execute( cmd=command, prompt="100%", timeout=20 ) |
| 175 | # Make sure the output is cleared |
| 176 | self.handle.sendline( "" ) |
| 177 | self.handle.expect( "\$" ) |
| 178 | self.handle.sendline( "" ) |
| 179 | self.handle.expect( "\$" ) |
| 180 | self.handle.sendline( "" ) |
| 181 | i = self.handle.expect( [ "password", "\$" ] ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 182 | if i == 0: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 183 | main.log.error( "Error, sudo asking for password" ) |
| 184 | main.log.error( self.handle.before ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 185 | return main.FALSE |
| 186 | else: |
| 187 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 188 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 189 | def pingLongKill( self ): |
| 190 | self.handle.sendline( "" ) |
| 191 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 192 | command = "sudo kill -SIGING `pgrep ping`" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 193 | main.log.info( command ) |
| 194 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 195 | self.handle.sendline( "" ) |
| 196 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 197 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 198 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 199 | def pingHostOptical( self, **pingParams ): |
| 200 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 201 | This function is only for Packet Optical related ping |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 202 | Use the next pingHost() function for all normal scenarios ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 203 | Ping from one mininet host to another |
| 204 | Currently the only supported Params: SRC and TARGET |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 205 | """ |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 206 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 207 | command = args[ "SRC" ] + " ping " + \ |
| 208 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 209 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 210 | main.log.warn( "Sending: " + command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 211 | self.handle.sendline( command ) |
| 212 | i = self.handle.expect( [ command, 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 ) ) |
| 218 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 219 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 220 | main.log.error( |
| 221 | self.name + |
| 222 | ": timeout when waiting for response from mininet" ) |
| 223 | main.log.error( "response: " + str( self.handle.before ) ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 224 | response = self.handle.before |
| 225 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 226 | main.log.error( self.name + ": EOF exception found" ) |
| 227 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 228 | main.cleanup() |
| 229 | main.exit() |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 230 | main.log.info( self.name + ": Ping Response: " + response ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 231 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 232 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 233 | main.lastResult = main.TRUE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 234 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 235 | else: |
| 236 | main.log.error( |
| 237 | self.name + |
| 238 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 239 | main.lastResult = main.FALSE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 240 | return main.FALSE |
| 241 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 242 | def pingHost( self, **pingParams ): |
| 243 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 244 | Pings between two hosts on remote mininet |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 245 | """ |
| 246 | self.handle.sendline( "" ) |
| 247 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 248 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 249 | command = "mininet/util/m " + \ |
| 250 | args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2" |
| 251 | main.log.info( command ) |
| 252 | response = self.execute( cmd=command, prompt="rtt", timeout=10 ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 253 | if utilities.assert_matches( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 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" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 259 | main.lastResult = 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" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 263 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | def startTcpdump( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 294 | self, |
| 295 | filename, |
| 296 | intf="eth0", |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 297 | port="port 6653", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 298 | user="sdn" ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 299 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 300 | Runs tcpdump on an interface and saves the file |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 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( "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 317 | i = self.handle.expect( [ 'No\ssuch\device', 'listening\son', |
| 318 | pexpect.TIMEOUT, "\$" ], timeout=10 ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 319 | main.log.info( self.handle.before + self.handle.after ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 320 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 321 | main.log.error( self.name + ": tcpdump - No such device exists.\ |
| 322 | tcpdump attempted on: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 323 | return main.FALSE |
| 324 | elif i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 325 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 326 | return main.TRUE |
| 327 | elif i == 2: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 328 | main.log.error( self.name + ": tcpdump command timed out!\ |
| 329 | Check interface name, given interface was: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 330 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 331 | elif i == 3: |
| 332 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 333 | return main.TRUE |
| 334 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 335 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 336 | return main.FALSE |
| 337 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 338 | main.log.error( self.name + ": EOF exception found" ) |
| 339 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 340 | main.cleanup() |
| 341 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 342 | except Exception: |
| 343 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 344 | main.cleanup() |
| 345 | main.exit() |
| 346 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 347 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 348 | """ |
| 349 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 350 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 351 | self.handle.sendline( "sudo pkill tcpdump" ) |
| 352 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 353 | self.handle.expect( "\$" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 354 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 355 | main.log.error( self.name + ": EOF exception found" ) |
| 356 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 357 | main.cleanup() |
| 358 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 359 | except Exception: |
| 360 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 361 | main.cleanup() |
| 362 | main.exit() |
| 363 | |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 364 | def runOpticalMnScript( self,name = 'onos', ctrllerIP = None ): |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 365 | import time |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 366 | import types |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 367 | """ |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 368 | Description: |
| 369 | This function is only meant for Packet Optical. |
| 370 | It runs python script "opticalTest.py" to create the |
| 371 | packet layer( mn ) and optical topology |
| 372 | Optional: |
| 373 | name - Name of onos directory. (ONOS | onos) |
| 374 | Required: |
| 375 | ctrllerIP = Controller(s) IP address |
| 376 | TODO: If no ctrllerIP is provided, a default |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 377 | $OC1 can be accepted |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 378 | """ |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 379 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 380 | self.handle.sendline( "" ) |
| 381 | self.handle.expect( "\$" ) |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 382 | self.handle.sendline( "cd ~/" + name + "/tools/test/topos" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 383 | self.handle.expect( "topos\$" ) |
| 384 | if ctrllerIP == None: |
| 385 | main.log.info( "You need to specify the IP" ) |
| 386 | return main.FALSE |
| 387 | else: |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 388 | controller = '' |
| 389 | if isinstance( ctrllerIP, types.ListType ): |
| 390 | for i in xrange( len( ctrllerIP ) ): |
| 391 | controller += ctrllerIP[i] + ' ' |
| 392 | main.log.info( "Mininet topology is being loaded with " + |
| 393 | "controllers: " + controller ) |
| 394 | elif isinstance( ctrllerIP, types.StringType ): |
| 395 | controller = ctrllerIP |
| 396 | main.log.info( "Mininet topology is being loaded with " + |
| 397 | "controller: " + controller ) |
| 398 | else: |
| 399 | main.log.info( "You need to specify a valid IP" ) |
| 400 | return main.FALSE |
| 401 | cmd = "sudo -E python opticalTest.py " + controller |
| 402 | main.log.info( self.name + ": cmd = " + cmd ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 403 | self.handle.sendline( cmd ) |
shahshreya | d524a94 | 2015-04-21 09:55:16 -0700 | [diff] [blame] | 404 | time.sleep(30) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 405 | self.handle.sendline( "" ) |
shahshreya | 3140b1a | 2015-04-28 14:22:15 -0700 | [diff] [blame] | 406 | self.handle.sendline( "" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 407 | self.handle.expect("mininet>") |
| 408 | return main.TRUE |
| 409 | except pexpect.EOF: |
| 410 | main.log.error( self.name + ": EOF exception found" ) |
| 411 | main.log.error( self.name + ": " + self.handle.before ) |
| 412 | return main.FALSE |
| 413 | |
| 414 | def attachLincOESession( self ): |
| 415 | """ |
| 416 | Since executing opticalTest.py will give you mininet |
| 417 | prompt, you would at some point require to get onto |
| 418 | console of LincOE ((linc@onosTestBench)1>) to execute |
| 419 | commands like bring a optical port up or down on a ROADM |
| 420 | You can attach to console of Linc-OE session by a cmd: |
| 421 | sudo ~/linc-oe/rel/linc/bin/linc attach |
| 422 | """ |
| 423 | try: |
| 424 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 425 | self.handle.expect( "\$" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 426 | self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 427 | self.handle.expect( ">" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 428 | return main.TRUE |
| 429 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 430 | main.log.error( self.name + ": EOF exception found" ) |
| 431 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 432 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 433 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 434 | def disconnect( self ): |
| 435 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 436 | Called at the end of the test to disconnect the handle. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 437 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 438 | if self.handle: |
shahshreya | 95dd6f8 | 2015-02-05 15:59:16 -0800 | [diff] [blame] | 439 | # Close the ssh connection |
| 440 | self.handle.sendline( "" ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 441 | # self.handle.expect( "\$" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 442 | i = self.handle.expect( [ '\$', 'mininet>', pexpect.TIMEOUT, |
| 443 | pexpect.EOF ], timeout=2 ) |
shahshreya | dccfbc6 | 2015-02-20 11:40:09 -0800 | [diff] [blame] | 444 | if i == 0: |
| 445 | self.handle.sendline( "exit" ) |
| 446 | self.handle.expect( "closed" ) |
| 447 | elif i == 1: |
| 448 | self.handle.sendline( "exit" ) |
| 449 | self.handle.expect( "exit" ) |
| 450 | self.handle.expect('\$') |
| 451 | self.handle.sendline( "exit" ) |
| 452 | self.handle.expect( "exit" ) |
| 453 | self.handle.expect( "closed" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 454 | else: |
| 455 | main.log.error( "Connection failed to the host" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 456 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 457 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 458 | def setIpTablesOUTPUT( self, dstIp, dstPort, action='add', |
| 459 | packetType='tcp', rule='DROP' ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 460 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 461 | Description: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 462 | add or remove iptables rule to DROP ( default ) |
| 463 | packets from specific IP and PORT |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 464 | Usage: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 465 | * specify action ( 'add' or 'remove' ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 466 | 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] | 467 | delete that specific rule. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 468 | * specify the destination ip to block with dstIp |
| 469 | * specify destination port to block to dstPort |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 470 | * optional packet type to block ( default tcp ) |
| 471 | * optional iptables rule ( default DROP ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 472 | WARNING: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 473 | * This function uses root privilege iptables command which may result |
| 474 | in unwanted network errors. USE WITH CAUTION |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 475 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 476 | import re |
| 477 | import time |
| 478 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 479 | # NOTE********* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 480 | # The strict checking methods of this driver function is intentional |
| 481 | # to discourage any misuse or error of iptables, which can cause |
| 482 | # severe network errors |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 483 | # ************* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 484 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 485 | # NOTE: Sleep needed to give some time |
| 486 | # for rule to be added and registered |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 487 | # to the instance |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 488 | time.sleep( 5 ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 489 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 490 | actionType = action.lower() |
| 491 | if actionType != 'add' and actionType != 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 492 | main.log.error( |
| 493 | "Invalid action type. 'add' or 'remove' table rule" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 494 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 495 | # NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 496 | main.log.error( |
| 497 | "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 498 | return |
| 499 | return |
| 500 | else: |
| 501 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 502 | # If there is no existing rule in the iptables, we will see an |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 503 | # 'iptables:'... message. We expect to see this message. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 504 | # Otherwise, if there IS an existing rule, we will get the prompt |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 505 | # back, hence why we expect $ for remove type. We want to remove |
| 506 | # an already existing rule |
| 507 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 508 | if actionType == 'add': |
| 509 | # NOTE: "iptables:" expect is a result of |
| 510 | # return from the command |
| 511 | # iptables -C ... |
| 512 | # Any changes by the iptables command return string |
| 513 | # will result in failure of the function. ( deemed unlikely |
| 514 | # at the time of writing this function ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 515 | # Check for existing rules on current input |
| 516 | self.handle.sendline( "" ) |
| 517 | self.handle.expect( "\$" ) |
| 518 | self.handle.sendline( |
| 519 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 520 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 521 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 522 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 523 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 524 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 525 | " -j " + |
| 526 | str( rule ) ) |
| 527 | i = self.handle.expect( [ "iptables:", "\$" ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 528 | print i |
| 529 | print self.handle.before |
| 530 | print "after: " |
| 531 | print self.handle.after |
| 532 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 533 | elif actionType == 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 534 | # Check for existing rules on current input |
| 535 | self.handle.sendline( "" ) |
| 536 | self.handle.expect( "\$" ) |
| 537 | self.handle.sendline( |
| 538 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 539 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 540 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 541 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 542 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 543 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 544 | " -j " + |
| 545 | str( rule ) ) |
| 546 | self.handle.expect( "\$" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 547 | print "before: " |
| 548 | print self.handle.before |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 549 | actualString = self.handle.after |
| 550 | expectString = "iptables:" |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 551 | print "Actual String:" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 552 | print actualString |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 553 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 554 | if re.search( expectString, actualString ): |
| 555 | matchResult = main.TRUE |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 556 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 557 | matchResult = main.FALSE |
| 558 | # If matchResult is main.TRUE, it means there is no matching rule. |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 559 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 560 | # If tables does not exist and expected prompt is returned, |
| 561 | # go ahead and add iptables rule |
| 562 | if matchResult == main.TRUE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 563 | # Ensure action type is add |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 564 | if actionType == 'add': |
| 565 | # -A is the 'append' action of iptables |
| 566 | actionAdd = '-A' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 567 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 568 | self.handle.sendline( "" ) |
| 569 | self.handle.sendline( |
| 570 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 571 | actionAdd + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 572 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 573 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 574 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 575 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 576 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 577 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 578 | " -j " + |
| 579 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 580 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 581 | infoString = "Rules added to " + str( self.name ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 582 | infoString += "iptables rule added to block IP: " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 583 | str( dstIp ) |
| 584 | infoString += "Port: " + \ |
| 585 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 586 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 587 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 588 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 589 | self.handle.expect( |
| 590 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 591 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 592 | main.log.error( |
| 593 | self.name + |
| 594 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 595 | except Exception: |
| 596 | main.log.exception( self.name + |
| 597 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 598 | main.cleanup() |
| 599 | main.exit() |
| 600 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 601 | main.log.error( |
| 602 | "Given rule already exists, but attempted to add it" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 603 | # If matchResult is 0, it means there IS a matching rule provided |
| 604 | elif matchResult == main.FALSE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 605 | # Ensure action type is remove |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 606 | if actionType == 'remove': |
| 607 | # -D is the 'delete' rule of iptables |
| 608 | actionRemove = '-D' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 609 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 610 | self.handle.sendline( "" ) |
| 611 | # Delete a specific rule specified into the function |
| 612 | self.handle.sendline( |
| 613 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 614 | actionRemove + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 615 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 616 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 617 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 618 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 619 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 620 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 621 | " -j " + |
| 622 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 623 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 624 | infoString = "Rules removed from " + str( self.name ) |
| 625 | infoString += " iptables rule removed \ |
| 626 | from blocking IP: " + \ |
| 627 | str( dstIp ) |
| 628 | infoString += " Port: " + \ |
| 629 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 630 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 631 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 632 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 633 | self.handle.expect( |
| 634 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 635 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 636 | main.log.error( |
| 637 | self.name + |
| 638 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 639 | except Exception: |
| 640 | main.log.exception( self.name + |
| 641 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 642 | main.cleanup() |
| 643 | main.exit() |
| 644 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 645 | main.log.error( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 646 | "Given rule does not exist,\ |
| 647 | but attempted to remove it" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 648 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 649 | # NOTE: If a bad usage of this function occurs, exit the entire |
| 650 | # test |
| 651 | main.log.error( "Bad rule given for iptables. Exiting..." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 652 | main.cleanup() |
| 653 | main.exit() |
| 654 | |
| 655 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 656 | if __name__ != "__main__": |
| 657 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 658 | sys.modules[ __name__ ] = RemoteMininetDriver() |