-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseSSH.sh
executable file
·62 lines (36 loc) · 1.92 KB
/
reverseSSH.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
# ReverseSSH first setup
# Run as root
yum install -y autossh
# Prep - Install findopenport on Target host
# & copy Target .pem & .pub keys to $USER/install
# Set user & userdir
USER="me"
TARGET="target.host.net"
# Creating installs folder
mkdir $USER/install
# Log output & errors
exec > /home/$USER/install/setup.log 2>&1
## Create local SSH key
su $USER -c 'cat /dev/zero | ssh-keygen -q -N ""'
## Copy Local host key to Target - presumes the same user is there (change if not)
cat /home/$USER/.ssh/id_rsa.pub | ssh -i /home/$USER/install/$USER.pem $USER@$TARGET "cat >> /home/$USER/.ssh/authorized_keys"
## Copy Target key to Local host
touch /home/$USER/.ssh/authorized_keys
chmod 644 /home/$USER/.ssh/authorized_keys
chown $USER:$USER /home/$USER/.ssh/authorized_keys
cat /home/$USER/install/$USER.pub > /home/$USER/.ssh/authorized_keys
## Find free ports on Target & output to install file
ssh -i /home/$USER/install/$USER.pem $USER@$TARGET 'findopenport 2>/dev/null' >
/home/$USER/install/openportlist.txt
readarray -t remote_port < /home/$USER/install/openportlist.txt
# Set temporary hostname
hostnamectl set-hostname "build-${remote_port[0]}"
## Set host AutoSSH port in user crontab
echo "@reboot nohup /usr/bin/autossh -M 0 -o 'ServerAliveInterval 10' -o 'ServerAliveCountMax 3' -NR ${remote_port[0]}:localhost:22 $USER@$TARGET &" >> /var/spool/cron/crontabs/$USER
## Create local host connection file & copy it to Target - presumes same username (change if needed)
echo "ssh -p ${remote_port[0]} [email protected]" > /home/$USER/install/build-${remote_port[0]}.sh
scp -i /home/$USER/install/$USER.pem /home/$USER/install/build-${remote_port[0]}.sh
$USER@$TARGET:/home/$USER/
## Start Reverse Proxy connection
nohup /usr/bin/autossh -M 0 -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" -NR ${remote_port[0]}:localhost:22 $USER@$TARGET &
# Now on the Target you can connect using the host connection file - eg. ./build-###