From 9d2c88b57de47d1cae8f1e6566517ce9a4e16549 Mon Sep 17 00:00:00 2001 From: Dennis Sengstock Date: Mon, 19 May 2025 12:59:29 +0200 Subject: [PATCH] Some redesign of the script to make it more stable and easier to use. --- remote-admin.sh | 141 ++++++++++++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 59 deletions(-) mode change 100644 => 100755 remote-admin.sh diff --git a/remote-admin.sh b/remote-admin.sh old mode 100644 new mode 100755 index 8f15e15..4efab4e --- a/remote-admin.sh +++ b/remote-admin.sh @@ -1,69 +1,92 @@ #!/bin/bash -# This script checks for the next free remote-admin pc and established a RDP session using xfreerdp to it. -# To use this script you need to create two files in your home directory -# -# .smbcredentials -# username=YOUR AD USERNAME -# password=YOUR AD PASSWORD -# -# .remote-admin -# USERNAME=administrator@zfd.forumzfd.de -# PASSWORD=AD ADMIN PASSWORD FROM BITWARDEN -# -# Make sure to proper chmod them (e.g. chmod 600) -# -# Add the following line to your /etc/fstab and edit YOUR_USERNAME -# -# //cluster-ho/Ampel /tmp/remote-ampel cifs credentials=/home/YOUR_USERNAME/.smbcredentials,user,noauto,workgroup=ZFD,dir_mode=0777,file_mode=0777,_netdev 0 0 -# -# setuid might be necessary on /usr/sbin/mount.cifs. Set it as root with -# -# chmod u+s /usr/sbin/mount.cifs +# Enhanced remote-admin connection script +# Checks for free remote-admin PC via SMB share and launches RDP connection -AMPEL_MOUNT=/tmp/remote-ampel +AMPEL_MOUNT="/tmp/remote-ampel" +SHARE="//cluster-ho/Ampel" +SMB_CRED="$HOME/.smbcredentials" +RDP_CRED="$HOME/.remote-admin" +FSTAB_ENTRY="$SHARE $AMPEL_MOUNT cifs credentials=$SMB_CRED,user,noauto,workgroup=ZFD,dir_mode=0777,file_mode=0777,_netdev 0 0" -if [ -f ~/.remote-admin ]; then - source ~/.remote-admin -else - echo ".remote-admin not found! Please create it!" - exit 4 -fi -if [ ! -f ~/.smbcredentials ]; then - echo ".smbcredentials not found! Please create it!" - exit 5 -fi -# Check for xfreerdp -if [ ! -f /usr/bin/xfreerdp ]; then - print "xfreerdp not found. Make sure it is installed" +# Logging function +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" +} + +ask_permission() { + read -p "$1 (y/n): " answer + [[ "$answer" =~ ^[Yy]$ ]] +} + +# Trap for cleanup +cleanup() { + umount "$AMPEL_MOUNT" 2>/dev/null + log "Unmounted $AMPEL_MOUNT." +} +trap cleanup EXIT + +# Check for credentials +if [ ! -f "$RDP_CRED" ]; then + log "$RDP_CRED not found! Please create it!" exit 1 -else # check for the mount point - mkdir -p $AMPEL_MOUNT - if grep -qs $AMPEL_MOUNT /proc/mounts; then - echo "$AMPEL_MOUNT already mounted." - else - echo "Trying to mount $AMPEL_MOUNT" - mount $AMPEL_MOUNT - if [ $? -eq 0 ]; then - echo "Mount successful." - else - echo "Mount failed. Check share" - exit 2 - fi +fi +if [ ! -f "$SMB_CRED" ]; then + log "$SMB_CRED not found! Please create it!" + exit 2 +fi + +# Source RDP credentials +source "$RDP_CRED" + +# Detect FreeRDP +FREERDP_BIN=$(command -v xfreerdp || command -v wfreerdp) +if [ -z "$FREERDP_BIN" ]; then + log "No FreeRDP client found (xfreerdp or wfreerdp). Please install one." + exit 3 +fi + +# Check fstab entry +if ! grep -qs "$AMPEL_MOUNT" /etc/fstab; then + log "Fstab entry for $AMPEL_MOUNT not found." + if ask_permission "Shall I add the entry automatically?"; then + sudo bash -c "echo '$FSTAB_ENTRY' >> /etc/fstab" + log "Fstab entry added." fi fi -sleep 2 -for i in $(ls $AMPEL_MOUNT) -do - if [[ $i == *"FREE"* ]]; then - RDP=$(echo $i | cut -f1,2 -d'-') - echo "$RDP is free :)" - xfreerdp /u:$USERNAME /p:$PASSWORD /v:$RDP.zfd.forumzfd.de /dynamic-resolution - umount $AMPEL_MOUNT - echo 'Connection closed. Bye' - exit 0 + +# Check mount.cifs permissions +if [ ! -u /usr/sbin/mount.cifs ]; then + log "mount.cifs is missing setuid bit." + if ask_permission "Shall I set it automatically?"; then + sudo chmod u+s /usr/sbin/mount.cifs + log "Setuid bit set on /usr/sbin/mount.cifs." fi +fi + +# Mount the share +mkdir -p "$AMPEL_MOUNT" +if grep -qs "$AMPEL_MOUNT" /proc/mounts; then + log "$AMPEL_MOUNT already mounted." +else + log "Mounting $AMPEL_MOUNT..." + if ! mount "$AMPEL_MOUNT"; then + log "Mount failed. Check share configuration." + exit 4 + fi + log "Mount successful." +fi + +# Search for free remote admin PC +sleep 1 +for i in "$AMPEL_MOUNT"/*; do + [[ "$i" == *FREE* ]] || continue + RDP=$(basename "$i" | cut -f1,2 -d'-') + log "$RDP is free. Starting RDP session..." + "$FREERDP_BIN" /u:"$USERNAME" /p:"$PASSWORD" /v:"$RDP.zfd.forumzfd.de" /dynamic-resolution + log "Connection closed. Bye." + exit 0 done -echo 'No Remote-Admin is free. :( Try again later.' -umount $AMPEL_MOUNT + +log "No Remote-Admin is free. Try again later." exit 0