get display number to remote access

This commit is contained in:
Tuncay ÇOLAK 2023-03-14 14:54:05 +03:00
parent f029e916ef
commit 1746d98172
3 changed files with 22 additions and 6 deletions

View file

@ -292,12 +292,15 @@ class System:
@staticmethod @staticmethod
def display(username): def display(username):
system = System() try:
if "\\" in username: system = System()
user_parser = username.split("\\") if "\\" in username:
username = user_parser[1] user_parser = username.split("\\")
display = system.db_service.select_one_result('session', 'display', " username='{0}'".format(username)) username = user_parser[1]
return display display = system.db_service.select_one_result('session', 'display', " username='{0}'".format(username))
return display
except Exception as e:
return None
@staticmethod @staticmethod
def desktop(username): def desktop(username):

View file

@ -498,3 +498,10 @@ class Util:
return version return version
else: else:
return None 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

View file

@ -51,6 +51,8 @@ class SetupVnc(AbstractPlugin):
def run_vnc_server(self): def run_vnc_server(self):
# user_name = self.db_service.select_one_result('session', 'username', " 1=1 order by id desc ") # user_name = self.db_service.select_one_result('session', 'username', " 1=1 order by id desc ")
user_name = self.get_username() 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.info('get logon username is {0}'.format(user_name))
self.logger.debug('Is VNC server installed?') self.logger.debug('Is VNC server installed?')
if self.is_installed('x11vnc') is False: if self.is_installed('x11vnc') is False:
@ -65,6 +67,8 @@ class SetupVnc(AbstractPlugin):
self.logger.debug('Getting display and username...') self.logger.debug('Getting display and username...')
# display_number = self.get_username_display(user_name) # display_number = self.get_username_display(user_name)
display_number = self.Sessions.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() desktop_env = self.get_desktop_env()
if desktop_env == "gnome": if desktop_env == "gnome":
display_number = self.get_username_display_gnome(user_name) 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)) #self.logger.info("Get home directory of {0} is {1}".format(user_name, homedir))
# this user_name for execute method # this user_name for execute method
user_name = self.get_as_user() 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)) 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: #if self.is_exist('{0}/.vncahenk{1}'.format(homedir, user_name)) is True:
# self.delete_folder('{0}/.vncahenk{1}'.format(homedir, user_name)) # self.delete_folder('{0}/.vncahenk{1}'.format(homedir, user_name))