Background: When you use ssh to manage servers, unexpected network disconnection and disconnection often occur. When the network is disconnected, the terminal is disconnected, if there is a program running on the front end or the current operation is very important and cannot be terminated at this time, the consequences may be very serious. At that time, you would like to cry, maybe some people say you can use nohup, good, however, in many cases, we do not know when the network is disconnected or when the network is disconnected. Even if we know nohup, it cannot be used effectively, and it is troublesome to switch back and forth between the front and back ends. Therefore, we found a way to achieve one-time configuration (or transparent to users) and benefit for life-screen.
Purpose: reliably recover abnormal and interrupted ssh sessions
Key points: Enable the virtual terminal with the powerful screen in linux, and work with the user's profile file once. After the user logs on to ssh, the system automatically prompts whether there is an abnormal interrupted session, if yes, you are prompted to select the session to be restored. If no, You can directly enter the new session. All operations are transparent to end users. If you combine the bastion host later, it will be more perfect.
Configuration process:
1. Install screen (omitted)
2. Add the following to the end of the profile file of a specific user (you can also use the global profile)
[Root @ admin200 ~] # More/home/chroot_test/. bash_profile
# Add by lai
Start_time = $ (date + % Y % m month % d % H: % M: % S)
Ssh_session_name = laijingli _ $ start_time
# Query suspended sessions
/Usr/bin/screen-ls |/bin/grep Detached>/dev/null
If ["$? "=" 0 "]; then
# Display suspended sessions
/Usr/bin/screen-ls |/bin/grep Detached
Echo-ne "\ 033 [32m \ 033 [01m you have an abnormal disconnected ssh session connection. Please enter the session number to be restored, or enter new to enable the new session: \ 033 [0 m"
# Judge Input
Read input_session
Input_check = '/usr/bin/screen-ls |/bin/grep Detached | awk' {print $1 "" $2}' |/bin/grep ^ $ input_session | wc -l'
# Echo input_chcek: $ input_check
Until ["$ input_session" = "new"] | ["$ input_check" = "1"]; do
Echo-en "\ 033 [31m \ 033 [01 m Please enter the session number or no: \ 033 [0 m"
Read input_session
Input_check = '/usr/bin/screen-ls |/bin/grep Detached | awk' {print $1 "" $2}' |/bin/grep ^ $ input_session | wc -l'
Done
# Enable or resume a new session
If ["$ input_session" = "new"]; then
Echo is enabling new session
Exec/usr/bin/screen-S $ ssh_session_name
Else
Echo is recovering the session: '/usr/bin/screen-ls |/bin/grep Detached | awk' {print $1 "" $2}' |/bin/grep ^ $ input_session'
Exec/usr/bin/screen-r $ input_session
Fi
Else
Echo 2 new: You have not abnormally disconnected the ssh session connection. The new session will be enabled.
Exec/usr/bin/screen-S $ ssh_session_name
Fi
3. Effect