network_shares_automount/sbin/scanmounts
MymsMan d4b65def69 Version 1.5
- fix grep to add -F flag to allow for mounts containing [name]
- Also timestamp log records
- Do blot log password
2024-02-05 16:26:17 +00:00

177 lines
5.7 KiB
Bash
Executable File

#!/bin/sh
TryMount()
{
type="$1"
name="$2"
doShareFolder=$(ls -1 "$name" | grep "shareFolder=on")
if [ "$doShareFolder" ]; then
if [ -d "/media/My Video" ]; then
sharesFolder="/media/My Video/[Shares] Do not delete!"
else
sharesFolder="/media/drive1/Video/[Shares] Do not delete!" # don't really need on HD as we can asccess the root
fi
fi
host=$(ls -1 "$name" | grep -e "^host" | cut -d'=' -f2 | sed -e 's/_/./g') #convert "_"s to "."s in host
folder=$(ls -1 "$name" | grep -e "^folder" | cut -d'=' -f2 | sed -e 's/_/\//g') #convert "_"s to "/"s in folder name
# should we wake up constantly?
wakeConstantly=$(ls -1 "$name" | grep "wakeConstantly")
# should we wake up now?
wake=$(ls -1 "$name" | grep "wakeNow")
if [ "$wake" = "wakeNow" ] || [ "$wakeConstantly" = "wakeConstantly" ]; then
mac=$(ls -1 "$name" | grep -e "^mac" | cut -d'=' -f2)
/mod/sbin/wakecmd "$mac" "$host"
if [ "$wake" = "wakeNow" ]; then
/mod/bin/busybox/rmdir "$name/wakeNow"
fi
fi
if [ ! -d "$name/wakeNow?" ]; then
mkdir "$name/wakeNow?"
fi
if [ ! -d "$name/wakeConstantly?" ] && [ ! -d "$name/wakeConstantly" ]; then
mkdir "$name/wakeConstantly?"
fi
ping -c 2 -w 3 $host > /dev/null # try ping again
pingresult=$?
tim=$(date '+%Y-%m-%d %H:%M:%S')
if [ $pingresult != 0 ]; then # mount or unmount?
#echo "$host not responding.."
mounted=$(mount | grep -F "/media/$name")
if [ "$mounted" ]; then
echo "$tim - $host not found - unmounting.."
# unmount if mounted
echo "umount "/media/$name""
umount "/media/$name" -l
/mod/bin/busybox/rmdir "/media/$name"
if [ "$sharesFolder" ]; then
umount "$sharesFolder/$name" -l
/mod/bin/busybox/rmdir "$sharesFolder/$name"
fi
fi
else
if [ ! -d "/media/$name" ]; then
echo "$tim - $host is on-line - attempting to mount $name"
mkdir "/media/$name" # and in My Videos?
if [ $type = "smb" ]; then
user=$(ls -1 "$name" | grep -e "^user") # keep the [user|password etc]=... for these as they go into mount int that format
password=$(ls -1 "$name" | grep -e "^password")
domain=$(ls -1 "$name" | grep -e "^domain")
unc="unc=\\\\$host\\`echo $folder | sed 's^/^\\\\^g'`"
echo "mount -t cifs "//$host/$folder" "/media/$name" -o $user,password=#######,$domain,$unc"
mount -t cifs "//$host/$folder" "/media/$name" -o "$user,$password,$domain,$unc"
elif [ $type = "nfs" ]; then
echo "mount -o soft "$host:/$folder" "/media/$name""
mount -o soft "$host:/$folder" "/media/$name"
fi
if [ $? != 0 ]; then # failed
echo "Mount failed..."
umount "/media/$name" -l
/mod/bin/busybox/rmdir "/media/$name"
elif [ "$sharesFolder" ]; then
mkdir -p "$sharesFolder/$name" # and in My Videos?
mount "/media/$name" "$sharesFolder/$name"
fi
fi
fi
}
CreateExample()
{
type="$1"
name="$2"
mkdir "$name/host=10_0_1_XX"
mkdir "$name/shareFolder=off"
mkdir "$name/mac=ABABABABABAB (only needed for wakeUp)"
mkdir "$name/wakeNow?"
mkdir "$name/wakeConstantly?"
if [ $type = "smb" ]; then
mkdir "$name/folder=ShareFolder"
mkdir "$name/user=User"
mkdir "$name/password=Password"
mkdir "$name/domain=Domain or Workgroup"
else
mkdir "$name/folder=ShareFolder (use _ for slash)"
fi
}
exec >/tmp/scanmounts.log 2>&1
date '+%Y-%m-%d %H:%M:%S'
if [ ! -d /mod/settings ]; then
mkdir /mod/settings
fi
cd /mod/settings
# create some help 1st time
if [ ! -d smb ]; then
mkdir -p "smb/*Create folder with mount name (opt+)"
mkdir -p "smb/*Then edit options in folder"
fi
if [ ! -d nfs ]; then
mkdir -p "nfs/*Create folder with mount name (opt+)"
mkdir -p "nfs/*Then edit options in folder"
fi
# set up links to where we can get at the settings folder (a bit messy due to HDR / HD differences)
mounted=$(mount | grep "\[ModSettings\]")
if [ ! "$mounted" ]; then
if [ -d "/media/My Video/[ModSettings]" ]; then # HDR - add link to settings here as it can't get to /mod
mount /mod/settings "/media/My Video/[ModSettings]"
elif [ -d "/media/drive1/[ModSettings]" ]; then # HD put [Modsettings] on root and add link to settings
mount /mod/settings "/media/drive1/[ModSettings]"
fi
fi
# reset wake constantly flags on restart and remove old settings
for type in smb nfs; do
if [ -d $type ]; then
cd $type
for mountname in [!*]*; do
if [ -d "$mountname/wakeConstantly" ]; then
mv "$mountname/wakeConstantly" "$mountname/wakeConstantly?"
fi
if [ -d "$name/wakeNow" ]; then
/mod/bin/busybox/rmdir "$name/wakeNow"
fi
if [ -d "$mountname/wakeUp=off" ]; then
/mod/bin/busybox/rmdir "$mountname/wakeUp=off"
fi
done
cd .. # back up to settings
fi
done
# now loop forever checking the mounts
while [ 1 ]
do
for type in smb nfs; do
if [ -d $type ]; then
cd $type
for mountname in [!*]*; do #ignore "comments" starting with *
if [ -d "$mountname" ]; then
#echo "Checking $mountname"
if [ "$(ls -A "$mountname")" != "" ]; then
TryMount $type "$mountname"
else
CreateExample $type "$mountname" #Empty dir - create examples
fi
fi
done
cd .. # back up to settings
fi
done
sleep 1
done