LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2018, 08:49 AM   #1
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Rep: Reputation: Disabled
automatically run (sudo) bash script after each xrdp login


Dear LQ community,

At my current workplace we have several VMs, which we log into using xrdp. Sometimes we want to open new xrdp sessions and other times we'd like to use a specific session, which is the reason why we use ports. The issue is that if someone forgets which port was used to do some computation, you can't connect to this session anymore. Therefore, i wrote a bash script, which gathers different pieces of information (using sudo when needed) and stores these into a file (similar to a log). It works and I'm happy.

But now, I haven't found a way to run this script every time a user logs in through xrdp!
I've tried:
  1. Adding the script to the startup applications
  2. Adding it to the folder /etc/profile.d/
  3. Adding a config file to /etc/init.d/

Is there any way to make it run after the xrdp login? For example through the xrdp settings (sesman.ini or xrdp.ini files)?

Please not that:
  • Using sudo for certain commands is necessary. (On the other hand, all commands could be run with sudo.)
  • Currently, we have to use xrdp and can't use for example xvnc.
  • The script should run once after each xrdp login.
  • The script requires bash (contains none POSIX 'commands').

I'm looking forward to reading your response and can give additional details if needed.
Thank you.
 
Old 08-02-2018, 09:18 PM   #2
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: Devuan+lxde
Posts: 656

Rep: Reputation: 190Reputation: 190
Have you tried adding it to /etc/xdg/autostart?

On second thought, you probably want something user specific, rather than to set globally

Last edited by TheEzekielProject; 08-02-2018 at 09:19 PM.
 
Old 08-03-2018, 02:39 AM   #3
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
No, I haven't tried that yet, but will do so now. Thanks for the input!

Actually, it should be set globally as logging this information is helpful for everyone.

In the meantime, I've tried adding the following line at the end of the file startwm.sh (located at /etc/xrdp/):
Code:
bash vm_logger.sh
But this wasn't successful either...

I'm adding now the following vm_logger.desktop into /etc/xdg/autostart:
Code:
[Desktop Entry]
Type=Application
Name=VM logins logger
Comment=Log partial info about remote sessions to be able to reuse a past sessions.
Exec=/home/bioinf/Desktop/vm_logger.sh
I'll be back to let you know how it went
 
Old 08-03-2018, 03:38 AM   #4
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Unfortunately putting *.desktop into /etc/xdg/autostart didn't work.
I also adapted the *.desktop file to:
Code:
[Desktop Entry]
Type=Application
Name=VM logins logger
Comment=Log partial info about remote sessions to be able to reuse a past sessions.
Exec=/home/bioinf/Desktop/vm_logger.sh
Icon=
Path=
Terminal=true
StartupNotify=false
But it still isn't running after a login...

Any input is welcomed as I don't see any other possibility than running a cronjob. (Which would be annoying as it would have to run every 15min or so even though once every login would be enough.)

Most VMs are running Xubuntu, so maybe the script could be run when the Desktop is 'generated'?
 
Old 08-03-2018, 05:43 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
Quote:
Originally Posted by bioinf View Post
In the meantime, I've tried adding the following line at the end of the file startwm.sh (located at /etc/xrdp/):
Code:
bash vm_logger.sh
But this wasn't successful either...
please show us the complete script (is it working otherwise?).

also, is vm_logger.sh in your $PATH?
 
Old 08-03-2018, 06:58 AM   #6
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Ondoho,

Thanks for your input. $PATH seems to get rewritten after each login, so even when I added the path to my script it was removed when I logged in again. Therefore, I copied my script into a path available for $PATH and yet it didn't work.

My script is working fine when running it locally as sudo:
Code:
#!/bin/bash
# run as root

# currently only works if using sesman-xvnc for logins

my_ip="$(sudo nslookup myMachine | grep Address | grep -v '#')"
my_path="/home/my/personal/path/"
ip2="$(sudo nslookup anotherMachine | grep Address | grep -v '#')"
login_file="vm_logins.log"

last_log="$(sudo sed -e '$!d' /var/log/xrdp-sesman.log)"
# get date, time, status (reconnection/new), display MORE?

