Compare commits
2 Commits
58c51672ca
...
200e4fef6c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
200e4fef6c | ||
|
|
15caed95c1 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.*journal
|
||||||
|
.*lastused
|
||||||
95
mutex_access.sh
Executable file
95
mutex_access.sh
Executable file
@ -0,0 +1,95 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
port=22 # default SSH port
|
||||||
|
cache=".maccess-lastused"
|
||||||
|
journal=".maccess-journal"
|
||||||
|
user_host=""
|
||||||
|
user="default"
|
||||||
|
jump=""
|
||||||
|
lock_file="lock.mutex"
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
echo "Usage: $0 [-p PORT -J jump1,jump2] user@host"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse command line options
|
||||||
|
while getopts ":p:J:u:" opt; do
|
||||||
|
case $opt in
|
||||||
|
p)
|
||||||
|
if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -gt 65535 ]; then
|
||||||
|
echo "Invalid port number: $OPTARG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
port="$OPTARG"
|
||||||
|
;;
|
||||||
|
J)
|
||||||
|
jump="-J $OPTARG"
|
||||||
|
echo "ProxyJump: $jump"
|
||||||
|
;;
|
||||||
|
u)
|
||||||
|
user="$OPTARG"
|
||||||
|
;;
|
||||||
|
\?)
|
||||||
|
echo "Invalid option: -$OPTARG"
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
:)
|
||||||
|
echo "Option -$OPTARG requires an argument"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND -1))
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "Using $cache"
|
||||||
|
|
||||||
|
if [ -f "$cache" ]; then
|
||||||
|
{
|
||||||
|
read -r jump_cached
|
||||||
|
read -r port_cached
|
||||||
|
read -r user_host_cached
|
||||||
|
} < "$cache"
|
||||||
|
jump="$jump_cached"
|
||||||
|
port="$port_cached"
|
||||||
|
user_host="$user_host_cached"
|
||||||
|
else
|
||||||
|
echo "Error: Cache file $cache not found." >&2
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
user_host="$1"
|
||||||
|
echo "$jump" > "$cache"
|
||||||
|
echo "$port" >> "$cache"
|
||||||
|
echo "$user_host" >> "$cache"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Check if lock file exists on remote
|
||||||
|
if ssh $jump -p "$port" "$user_host" "test -e '$lock_file'"; then
|
||||||
|
echo "Error: Lock file $lock_file exists on $user_host" >&2
|
||||||
|
whoisusing=$(ssh $jump -p $port $user_host "cat $lock_file")
|
||||||
|
echo "At the moment in use by $whoisusing" >&2
|
||||||
|
echo "Session might be active. Exiting." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create lock file
|
||||||
|
ssh $jump -p "$port" "$user_host" "echo '$user' > '$lock_file'"
|
||||||
|
echo "Locked for user $user"
|
||||||
|
echo "[Start] [$(date +%Y-%m-%d-%H-%M-%S)] $user" >> $journal
|
||||||
|
cleanup() {
|
||||||
|
echo "Removing lock file..."
|
||||||
|
ssh $jump -p "$port" "$user_host" "rm -f '$lock_file'"
|
||||||
|
echo "[End] [$(date +%Y-%m-%d-%H-%M-%S)] $user" >> $journal
|
||||||
|
}
|
||||||
|
|
||||||
|
# Register cleanup trap
|
||||||
|
trap cleanup EXIT INT TERM
|
||||||
|
|
||||||
|
# Start interactive session
|
||||||
|
echo "Starting SSH session (port $port). Lock file created."
|
||||||
|
ssh $jump -p "$port" "$user_host"
|
||||||
|
|
||||||
16
tun.sh
16
tun.sh
@ -4,7 +4,7 @@ version=0.1
|
|||||||
idx=0
|
idx=0
|
||||||
killFlag=0
|
killFlag=0
|
||||||
endpoint="192.168.105.100"
|
endpoint="192.168.105.100"
|
||||||
user="vptyp"
|
user="$USER"
|
||||||
cacheFile=.tunLast
|
cacheFile=.tunLast
|
||||||
privateKeyLoc="~/.ssh/id_ecdsa"
|
privateKeyLoc="~/.ssh/id_ecdsa"
|
||||||
override=0
|
override=0
|
||||||
@ -46,9 +46,19 @@ function usage {
|
|||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function about {
|
||||||
|
echo "
|
||||||
|
${scriptName} ver.${version}
|
||||||
|
Setup with next settings:
|
||||||
|
started up under user: ${user}
|
||||||
|
endpoint: ${endpoint}
|
||||||
|
cacheFile: ${cacheFile}
|
||||||
|
"
|
||||||
|
}
|
||||||
|
|
||||||
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
|
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
|
||||||
-V | --version )
|
-a | --about )
|
||||||
echo "$version"
|
about
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
-k | --kill )
|
-k | --kill )
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user