The main function of the SSH limit perl script is:
1. Limiting the maximum number of logons for an SSH user is n,n customizable.
2. Support White list, such as root, test login is not restricted.
If an SSH user's maximum number of logons exceeds a specified number, then the login will kick off the previous login to control the number of logins.
The script requires the host to support Perl, and if not, install it yum.
Script Source:
#!/usr/bin/perl-w use strict;
#white list My @ALLOW_USERS = qw{test root LULU1};
#the Maximum number of SSH login my $LOGIN _times = 1;
Sub Main {My @lines = ' Ps-eo user,pid,etime,cmd | grep sshd ';
My $users;
For my $line (@lines) {if ($user, $pid, $etime, $cmd) = $line =~/^ ([^\s]+) \s+ (\d+) \s+ ([^\s]+) \s+ (sshd:.+) $/) {
Next if grep {$user eq $_} @ALLOW_USERS;
My $proc = {' pid ', $pid, ' etime ', $etime, ' cmd ', $cmd};
Push @{$users->{$user}}, $proc; } for my $key (% $users) {My @sshs = sort {I ($lb, $la) = (Length ($b->{' etime '}), Length ($a ;
{' ETime '}));
if ($lb = = $la) {$b->{' etime '} cmp $a->{' etime '};
else {$lb <=> $la;
} @{$users->{$key}};
$LOGIN _times = 1 if $LOGIN _times < 1;
for (1.. $LOGIN _times) {pop @sshs;};
For my $ssh (@sshs) {Kill 9, $ssh->{' pid '};
}} while (1) {main;
Sleep 3;
}
"Use Method"
The save script is saved to the root directory, named limit.pl, and then executes:
echo "/root/limit.pl &" >>/etc/rc.d/rc.local (Join boot)
/root/limit.pl & (Run script)