Example of monitoring SSH running reverse forward

Source: Internet
Author: User
Tags egrep

Run it as another user. For example, the user name is "user"

#! /Bin/KSh
Su-user-S/bin/KSh "/home/ssh_monitor.sh $ *"

 

Ssh_monitor.sh:

#! /Bin/KSh

###### Config ########
Logfile = "./ssh_admin.log"
Known_hosts = "/home/user/. Ssh/known_hosts"
Ssh_pid = '0'
Break_out = 'n'

State_key_string = 'established'
Hasuserconnected = 0

########## SSH forward ##########
Ssh_port = '22'
Remote_host = '192. 168.1.142'
Remote_http_port = '123'
User_name = 'user'
Total_seconds = 600
############ End ################

Isclientexit ()
{
# Echo "Enter isclientexit"

# Is the tunnel up? Perform Two tests:

#1. Check for relevant process ($ command)
# Pgrep-F-X "$ command">/dev/null 2> & 1 | $ command

#2. Test Tunnel by looking at "netstat" output on $ remote_host

Echo "ssh-p $ ssh_port $ user_name @ $ remote_host netstat-an | egrep \" TCP. *: $ remote_http_port. * $ state_key_string \ "| WC-l"

Num = $ (ssh-p $ ssh_port $ user_name @ $ remote_host netstat-an | egrep "TCP. *: $ remote_http_port. * $ state_key_string" | WC-l )\

>/Dev/null 2> & 1

If (hasuserconnected = 1 & num = 0 ))
Then
Echo "client has exited." >>$ logfile
Break_out = 'y'

Elif (num! = 0 & hasuserconnected = 0 ))
Then

Echo "client has connected." >>$ logfile
Hasuserconnected = 1

Fi
}

Trap_exit ()
{
Print "Enter trap_exit" >>$ logfile
# Post_event_script
Break_out = 'y'
}

Post_event_script ()
{
Print "Enter post_event_script"> $ logfile
# Ps-Ef | grep "ssh" | grep-V grep | awk '{print $2}' | read kill_pids
# Echo $ kill_pids
Echo "Kill-9 $ {ssh_pid}" >>$ logfile
Kill-9 "$ ssh_pid"
Ssh-keygen-r $ remote_host
Return 0
}

################
# Start of main #
################

Trap 'trap _ exit '1 2 3 15

[[! -S $ logfile] & touch $ logfile
[[! -S $ known_hosts] & touch $ known_hosts

Echo "###################################### ########### "> $ logfile
Echo "ssh tunnel start." >>$ logfile
Echo "Wait..." >>$ logfile

######### Process paramter ############
Echo $ *
While getopts ": P: U: R: O:" Arg # The colon following the option indicates that parameters are required for this option.
Do
Case $ ARG in
P)
Remote_http_port = $ optarg
;;
U)
User_name = $ optarg
;;
R)
Remote_host = $ optarg
;;

O)
Total_seconds = $ optarg
;;
?) # When there are unrecognized options, What Is Arg?
Echo "unkonw argument"
Exit 1
;;
Esac
Done

Echo "++"
Echo $ remote_host
Echo $ user_name
Echo $ remote_http_port
Echo $ total_seconds
Echo "++"

Echo $ Shell

# Ssh-P 22003 user@127.0.0.1
# Echo "ssh-r 2200:127. 0.0.1: 22-N user@192.168.1.142"
# $ Command is the command used to create the reverse SSH Tunnel
Command = "ssh-o" \ '"stricthostkeychecking no" \' "-p $ ssh_port-Q-n-r $ remote_host: $ remote_http_port: 127.0.0.1: $ ssh_port $ user_name @ $ remote_host"
Echo $ command
Ksh "$ command" | & amp &#????? 'Stricthostkeychecking no' has no method to make it legal.
# Ssh-O 'stricthostkeychecking no'-p $ ssh_port-Q-n-r $ remote_host: $ remote_http_port: 127.0.0.1: $ ssh_port $ user_name @ $ remote_host &
Ssh_pid = $!
Echo "ssh PID: $ {ssh_pid}" >>$ logfile

Until (total_seconds = 0 ))
Do
Isclientexit
Echo $ break_out
If [[$ break_out = 'y']
Then
Break
Fi

(Total_seconds = total_seconds-1 ))
Sleep 1
# Echo "after sleep 1 s"
# Echo "$ total_seconds"
Done

# Kill all SSH and-R;
Post_event_script

Exit 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.