ssh单点登录限制脚本(支持白名单 可自定义最大登录数)
时间:2016-06-05 01:26 来源:linux.it.net.cn 作者:IT
网上搜出来3个文章,有2个都设置密码,我就想不通,就这么一个脚本设置密码有什么意思?现共享出来给大家
ssh limit perl脚本主要作用:
1.限制一个ssh用户的最大登录数为n,n可自定义.
2.支持白名单,如root、test登录不受限制.
如果一个ssh用户的最大登录数超过指定数字,则后登录的会把先前登录的踢掉,以此达到控制登录数的目的.该脚本需要主机支持perl,如果没有,需要yum安装.
脚本内容:
cat /root/soft_shell/limit.pl
01
#!/usr/bin/perl -w
02
use strict;
03
#white list
04
my @ALLOW_USERS = qw{
05
test
06
root
07
rocdk890
08
};
09
#the maximum number of ssh login
10
my $LOGIN_TIMES = 1;
11
sub main
12
{
13
my @lines = `ps -eo user,pid,etime,cmd | grep sshd`;
14
my $users;
15
for my $line (@lines) {
16
if(my ($user, $pid, $etime, $cmd) = $line =~ /^([^\s]+)\s+(\d+)\s+([^\s]+)\s+(sshd:.+)$/) {
17
next if grep {$user eq $_} @ALLOW_USERS;
18
my $proc = {'pid', $pid, 'etime', $etime, 'cmd', $cmd};
19
push @{$users->{$user}}, $proc;
20
}
21
}
22
for my $key(keys(%$users)) {
23
my @sshs = sort {
24
my ($lb, $la) = (length($b->{'etime'}), length($a->{'etime'}));
25
if($lb == $la) {
26
$b->{'etime'} cmp $a->{'etime'};
27
} else {
28
$lb <=> $la;
29
}
30
} @{$users->{$key}};
31
$LOGIN_TIMES = 1 if $LOGIN_TIMES < 1;
32
for (1 .. $LOGIN_TIMES) { pop @sshs; };
33
for my $ssh (@sshs) {
34
kill 9, $ssh->{'pid'};
35
}
36
}
37
}
38
while(1) {
39
main;
40
sleep 3;
41
}
加入开机启动:
echo "nohup /root/soft_shell/limit.pl > /dev/null 2>&1" >> /etc/rc.local
后台运行:
nohup /root/soft_shell/limit.pl > /dev/null 2>&1
(责任编辑:IT)
网上搜出来3个文章,有2个都设置密码,我就想不通,就这么一个脚本设置密码有什么意思?现共享出来给大家 ssh limit perl脚本主要作用: 1.限制一个ssh用户的最大登录数为n,n可自定义. 2.支持白名单,如root、test登录不受限制. 如果一个ssh用户的最大登录数超过指定数字,则后登录的会把先前登录的踢掉,以此达到控制登录数的目的.该脚本需要主机支持perl,如果没有,需要yum安装. 脚本内容: cat /root/soft_shell/limit.pl
加入开机启动: echo "nohup /root/soft_shell/limit.pl > /dev/null 2>&1" >> /etc/rc.local 后台运行: nohup /root/soft_shell/limit.pl > /dev/null 2>&1 (责任编辑:IT) |