# Enabling splitting string into array uses a space as delimeter until command "unset IFS"
IFS=' '
split_log=($last_log)

# Differentiate if it's a new session or not as the position in the array might change.
if [[ $last_log = *"created"* ]];
	then
		status="Session:new"
		date_time=${split_log[0]}
		# remove the port using ':' as delimeter
		IFS=':'
			ori_ip_port=(${split_log[11]})
			ori_ip=${ori_ip_port[0]}
		# revert the delimeter to its previous state, hence a space
		unset IFS
		display=""
elif [[ $last_log = *"reconnected"* ]];
	then
		status="Session:reconnected"
		date_time=${split_log[0]}
		# remove the port using ':' as delimeter
		IFS=':'
			ori_ip_port=(${split_log[13]})
			ori_ip=${ori_ip_port[0]}
		# revert the delimeter to its previous state, hence a space
		unset IFS
		display="Display${split_log[9]//,}"
else
	exit
fi

# get port
netstat_result=($(sudo netstat -tulpn | grep xrdp-sesman))
# extract the port using ':' as delimeter
IFS=':'
netstat_result_ip_port=(${netstat_result[3]})
port="Port:${netstat_result_ip_port[1]}"
# revert the delimeter to its previous state, hence a space
unset IFS

# revert the delimeter to its original state (before running this script)
unset IFS

host_name=$(hostname)

# Store the log in the folder of the appropriate user
if [[ $my_ip = *$ori_ip* ]];
	then
		# Store date, time, originating ip, status, display and port
		sudo echo "$date_time $status Host:$host_name $port  $display Origin:$ori_ip" >> "$my_path$login_file"
		# Remove duplicates in the log file (as user might login again even without reusing rdp)
		sudo sort -u -o "$my_path$login_file" "$my_path$login_file"
		
elif [[ $ip2 = *$ori_ip* ]];
	then
		echo $date_time $status $port  $display $ori_ip
fi
Obviously, the variables $my_ip, $my_path, $ip2 use adapted with personalized parameters instead of the ones I put for sharing this post. (E.g. I replace myMachine with my hostname, etc.)
As $PATH gets rewritten every login, I might actually search where this happens so that I could run my script from there...
 
Old 08-03-2018, 07:05 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
sorry, i meant this:
please show us the complete startwm script (is it working otherwise?).
 
Old 08-03-2018, 07:18 AM   #8
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
sorry, i meant this:
please show us the complete startwm script (is it working otherwise?).
I assume that it's working... Is there anyway I can confirm this? (I know for sure that it's mentioned in sesman.ini.)
Code:
#!/bin/sh

if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
fi

. /etc/X11/Xsession


bash vm_logger.sh
 
Old 08-03-2018, 11:57 PM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
i'm guessing it never gets to that last line after sourcing /etc/X11/Xsession.
would need to see /etc/X11/Xsession, too (which distro is this?).
 
Old 08-04-2018, 03:54 AM   #10
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
Could you run xrdp from a script and run your script from that script?
 
Old 08-06-2018, 01:31 AM   #11
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
i'm guessing it never gets to that last line after sourcing /etc/X11/Xsession.
would need to see /etc/X11/Xsession, too (which distro is this?).
The VM is running Xubuntu (XFCE on Ubuntu) version 4.12. (Hence, Ubuntu 16.04.4 LTS.)

/etc/X11/Xsession:
Code:
#!/bin/sh
#
# /etc/X11/Xsession
#
# global Xsession file -- used by display managers and xinit (startx)

# $Id: Xsession 967 2005-12-27 07:20:55Z dnusinow $

set -e

PROGNAME=Xsession

message () {
  # pretty-print messages of arbitrary length; use xmessage if it
  # is available and $DISPLAY is set
  MESSAGE="$PROGNAME: $*"
  echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2
  if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
    echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
  fi
}

message_nonl () {
  # pretty-print messages of arbitrary length (no trailing newline); use
  # xmessage if it is available and $DISPLAY is set
  MESSAGE="$PROGNAME: $*"
  echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2;
  if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
    echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
  fi
}

errormsg () {
  # exit script with error
  message "$*"
  exit 1
}

