Perl script implementation limits SSH maximum logon count (white list support) _perl

Source: Internet
Author: User
Tags ssh perl script

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)

Related Article

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.