From 1746d981720fc1d4f2e44aaf907d01c909866434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuncay=20=C3=87OLAK?= Date: Tue, 14 Mar 2023 14:54:05 +0300 Subject: [PATCH] get display number to remote access --- src/base/system/system.py | 15 +++++++++------ src/base/util/util.py | 7 +++++++ src/plugins/remote-access/setup-vnc-server.py | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/base/system/system.py b/src/base/system/system.py index f6e8445..5e31b1b 100644 --- a/src/base/system/system.py +++ b/src/base/system/system.py @@ -292,12 +292,15 @@ class System: @staticmethod def display(username): - system = System() - if "\\" in username: - user_parser = username.split("\\") - username = user_parser[1] - display = system.db_service.select_one_result('session', 'display', " username='{0}'".format(username)) - return display + try: + system = System() + if "\\" in username: + user_parser = username.split("\\") + username = user_parser[1] + display = system.db_service.select_one_result('session', 'display', " username='{0}'".format(username)) + return display + except Exception as e: + return None @staticmethod def desktop(username): diff --git a/src/base/util/util.py b/src/base/util/util.py index 40ad67e..f52fd52 100644 --- a/src/base/util/util.py +++ b/src/base/util/util.py @@ -498,3 +498,10 @@ class Util: return version else: return None + + # return active user in sessions list + @staticmethod + def get_active_user(): + result_code, p_out, p_err = Util.execute("for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }'); do loginctl show-session -p Id -p Name -p User -p State -p Type -p Remote $sessionid | sort; done | awk -F= '/Name/ { name = $2 } /User/ { user = $2 } /State/ { state = $2 } /Type/ { type = $2 } /Remote/ { remote = $2 } /User/ && remote == \"no\" && state == \"active\" && (type == \"x11\" || type == \"wayland\") { print name }\'") + p_out = str(p_out).rstrip() + return p_out diff --git a/src/plugins/remote-access/setup-vnc-server.py b/src/plugins/remote-access/setup-vnc-server.py index 5ada38c..409efb0 100644 --- a/src/plugins/remote-access/setup-vnc-server.py +++ b/src/plugins/remote-access/setup-vnc-server.py @@ -51,6 +51,8 @@ class SetupVnc(AbstractPlugin): def run_vnc_server(self): # user_name = self.db_service.select_one_result('session', 'username', " 1=1 order by id desc ") user_name = self.get_username() + if user_name is None: + user_name = self.get_active_user() self.logger.info('get logon username is {0}'.format(user_name)) self.logger.debug('Is VNC server installed?') if self.is_installed('x11vnc') is False: @@ -65,6 +67,8 @@ class SetupVnc(AbstractPlugin): self.logger.debug('Getting display and username...') # display_number = self.get_username_display(user_name) display_number = self.Sessions.display(user_name) + if display_number is None: + display_number = self.get_username_display() desktop_env = self.get_desktop_env() if desktop_env == "gnome": display_number = self.get_username_display_gnome(user_name) @@ -73,6 +77,8 @@ class SetupVnc(AbstractPlugin): #self.logger.info("Get home directory of {0} is {1}".format(user_name, homedir)) # this user_name for execute method user_name = self.get_as_user() + if user_name is None: + user_name = self.get_active_user() self.logger.debug('Username:{0} Display:{1}'.format(user_name, display_number)) #if self.is_exist('{0}/.vncahenk{1}'.format(homedir, user_name)) is True: # self.delete_folder('{0}/.vncahenk{1}'.format(homedir, user_name))