forked from rake74/bash_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsud
45 lines (39 loc) · 1.1 KB
/
sud
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
#!/bin/bash
yourDomain='example.com'
case "$1" in
'' ) echo "Usage: $(basename $0) [hostname|ip]" && exit 1 ;;
* ) node="$1"
esac
DNScheck () {
[ "x$1" = 'x' ] && return 1
host -t A "$1" | \
grep -q -e 'has address' -e 'domain name pointer'
}
portOpenCheck () {
[ "x$1" = 'x' ] && return 1
timeout 1 bash -c "</dev/tcp/$1/22" &> /dev/null && return 0 || return 1
}
# checkif looks like IP - if not, see if we can resolve
ipregex='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
if [[ $node =~ ^$ipregex\.$ipregex\.$ipregex\.$ipregex$ ]]; then
DNScheck $node || "will use $node address, but no reverse DNS is setup"
else
if [[ "$node" == *$yourDomain ]] ; then
if ! DNScheck $node ; then
echo "unable to resolve $node" && exit 1
fi
else
if DNScheck $node.$yourDomain ; then
node=$node.$yourDomain
else
if ! DNScheck $node ; then
echo "unable to resolve $node" && exit 1
fi
fi
fi
fi
while ! portOpenCheck $node ; do
echo "$(date +'%F %T') | Waiting for $node 22 to become available..."
sleep 2
done
ssh -t $node 'cat /etc/motd 2>/dev/null ; sudo -i'