internal_errormsg () {
  # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  # One big call to message() for the sake of xmessage; if we had two then
  # the user would have dismissed the error we want reported before seeing the
  # request to report it.
  errormsg "$*" \
           "Please report the installed version of the \"x11-common\"" \
           "package and the complete text of this error message to" \
           "<debian-x@lists.debian.org>."
}

# initialize variables for use by all session scripts

OPTIONFILE=/etc/X11/Xsession.options

SYSRESOURCES=/etc/X11/Xresources
USRRESOURCES=$HOME/.Xresources

SYSSESSIONDIR=/etc/X11/Xsession.d
USERXSESSION=$HOME/.xsession
USERXSESSIONRC=$HOME/.xsessionrc
ALTUSERXSESSION=$HOME/.Xsession
ERRFILE=$HOME/.xsession-errors

# attempt to create an error file; abort if we cannot
if (umask 077 && touch "$ERRFILE") 2> /dev/null && [ -w "$ERRFILE" ] &&
  [ ! -L "$ERRFILE" ]; then
  chmod 600 "$ERRFILE"
elif ERRFILE=$(tempfile 2> /dev/null); then
  if ! ln -sf "$ERRFILE" "${TMPDIR:=/tmp}/xsession-$USER"; then
    message "warning: unable to symlink \"$TMPDIR/xsession-$USER\" to" \
             "\"$ERRFILE\"; look for session log/errors in" \
             "\"$TMPDIR/xsession-$USER\"."
  fi
else
  errormsg "unable to create X session log/error file; aborting."
fi

# truncate ERRFILE if it is too big to avoid disk usage DoS
if [ "`stat -c%s \"$ERRFILE\"`" -gt 500000 ]; then
  T=`mktemp -p "$HOME"`
  tail -c 500000 "$ERRFILE" > "$T" && mv -f "$T" "$ERRFILE" || rm -f "$T"
fi

exec >>"$ERRFILE" 2>&1

echo "$PROGNAME: X session started for $LOGNAME at $(date)"

# sanity check; is our session script directory present?
if [ ! -d "$SYSSESSIONDIR" ]; then
  errormsg "no \"$SYSSESSIONDIR\" directory found; aborting."
fi

# Attempt to create a file of non-zero length in /tmp; a full filesystem can
# cause mysterious X session failures.  We do not use touch, :, or test -w
# because they won't actually create a file with contents.  We also let standard
# error from tempfile and echo go to the error file to aid the user in
# determining what went wrong.
WRITE_TEST=$(tempfile)
if ! echo "*" >>"$WRITE_TEST"; then
  message "warning: unable to write to ${WRITE_TEST%/*}; X session may exit" \
          "with an error"
fi
rm -f "$WRITE_TEST"

# use run-parts to source every file in the session directory; we source
# instead of executing so that the variables and functions defined above
# are available to the scripts, and so that they can pass variables to each
# other
SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)
if [ -n "$SESSIONFILES" ]; then
  set +e
  for SESSIONFILE in $SESSIONFILES; do
    . $SESSIONFILE
  done
  set -e
fi

exit 0

# vim:set ai et sts=2 sw=2 tw=80:
 
Old 08-06-2018, 01:44 AM   #12
bioinf
LQ Newbie
 
Registered: Aug 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
Could you run xrdp from a script and run your script from that script?
I understand what you'd like me to do, but the only script that I could think of was startwm.sh. Please let me know if you meant a different script.
When I try to run this script from the command line, the first time I get an error (something about keyboard and mouse) and afterwards it's doing nothing.
 
Old 08-06-2018, 03:00 AM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
i don't see anything helpful in that script, except:
ERRFILE=$HOME/.xsession-errors
might want to look at that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run automatically shell script after the user login Waiyan Lynn Linux - Newbie 1 11-20-2017 09:54 PM
How to automatically run a script on login thelynx Linux - Newbie 6 01-30-2015 09:41 AM
Run bash script automatically after the root log in Noazet Linux - Newbie 1 03-30-2014 06:23 PM
Run a script automatically upon user login? FastFeet Linux - General 3 05-21-2009 05:23 AM
Run shell script automatically after Login. hnshashi Linux - Newbie 14 09-26-2008 04:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration