Exemple #1
0
    def create_vnc_tunnel(self, vnc_port):
        if not self.connected:
            raise exceptions.MisuseError("The client is not yet connected. Misuse of function \"create_vnc_tunnel()\"")
        elif vnc_port > 99 or vnc_port <= 0:
            raise exceptions.MisuseError("VNC port number should be between 1 and 99. ")

        _, _, stderr = self.client.exec_command('vncserver :' + str(vnc_port))
        for line in stderr:
            print('... ' + line.strip('\n'))

        actual_port = 5900 + vnc_port

        super().create_forward_tunnel(actual_port, actual_port)
Exemple #2
0
    def __new__(cls, *args, **kwargs):
        # to prevent misuse of the class
        if cls.instantiated:
            raise exceptions.MisuseError(
                "class %s has been instantiated. Misuse of constructor. " %
                cls.__name__)

        return super(UGProfile, cls).__new__(cls, *args, **kwargs)
Exemple #3
0
    def create_vnc_tunnel(self):
        if not self.connected:
            raise exceptions.MisuseError(
                "The client is not yet connected. Misuse of function \"create_vnc_tunnel()\""
            )

        super().create_forward_tunnel(ECFConnection.LOCAL_PORT,
                                      ECFConnection.REMOTE_PORT)
Exemple #4
0
    def killall_VNC_servers(self):
        if not self.connected:
            raise exceptions.MisuseError(
                "The client is not yet connected. Misuse of function \"killall_VNC_servers()\"")

        # rm -f ~/.vnc/$HOST*
        # to prevent any "stale" servers showing when we check ports
        _, _, stderr = self.client.exec_command("killall Xtigervnc; rm -f ~/.vnc/$HOST*")
        for line in stderr:
            print(line)
Exemple #5
0
    def update_ports(self):
        self.used_ports_lst = []
        self.ports_by_me_lst = []
        if not self.connected:
            raise exceptions.MisuseError("The client is not yet connected. Misuse of function \"update_ports()\"")

        # formulate the vnc port scanning cmd
        scan_ports_cmd_lst = []
        for port in range(5900, 5999):
            scan_ports_cmd_lst.append('sh -c "nc -z -nv 127.0.0.1 ' + str(port) + ' 2>&1" | grep \'open\|succeeded\'')
        scan_ports_cmd = ";".join(scan_ports_cmd_lst)

        # send out the vnc port scanning cmd
        _, stdout, _ = self.client.exec_command(scan_ports_cmd)
        for line in stdout:
            if "open" in line:  # on other machines
                line = line.replace('(UNKNOWN) [127.0.0.1] ', '')  # remove prefix
                line = line.replace(' (?) open', '')  # remove suffix
            elif "succeeded" in line:  # on ug250 and ug251
                line = line.replace('Connection to 127.0.0.1 ', '')  # remove prefix
                line = line.replace(' port [tcp/*] succeeded!', '')  # remove suffix
            else:
                raise exceptions.MisuseError("Unexpected output when scanning vnc ports: %s" % line)

            this_used_port = int(line) - 5900
            if this_used_port <= 0 or this_used_port > 99:
                raise exceptions.MisuseError("Unexpected port number when scanning vnc ports: %d" % this_used_port)
            self.used_ports_lst.append(this_used_port)

            # use the API by vncserver to see what ports are created by me
            _, stdout, _ = self.client.exec_command('vncserver -list')

            for line in stdout:
                this_port_by_me = re.findall(r'\d+', line)
                if len(this_port_by_me) != 0:
                    self.ports_by_me_lst.append(int(this_port_by_me[0]))
Exemple #6
0
    def save_profile(self, last_lab, viewer):
        if type(last_lab) is not str or last_lab not in ("EECG", "ECF"):
            raise exceptions.MisuseError(
                "last_lab should be one of 'EECG' or 'ECF'")

        self["last_lab"] = last_lab
        self["viewer"] = viewer
        try:
            with open(PROFILE_FILE_PATH, 'w') as outfile:
                profile_dict = copy.deepcopy(self._profile)
                profile_dict["last_checked_update"] = datetime.datetime.now(
                ).isoformat()
                profile_dict["EECG"]["passwd"] = base64.b64encode(
                    self["EECG"]["passwd"].encode('ascii')).decode('ascii')
                profile_dict["ECF"]["passwd"] = base64.b64encode(
                    self["ECF"]["passwd"].encode('ascii')).decode('ascii')

                json_data = json.dumps(profile_dict, indent=4)
                outfile.write(json_data)
        except Exception:
            # need to handle any write permission issues, once observed
            raise Exception