#!/bin/bash if [ -z "$1" ]; then echo "Please pass the password you would like to use as an argument to this script"; exit 1 ; else echo "Good start.going ahead."; fi apt update && apt install tmux -y password=$1 passwordfile='/etc/x11vnc.pass' servicefile='/etc/systemd/system/x11vnc.service' timerfile='/etc/systemd/system/x11vnc.timer' script='/usr/bin/startxvnc' apt-get update apt-get install x11vnc net-tools -y x11vnc -storepasswd $password $passwordfile cat >$servicefile <<'EOT' [Unit] Description="x11vnc" Requires=display-manager.service [Service] Type=simple #Environment=XAUTHORITY=$(ps aux | grep -o -E "(-auth)(.*)(Xauth)[a-zA-Z]+") #ExecStart=tmux new -s gj -d '/usr/bin/x11vnc $(ps aux | grep -o -E "(-auth)(.*)(Xauth)[a-zA-Z]+") -display :0 -rfbauth /etc/x11vnc.pass -shared -forever' #ExecStartPre=tmux new -s x11vnc -d ExecStart=bash -c /usr/bin/startxvnc #ExecStop=tmux kill-session -t x11vnc Restart=on-failure RestartSec=2 [Install] WantedBy=graphical.target WantedBy=multi-user.target EOT cat >$timerfile <<'EOT' Unit] Description=Wait for some time before running X11VNC [Timer] OnBootSec=5sec [Install] WantedBy=timers.target EOT cat >$script <<'EOT' #!/bin/bash getxauths() { xauths=() ps aux | grep -o -E "(-auth)(.*)(Xauth)[a-zA-Z]+" | (while IFS= read -r xauth do xauths+=("$xauth") done printf "%s\n" "${xauths[@]}" | sort -u ) } getpname() { #psp=$(ps -p $(lsof -t -i:5900) -o command) psp=$(cat /proc/$(lsof -t -i:$1)/cmdline) echo $psp } killproc() { pkill -9 $(lsof -t -i:$1) echo "Killed by port $1" } starttmux() { xauth=$1 tmux new -s $xauth -d tmux send-keys -t $xauth:0 "for (( ; ; )) ; do sleep 3s; ((disp ^= 1)) ; /usr/bin/x11vnc -display :\$disp -auth $xauth -rfbauth /etc/x11vnc.pass -shared -forever ; done" Enter } #echo $(getpname) start() { for (( ; ; )) ; do #echo $psp xauths=$(getxauths) for xauth in $xauths ; do xauth="$xauth" | xargs if [[ ${xauth} != *"-auth"* ]];then unset tsessions tsessions=($(tmux list-sessions -F '#{session_name}')) echo "tessions is " ${tsessions[@]} echo "xauth is " ${xauth} if [[ ! " ${tsessions[@]} " =~ " ${xauth} " ]]; then starttmux $xauth psp=$(getpname 5900) echo $psp | grep -E -o "(-auth)(.*)(Xauth)[a-zA-Z]+" | (while IFS= read -r xold do sessionname=$(echo ${xold#"-auth"} | xargs) #tmux kill-session -t "$sessionname" if [[ ! " ${tsessions[@]} " =~ " ${xauth} " ]]; then if [[ $psp == *"user/125"* ]] ; then killproc 5900 ; tmux kill-session -t "$sessionname" ; echo "session name is $sessionname" ; fi ; else starttmux $xauth ; sleep 30s ; fi done) fi fi done sleep 3s done } start EOT chmod u+x $script sed -i -e 's/WaylandEnable=true/WaylandEnable=false/g' /etc/gdm3/custom.conf sed -i -e 's/#WaylandEnable=false/WaylandEnable=false/g' /etc/gdm3/custom.conf systemctl daemon-reload systemctl enable x11vnc.timer systemctl stop x11vnc.service systemctl disable x11vnc.service systemctl restart x11vnc.timer