Quantcast
Channel: sleeplessbeastie's notes
Viewing all articles
Browse latest Browse all 770

How to terminate active ssh sessions

$
0
0

Several months ago I have shortly described how to close non-responsive ssh session, which comes handy at times. Today I will describe how to close every active ssh session by inspecting existing pseudoterminals.

The idea is quite simple as it can be described just in several simple steps.

List process IDs using pseudoterminals
lsof -w -t +d /dev/pts/
Sort generated list
sort
Remove duplicated entries
uniq
List parent process IDs
xargs ps -oppid= -p
Extend list by process name and owner
xargs ps -ocomm=,pid=,user= -p
Filter sshd processes
awk '($1 == "sshd") {print $2}'
Terminate filtered processes
xargs kill

Use following command to close every active ssh session.

# lsof -w -t +d /dev/pts/ | sort | uniq | \
  xargs ps -oppid= -p | \
  xargs ps -ocomm=,pid=,user= -p | \
  awk '($1 == "sshd") {print $2}' | \
  xargs kill

Use following command to close every active ssh session owned by specified username.

# lsof -w -t +d /dev/pts/ | sort | uniq | \
  xargs ps -oppid= -p | \
  xargs ps -ocomm=,pid=,user= -p | \
  awk '($1 == "sshd"&& $3 == "USERNAME") {print $2}' | \
  xargs kill

It is quite simple solution that can be used to identify and close parent processes based on defined criteria.


Viewing all articles
Browse latest Browse all 770

Trending Articles