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