Compare commits

...

2 commits

2 changed files with 158 additions and 74 deletions

View file

@ -1,29 +1,90 @@
## README # README
This repository contains some more or less usefull linux scripts. ;) This repository contains some more or less usefull linux scripts. ;)
**`remote-admin.sh`** **`remote-admin.sh`**
This script checks for the next free remote-admin pc and established a RDP session using xfreerdp to it. ## Remote Admin Enhanced
To use this script you need to create two files in your home directory
.smbcredentials This script automatically connects you to a free remote admin PC via RDP. It uses an SMB share to check availability and FreeRDP to establish the connection. The script verifies necessary system settings and assists with setup.
username=YOUR AD USERNAME ## Requirements
password=YOUR AD PASSWORD
.remote-admin * Bash shell
* FreeRDP (either `xfreerdp` or `wfreerdp`)
* Access to the SMB share `//cluster-ho/Ampel`
* Two configuration files:
* `~/.smbcredentials` (for SMB access)
* `~/.remote-admin` (for RDP login)
## Configuration Files
### \~/.smbcredentials
```
username=YOUR_AD_USERNAME
password=YOUR_AD_PASSWORD
```
**Important:** Set secure file permissions:
```bash
chmod 600 ~/.smbcredentials
```
### \~/.remote-admin
```
USERNAME=administrator@zfd.forumzfd.de USERNAME=administrator@zfd.forumzfd.de
PASSWORD=AD ADMIN PASSWORD FROM BITWARDEN PASSWORD=YOUR_ADMIN_PASSWORD
```
Make sure to proper chmod them (e.g. `chmod 600`) **Secure this file too:**
Add the following line to your /etc/fstab and edit YOUR_USERNAME ```bash
chmod 600 ~/.remote-admin
```
## fstab Configuration
Add the following line to `/etc/fstab` (if not already present):
```
//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 //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 The script can also add this line automatically (after confirmation).
chmod u+s /usr/sbin/mount.cifs ## Preparing mount.cifs
Make sure you have xfreerdp installed on your computer. To allow regular users to mount the share, `mount.cifs` must have the `setuid` bit:
```bash
sudo chmod u+s /usr/sbin/mount.cifs
```
This step is also detected by the script and can be applied automatically after confirmation.
## Usage
```bash
./remote-admin.sh
```
The script:
* checks whether all requirements are met,
* mounts the SMB share (if not already mounted),
* searches for a free remote admin PC,
* starts the RDP connection,
* and automatically unmounts the share when finished.
## Notes
* If no remote admin PC is available, the script will let you know.
* You can safely run the script multiple times.
* Users are expected to install FreeRDP themselves, if it's not already present.
---
Enjoy your remote adventures and happy adminning! ✨

137
remote-admin.sh Normal file → Executable file
View file

@ -1,69 +1,92 @@
#!/bin/bash #!/bin/bash
# This script checks for the next free remote-admin pc and established a RDP session using xfreerdp to it. # Enhanced remote-admin connection script
# To use this script you need to create two files in your home directory # Checks for free remote-admin PC via SMB share and launches RDP connection
#
# .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
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 # Logging function
source ~/.remote-admin log() {
else echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
echo ".remote-admin not found! Please create it!" }
exit 4
fi ask_permission() {
if [ ! -f ~/.smbcredentials ]; then read -p "$1 (y/n): " answer
echo ".smbcredentials not found! Please create it!" [[ "$answer" =~ ^[Yy]$ ]]
exit 5 }
fi
# Check for xfreerdp # Trap for cleanup
if [ ! -f /usr/bin/xfreerdp ]; then cleanup() {
print "xfreerdp not found. Make sure it is installed" 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 exit 1
else # check for the mount point fi
mkdir -p $AMPEL_MOUNT if [ ! -f "$SMB_CRED" ]; then
if grep -qs $AMPEL_MOUNT /proc/mounts; then log "$SMB_CRED not found! Please create it!"
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 exit 2
fi 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
fi fi
sleep 2
for i in $(ls $AMPEL_MOUNT) # Check mount.cifs permissions
do if [ ! -u /usr/sbin/mount.cifs ]; then
if [[ $i == *"FREE"* ]]; then log "mount.cifs is missing setuid bit."
RDP=$(echo $i | cut -f1,2 -d'-') if ask_permission "Shall I set it automatically?"; then
echo "$RDP is free :)" sudo chmod u+s /usr/sbin/mount.cifs
xfreerdp /u:$USERNAME /p:$PASSWORD /v:$RDP.zfd.forumzfd.de /dynamic-resolution log "Setuid bit set on /usr/sbin/mount.cifs."
umount $AMPEL_MOUNT fi
echo 'Connection closed. Bye' 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 exit 0
fi
done done
echo 'No Remote-Admin is free. :( Try again later.'
umount $AMPEL_MOUNT log "No Remote-Admin is free. Try again later."
exit 0 exit 0