-
Notifications
You must be signed in to change notification settings - Fork 0
/
autossh.sh
66 lines (58 loc) · 1.83 KB
/
autossh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
#Enter default device details here
#These are used if no flags are supplied
username="[default user login]"
mac="[default device MAC address]"
while getopts ":m:l:n" opt; do
case $opt in
m)
if [[ $OPTARG =~ ^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$ ]]; then
echo "Valid MAC."
mac=$OPTARG
else
echo "Error: Invalid MAC Address."
echo -e "Usage:\n [-m (Mac Address) | specify Mac address] [-l (login) | specify login] [-n |Nmap scan]"
exit 1
fi
;;
l)
username=$OPTARG
;;
n)
if sudo -n true 2>/dev/null; then
echo -e "Scanning for IP via Nmap.\nPlease wait..."
else
echo "Scanning for IP via Nmap requires root, please enter your password:"
sudo -v
echo "Please wait..."
fi
localip=$(hostname -I | awk '{print $1}' | grep -E -o "([0-9]{1,3}[\.]){2}[0-9]{1,3}.")"*"
ip=$(sudo nmap -sP -n $localip | grep -i -B 2 7c:dd:90:b1:00:83 | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}+$ ]]; then
echo "Successfully found IP via Nmap scan."
ssh="ssh "$ip" -l "$username
echo "Connecting to" $ip "as" $username"."
eval $ssh
else
echo "Error: Failed to find host, may be offline."
exit 3
fi
exit 0
;;
\?)
echo "Invalid option: -$OPTARG"
echo -e "Usage:\n [-m (Mac Address) | specify Mac address] [-l (login) | specify login] [-n |Nmap scan]"
exit 2
;;
esac
done
ip=$(ip neighbour | grep $mac | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Successfully found IP."
ssh="ssh "$ip" -l "$username
echo "Connecting to" $ip "as" $username"."
eval $ssh
else
echo -e "Error: Failed to find host, may be offline."
exit 3
fi