CrossingTheRoad2020
CrossingTheRoad2020

Reputation: 61

Auto SSH with OpenWRT Router to AWS EC2 Server

I've got a OpenWRT router that I'm trying to make a persistent reverse ssh tunnel to an Amazon AWS server. The issue is my ISP changes my public IP so in order to ssh to it, I have to use port knocking to prevent every IP from seeing the ssh port. I've written a bash script to make a connection. And no, I don't want to use autossh...it doesn't support input key path.

What works:

Anyway this script is located at /root/scripts/autosshtoaws.sh. I can run it fine from the terminal with the command ./root/scripts/autosshtoaws.sh. It goes in the background just fine. If I manually disconnect my internet line, sshd on the AWS server is configured to kill the socket AND my router is configured to kill the socket. It attempts like it's suppose to.

The problem:

I've got this running as an init.d service. It starts up well after network does. When I reboot the router and I look at the netstat command, it shows multiple attempts as if it is trying over and over and over.

Here is the script:

#!/bin/bash
PATH=/usr/sbin:/sbin:/usr/bin:/bin

ssh_command="/usr/bin/netcat -z MYMACHINE.amazonaws.com 777 611 501; sleep 2; /usr/bin/ssh -i /root/.ssh/aws_key.pem -R 8080:192.168.0.99:7070 [email protected]"

while true; do
    if [[ -z $(ps | grep "$ssh_command" | sed '$ d') ]]
    then eval $ssh_command
    else sleep 60
    fi
done

This is what the netstat command shows:

tcp        0      0 1.1.1.1:54926       2.2.2.2:22          ESTABLISHED
tcp        0      0 1.1.1.1:54312       2.2.2.2:22          TIME_WAIT
tcp        0      0 1.1.1.1:54760       2.2.2.2:22          TIME_WAIT
tcp        0      0 1.1.1.1:54700       2.2.2.2:22          TIME_WAIT
tcp        0      0 1.1.1.1:54636       2.2.2.2:22          TIME_WAIT

Here is the service configuration:

#!/bin/sh /etc/rc.common
USE_PROCD=1
START=95
STOP=01
start_service() {
    procd_open_instance
    procd_set_param command /bin/sh "/root/scripts/autostartsshtoaws.sh"
    procd_close_instance
}

Upvotes: 0

Views: 568

Answers (0)

Related Questions