rabin 5 years ago
parent
commit
24d614950e
3 changed files with 35 additions and 0 deletions
  1. 12 0
      src/shell/autossh/daemon
  2. 20 0
      src/shell/autossh/expect
  3. 3 0
      src/shell/autossh/start

+ 12 - 0
src/shell/autossh/daemon

@@ -0,0 +1,12 @@
+#!/usr/bin/env sh
+set -e
+while true
+do
+	pids=`ps aux|grep "/usr/bin/ssh -f -NR"|grep -v entrypoint|grep -v grep|grep -v process|awk '{print $1}'`
+	if [ "$pids" ]; then
+	    sleep 60
+	else
+	    /data/dm/src/shell/autossh/expect $1 root $2 $3
+	    echo "ssh started"
+	fi
+done

+ 20 - 0
src/shell/autossh/expect

@@ -0,0 +1,20 @@
+#!/usr/bin/expect --
+#!/bin/sh
+set port [lrange $argv 0 0]
+set username [lrange $argv 1 1]
+set remoteHost [lrange $argv 2 2]
+set password [lrange $argv 3 3]
+
+spawn /usr/bin/ssh -f -NR "*:$port:localhost:22" $username@$remoteHost
+match_max 100000;
+set timeout 60;
+expect {
+    "?sh: Error*"
+        { puts "CONNECTION_ERROR"; exit; }
+    "*yes/no*"
+        { send "yes\r"; exp_continue; }
+    "*?assword:*" {
+         send "$password\r"; set timeout 4;
+         expect "*?assword:*" { puts "WRONG_PASSWORD"; exit; }
+    }
+}

+ 3 - 0
src/shell/autossh/start

@@ -0,0 +1,3 @@
+#!/usr/bin/env sh
+set -e
+/data/dm/src/shell/autossh/daemon $1 $2 $3 >/dev/null &