def get_port(self, ip):
        if not self._port_cache:
            self._port_cache = {}
            port_re = re.compile(r".+ tcp dpt:(\d+).*to:([\d\.]+)\:22")
            cmd = "iptables -n -t nat -L PREROUTING"
            code, out, err = self.server.ssh.execute(cmd)
            for l in out:
                m = port_re.match(l)
                if m:
                    self._port_cache[m.group(2)] = int(m.group(1))
        port = self._port_cache.get(ip)

        if port is None:
            if self._port_cache:
                port = max(self._port_cache.values()) + 1
            else:
                port = 1222
        self._port_cache[ip] = port
        cmd = IPT_PORT_TEMPLATE.format(host_ip=self.server.host, ip=ip,
                                       port=port, action="I")
        self.server.ssh.run(cmd)
        return port
Get forwarded ssh port for instance ip.
dataset/ETHPy150Open openstack/rally/rally/deployment/serverprovider/providers/lxc.py/LxcHost.get_port