限制SSH单用户登录

四月 9, 2010 by · Leave a Comment 

功能: 实现root之外的用户只能登录一个ssh
使用方法: 用root权限在后台(比如screen里)运行这个脚本

#!/usr/bin/perl -w
use strict;
sub main
{
    my @lines = `ps -eo user,pid,etime,cmd | grep sshd`;
    my $users;
    for my $line (@lines) {
        if(my ($user, $pid, $etime, $cmd) = $line =~ /^([^s]+)s+(d+)s+([^s]+)s+(sshd:.+)$/) {
            next if($user eq ‘root’);
            my $proc = {‘pid’, $pid, ‘etime’, $etime, ‘cmd’, $cmd};
            push @{$users->{$user}}, $proc;
        }
    }
    for my $key(keys(%$users)) {
         my @sshs = sort {
            my ($lb, $la) = (length($b->{‘etime’}), length($a->{‘etime’}));
            if($lb == $la) {
                $b->{‘etime’} cmp $a->{‘etime’};
            } else {
                $lb <=> $la;
            }
        } @{$users->{$key}};
        for (1 .. 1) { shift @sshs; };
        for my $ (@sshs) {
            kill 9, $ssh->{‘pid’};
        }
    }
}
while(1) {
    main;
    sleep 3;
}

About admin

注意:

1、本站启用了审核机制,你的留言可能稍后才会显示,请不要重复提交,谢谢。
2、留言时的头像是Gravatar提供的服务。想设置的看这里
3、评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。

To submit your comment, click the image below where it asks you to...
Clickcha - The One-Click Captcha