transferring several commands from private repo

This commit is contained in:
Ville Rantanen
2021-10-08 11:19:58 +03:00
parent 754938ca32
commit 707eba3ead
20 changed files with 1140 additions and 289 deletions

129
web/rclone-mount Executable file
View File

@@ -0,0 +1,129 @@
#!/bin/bash
_helpexit() {
printf "Usage: %s [-u] remote:[path]
remote from rclone config. Note, all remotes end with :
-u will unmount all FUSE.rclone paths if no path given!
" "$( basename $0 )"
echo Current mounts:
cat /proc/mounts | grep fuse.rclone | awk '{ print $1 "\t" $2 }'
exit 1
}
_menu() {
echo Current mounts:
cat /proc/mounts | grep fuse.rclone | awk '{ print $1 "\t" $2 }'
_askpass
choice=$( rclone --ask-password=false listremotes | \
sort | \
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select remote" \
-N -D n:1 i:1 )
echo $choice
if [[ -z "$choice" ]]; then
_helpexit
fi
remote="$choice"
}
_askpass() {
if [[ -n "$RCLONE_CONFIG_PASS" ]]; then
# password already set
return
fi
if grep -q ^"# Encrypted rclone configuration File" "$HOME/.config/rclone/rclone.conf"; then
read -s -p "rclone config password: " RCLONE_CONFIG_PASS
export RCLONE_CONFIG_PASS
else
RCLONE_CONFIG_PASS="not-required"
true
fi
}
_check_mount() {
if [[ -d "$valid_name" ]]; then
device1=$( stat -c "%d" ~/mnt/"$valid_name" )
device2=$( stat -c "%d" ~/mnt )
else
device1=valid
device2=valid
fi
}
[[ -z "$1" ]] && _menu
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && _helpexit
[[ ${!i} = "--help" ]] && _helpexit
done
unmount=false
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-u" ]] && { unmount=true; continue; }
if [[ -z "$remote" ]]; then
remote="${!i}"
fi
done
mkdir -p ~/mnt
valid_name=$( echo "${remote%:}" | sed -e 's/:/-/g' | sed -e 's/[^A-Za-z0-9._@-]//g' )
if [[ "$unmount" = true ]]; then
cd ~/mnt
if [[ -n "$remote" ]]; then
fusermount -u -z ~/mnt/$valid_name
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
else
# no path, unmount all
cat /proc/mounts | grep fuse.rclone | awk '{ print $2 }' | while read dir; do
echo Unmounting $dir
fusermount -u -z "$dir"
rmdir --ignore-fail-on-non-empty "$dir" &>/dev/null
done
fi
exit
fi
if [[ -z "$remote" ]]; then
echo "No remote given"
_helpexit
fi
_check_mount
tries=20
if [[ $device1 = $device2 ]]; then
echo "Mounting $remote in ~/mnt/$valid_name"
_askpass
cd ~/mnt
mkdir -p "$valid_name"
echo ""
rclone --ask-password=false mount --daemon "${remote}" ~/mnt/"$valid_name"
if [[ $? -gt 0 ]]; then
tries=1
fi
echo "Waiting for mount to finish ..."
else
echo "~/mnt/$valid_name is already mounted"
exit
fi
while [[ $tries -gt 0 ]]; do
read -t 1 -n 1 key
_check_mount
if [[ $device1 = $device2 ]]; then
tries=$(( $tries - 1 ))
else
df -h "$valid_name"
exit 0
fi
done
echo $remote was not mounted
fusermount -u -z ~/mnt/$valid_name &>/dev/null
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
exit 1

9
web/ssh-nosave Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
if [[ "$1" = "-h" ]]; then
echo "Set options to not use host key checking, and pass all commands to 'ssh'."
exit
fi
exec ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$@"

71
web/ssh-scan-lan Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/bash
if [[ "$1" = "-h" ]]; then
echo This scans your LAN for SSH servers.
echo 'Requires: nmap; ifconfig or nmcli'
exit
fi
PATH=$PATH:/sbin/:/usr/sbin
which nmap &>/dev/null || {
echo nmap required
exit 1
}
which ifconfig &>/dev/null && {
IPLIST="ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*'"
ifmask() {
ifconfig | grep $1 | \
grep -Eo 'netmask (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'
}
}
which nmcli &>/dev/null && {
IPLIST="nmcli | grep -Eo 'inet4 ([0-9]*\.){3}[0-9]*'"
ifmask() {
cidr=$( nmcli | grep 'inet4 ' | grep $1 | \
grep -Eo '([0-9]*$)' )
value=$(( 0xffffffff ^ ((1 << (32 - $cidr)) - 1) ))
echo "$(( (value >> 24) & 0xff )).$(( (value >> 16) & 0xff )).$(( (value >> 8) & 0xff )).$(( value & 0xff ))"
}
}
servers_found=""
while read myip; do
ip_base=$( echo $myip | cut -d. -f 1-3 )
mymask=$( ifmask $myip )
IFS=. read -r i1 i2 i3 i4 <<< "$myip"
IFS=. read -r m1 m2 m3 m4 <<< "$mymask"
f1=$((i1 & m1))
f2=$((i2 & m2))
f3=$((i3 & m3))
f4=$(((i4 & m4)+1))
l1=$((i1 & m1 | 255-m1))
l2=$((i2 & m2 | 255-m2))
l3=$((i3 & m3 | 255-m3))
l4=$(((i4 & m4 | 255-m4)-1))
first_ip="$f1.$f2.$f3.$f4"
last_ip="$l1.$l2.$l3.$l4"
printf "IP: %15s/%-15s scan: %15s - %-15s\n" \
"$myip" "$mymask" "$first_ip" "$last_ip"
while read line; do
if [[ "$line" = "#"* ]]; then continue; fi
ip=$( echo $line | grep -Eo '([0-9]*\.){3}[0-9]*' )
if [[ $ip = $myip ]]; then continue; fi
ssh_server=$( ssh-keyscan -p 22 -t rsa -T 1 "$ip" 2>&1 | grep ^# | sed 's/:\([0-9]\+\)/ -p \1/' )
printf -v servers_found "%s%s\n" "$servers_found" "$ssh_server"
done < <( nmap -T5 -sP "$f1-$l1.$f2-$l2.$f3-$l3.$f4-$l4" -oG - )
done < <( eval $IPLIST | \
grep -Eo '([0-9]*\.){3}[0-9]*' | \
grep -v 127.0.0.1 | \
grep -v ^172. )
echo "SSH Servers found:"
printf "%s" "$servers_found" | sort -n | grep -v "^$"

107
web/sshfs-mount Executable file
View File

@@ -0,0 +1,107 @@
#!/bin/bash
_helpexit() {
printf "Usage: %s [-u] [-nosave] host [path]
host can be from ssh config
path is the remote path. Defaults to remote home.
-u will unmount all FUSE.sshfs paths if no path given!
-nosave will not use host key checking, nor save it anywhere. potentially dangerous.
" "$( basename $0 )"
echo Current mounts:
cat /proc/mounts | grep fuse.sshfs | awk '{ print $1 "\t" $2 }'
exit 1
}
_menu() {
echo Current mounts:
cat /proc/mounts | grep fuse.sshfs | awk '{ print $1 "\t" $2 }'
choice=$( grep -Pi "^host ([^*]+)$" $HOME/.ssh/config | \
sed 's/host //i' | tr ' ' '\n' | sort | \
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select server" \
-N -D n:1 i:1 )
echo $choice
if [[ -z "$choice" ]]; then
_helpexit
fi
host="$choice"
}
[[ -z "$1" ]] && _menu
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && _helpexit
[[ ${!i} = "--help" ]] && _helpexit
done
unmount=false
nosave=false
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-u" ]] && { unmount=true; continue; }
[[ ${!i} = "-nosave" ]] && { nosave=true; continue; }
if [[ -z "$host" ]]; then
host="${!i}"
else
localpath="${!i}"
fi
done
mkdir -p ~/mnt
valid_name=$( echo "$host" | sed -e 's/[^A-Za-z0-9._@-]//g')
path=:"${localpath}"
if [[ "$nosave" = true ]]; then
NOSAVE="-o ssh_command='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'"
fi
if [[ "$unmount" = true ]]; then
cd ~/mnt
if [[ -n "$host" ]]; then
fusermount -u -z ~/mnt/$valid_name
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
else
# no path, unmount all
cat /proc/mounts | grep fuse.sshfs | awk '{ print $2 }' | while read dir; do
echo Unmounting $dir
fusermount -u -z "$dir"
rmdir --ignore-fail-on-non-empty "$dir" &>/dev/null
done
fi
exit
fi
if [[ -z "$host" ]]; then
echo "No host given"
_helpexit
fi
if [[ -d "$valid_name" ]]; then
device1=$( stat -c "%d" "$valid_name" )
device2=$( stat -c "%d" . )
else
device1=valid
device2=valid
fi
if [[ $device1 = $device2 ]]; then
echo "Mounting $valid_name$path in ~/mnt/$valid_name"
cd ~/mnt
mkdir -p "$valid_name"
eval sshfs \
-o reconnect \
-o ServerAliveInterval=45,ServerAliveCountMax=2 \
$NOSAVE \
-o follow_symlinks "$valid_name$path" "$valid_name"
if [[ $? -gt 0 ]]; then
rmdir "$valid_name"
else
df -h "$valid_name"
fi
else
echo "~/mnt/$valid_name is already mounted"
fi