execute a command on instance with ssh and return if cmd param is not None
        connect to ssh if cmd is None
        :param instance:
        :param tag_ssh_user:
        :param tag_ssh_port:
        :param cmd: execute this command if not None
        :return:

def connect(self, instance, tag_ssh_user, tag_ssh_port, cmd):
        """
        execute a command on instance with ssh and return if cmd param is not None
        connect to ssh if cmd is None
        :param instance:
        :param tag_ssh_user:
        :param tag_ssh_port:
        :param cmd: execute this command if not None
        :return:
        """
        ssh_user = SergentSsh.get_ssh_user(instance, tag_ssh_user)
        ssh_port = SergentSsh.get_ssh_port(instance, tag_ssh_port)

        if self._using_vpn is True:
            ssh_ip = instance.private_ip_address
        else:
            ssh_ip = instance.ip_address

        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())

        logger.debug('connecting to %s with port %s and user %s', ssh_ip, ssh_port, ssh_user)
        mykey = RSAKey.from_private_key(self._key_file)

        client.connect(hostname=ssh_ip, port=ssh_port, username=ssh_user, pkey=mykey)

        if cmd is None:
            with NamedTemporaryFile(mode='w+') as tmp_key_file:
                mykey.write_private_key(tmp_key_file, password=None)
                tmp_key_file.flush()
                cmd = 'ssh -i %s %s@%s -p %s' % (tmp_key_file.name, ssh_user, ssh_ip, ssh_port)
                logger.debug(cmd)
                os.system(cmd)
        else:
            stdin, stdout, stderr = client.exec_command(command=cmd)
            out_str = stdout.read()
            out_err = stderr.read().strip(' \t\n\r')
            print(out_str)
            if out_err != '':
                print(out_err)
                sys.exit(1)