Skip to content

Instantly share code, notes, and snippets.

@dash17291
Last active October 28, 2016 14:48
Show Gist options
  • Save dash17291/5434519 to your computer and use it in GitHub Desktop.
Save dash17291/5434519 to your computer and use it in GitHub Desktop.
#!/bin/bash
key="$1"
for P in /bin /sbin /usr/bin /usr/sbin
do
if [ -x $P/smbclient ]
then
SMBCLIENT=$P/smbclient
break
fi
done
[ -x $SMBCLIENT ] || exit 1
### per workgroup:
#WORKGROUP=$($SMBCLIENT -gNL $key 2>&1 | grep "^Domain=" | head -1 | sed -e "s/^[^\[]*\[//g" -e "s/\].*//g")
#CREDENTIALS=$(find /home/*/.config/smb/credentials/${WORKGROUP}/{key}.credentials | head -1)
### simplified:
CREDENTIALS=$(find /home/*/.config/smb/${key}.credentials 2> /dev/null | head -1)
opts="-fstype=cifs"
if [ "$CREDENTIALS" ]
then
opts="$opts,credentials=$CREDENTIALS"
LOCAL_USER=$(dirname $CREDENTIALS | sed "s,^/home/\([^/]*\)/.*$,\1,g")
else
opts="$opts,guest"
CREDENTIALS_DIR=""
CREDENTIALS_DIR=$(find /home/*/.config/smb/ -maxdepth 0 2> /dev/null | head -1)
if [ -z "$CREDENTIALS_DIR" ]; then
# no any credentials file or directory
exit 1
fi
LOCAL_USER=$(dirname $CREDENTIALS_DIR | sed "s,^/home/\([^/]*\)/.*$,\1,g")
fi
opts="$opts,uid=$LOCAL_USER,gid=$LOCAL_USER"
$SMBCLIENT -gNL $key 2>/dev/null| awk -v key="$key" -v opts="$opts" -F'|' -- '
BEGIN { ORS=""; first=1 }
/Disk/ {
if (first)
print opts; first=0
dir = $2
loc = $2
# Enclose mount dir and location in quotes
# Double quote "$" in location as it is special
gsub(/\$$/, "\\$", loc);
gsub(/\&/,"\\\\&",loc)
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
}
END { if (!first) print "\n"; else exit 1 }
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment