#!/bin/bash # Mounts Android Phones and USB drives (encrypted or not). This script will # replace the older `dmenumount` which had extra steps and couldn't handle # encrypted drives. # TODO: Try decrypt for drives in crtypttab # TODO: Add some support for connecting iPhones (although they are annoying). IFS=' ' # Function for escaping cell-phone names. escape() { echo "$@" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g"; } # Function for prompting user for a mountpoint. getmount() { [ -z "$1" ] && mp="$(find "/media/$USER" /mnt -mindepth 1 -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")" || mp="$1" test -n "$mp" if [ ! -d "$mp" ]; then mkdiryn=$(printf "Yes\nNo" | dmenu -i -p "$mp does not exist. Create it?") [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" 2>/dev/null || sudo -A mkdir -p "$mp") fi } attemptmount() { # Attempt to mount without a mountpoint, to see if drive is in fstab. mplabel=$(sudo lsblk -no "label" "$chosen") mp="/media/$USER/$mplabel" [ -n "$mplabel" ] || return if [ ! -d "$mp" ] && [ ! -d "/mnt/$mplabel" ]; then getmount "$mp" && sudo -A mount "$chosen" "$mp" >/dev/null 2>&1 || return 1 elif [ -d "$mp" ] && [ ! -d "/mnt/$mplabel" ]; then sudo -A mount "$chosen" "$mp" >/dev/null 2>&1 || return 1 elif [ -d "/mnt/$mplabel" ]; then getmount "/mnt/$mplabel" && sudo -A mount "$chosen" "/mnt/$mplabel" >/dev/null 2>&1 || return 1 else sudo -A mount "$chosen" >/dev/null 2>&1 || return 1 fi } notify-send -t 10000 "🔎 Searching drives to mount..." # Check for phones. phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/📱/")" mountedphones="$(grep "simple-mtpfs" /etc/mtab)" # If there are already mounted phones, remove them from the list of mountables. [ -n "$mountedphones" ] && phones="$(for phone in $phones; do for mounted in $mountedphones; do escphone="$(escape "$phone")" [[ "$mounted" =~ $escphone ]] && break 1 done && continue 1 echo "$phone" done)" iphones="$(ideviceinfo | grep -E "^DeviceName" | sed "s/DeviceName: //;s/^/🍎/")" mountediphones="$(grep "ifuse" /etc/mtab)" [ -n "$mountediphones" ] && iphones="$(for iphone in $iphones; do for mounted in $mountediphones; do esciphone="$(escape "$iphone")" [[ "$mounted" =~ $esciphone ]] && break 1 done && continue 1 echo "$iphone" done)" # Check for drives. lsblkoutput="$(sudo lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")" # Get all LUKS drives allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)" # Get a list of the LUKS drive UUIDs already decrypted. decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" # Functioning for formatting drives correctly for dmenu: filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}'; } # Get only LUKS drives that are not decrypted. unopenedluks="$(for drive in $allluks; do uuid="${drive%% *}" uuid="${uuid//-/}" # This is a bashism. [ -n "$decrypted" ] && for open in $decrypted; do [ "$uuid" = "$open" ] && break 1 done && continue 1 echo "🔒 $drive" done | filter)" # Get all normal, non-encrypted or decrypted partitions that are not mounted. normalparts="$(echo "$lsblkoutput" | grep -v crypto_LUKS | grep -v 'part 1M' | grep 'part\|rom\|crypt' | sed "s/^/💾 /" | filter)" # Get all available IP addresses with open Samba shares in the wlan0 subnet, excluding eth0 IP smbips="$(sudo arp-scan --interface=eth0 --interface=wlan0 --localnet | grep -vEi '(EFM Networks|DUP:)' | awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/{print $1}')" # Get currently mounted CIFS shares mountedsmbs="$(grep 'cifs' /etc/mtab)" for smbip in $smbips; do win=$( smbclient -L "$smbip" -U% -A /dev/stdin </dev/null | awk '/Disk/ {print $1}' | grep -vEi '(ADMIN|\w)\$' username=$(whoami) password=$(pass show default/windows) EOF ) mac=$( smbclient -L "$smbip" -U% -A /dev/stdin </dev/null | awk '/Disk/ {print $1}' | grep -vEi '(ADMIN|\w)\$|Macintosh' username=$(whoami) password=$(pass show default/mac) EOF ) smb=$( smbclient -L "$smbip" -U% -A /dev/stdin </dev/null | awk '/Disk/ {print $1}' | grep -vEi '(ADMIN|\w)\$|Macintosh' username=$(whoami) password=$(pass show default/default) EOF ) while IFS= read -r share; do if ! echo "$smbshares" | grep -q "$share"; then smbshares+="//$smbip/$share"$'\n' fi done <<<"$win" while IFS= read -r share; do if ! echo "$smbshares" | grep -q "$share"; then smbshares+="//$smbip/$share"$'\n' fi done <<<"$mac" while IFS= read -r share; do if ! echo "$smbshares" | grep -q "$share"; then smbshares+="//$smbip/$share"$'\n' fi done <<<"$smb" done smbshares="$(echo "$smbshares" | sed '/^$/d')" [ -n "$smbshares" ] && smbs="$(echo "$smbshares" | while IFS= read -r smb; do [ -n "$mountedsmbs" ] && for mountedsmb in $mountedsmbs; do mountedsmb="${mountedsmb%% *}" [[ "$mountedsmb" =~ $smb ]] && break 1 done && continue 1 echo "📡 ${smb#//*}" done)" # Add all to one variable. If no mountable drives found, exit. alldrives="$(echo "$phones $iphones $unopenedluks $normalparts $smbs" | sed "/^$/d;s/ *$//")" # Quit the script if a sequential command fails. set -e test -n "$alldrives" || { echo "No mountable drives" && exit; } # Feed all found drives to dmenu and get user choice. chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)" case "$chosen" in 💾*) chosen="${chosen%% *}" chosen="${chosen:1}" # This is a bashism. parttype="$(echo "$lsblkoutput" | grep "$chosen")" attemptmount || { getmount case "${parttype##* }" in vfat) sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000 ;; btrfs) sudo -A mount "$chosen" "$mp" ;; *) sudo -A mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)" ;; esac } notify-send "💾 Drive Mounted." "$chosen mounted to $mp." ;; 🔒*) chosen="${chosen%% *}" chosen="${chosen:1}" # This is a bashism. # Number the drive. while [ -e "/dev/mapper/usb$num" ]; do num=$((num + 1)) done pass show default/default | sudo cryptsetup open "$chosen" "usb$num" || ${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$chosen" "usb$num" # Check if now decrypted. test -b "/dev/mapper/usb$num" chosen="/dev/mapper/usb$num" attemptmount || { getmount sudo -A mount "/dev/mapper/usb$num" "$mp" -o uid="$(id -u)",gid="$(id -g)" || sudo -A mount "/dev/mapper/usb$num" "$mp" } notify-send "🔓 Decrypted drive Mounted." "$chosen decrypted and mounted to $mp." ;; 📱*) notify-send "❗ Note" "Remember to allow file access on your phone now." getmount number="${chosen%%:*}" number="${chosen:1}" # This is a bashism. sudo -A simple-mtpfs -o allow_other -o fsname="simple-mtpfs-$(escape "$chosen")" --device "$number" "$mp" 2>dev/null notify-send "🤖 Android Mounted." "Android device mounted to $mp." ;; 🍎*) getmount number="${chosen%%:*}" number="${chosen:1}" # This is a bashism. sudo -A ifuse "$mp" 2>dev/null notify-send "🍎 iPhone Mounted." "$chosen mounted to $mp." ;; 📡*) chosen="//${chosen##* }" path="${chosen##*/}" if getmount "/media/$USER/$path"; then sudo -A mount -t cifs -o username="$(whoami)",password="$(pass show default/default)" "$chosen" "$mp" 2>/dev/null || sudo -A mount -t cifs -o username="$(whoami)",password="$(pass show default/windows)" "$chosen" "$mp" 2>/dev/null else notify-send "❌ Failed to mount samba" "$chosen Check if $(whoami) is added to samba user list." sudo rm -rf "$mp" exit fi notify-send "📡 Samba successfully mounted to" "$mp" ;; esac