我是新手Bash,并负责脚本检查合规过程。
来自bash(或者如果Python更好),我需要从运行脚本的主机中脚本脚本SSH连接。
例如:SSH -L testaccount localhost
,但我需要运行52次,以便它被IPS捕获。
运行此字符串时,我会提示我输入密码,我必须点击Enter才能完成脚本。
是否有一种方法可以包括密码或运输返回以充当手动干预,以便我不必每次输入输入?
这是我能够工作的样本,但仅测序了30次尝试:
#!/bin/bash
i=0
while [$i -lt 52]
do
echo | ssh -l testaccount localhost&
i=$[$i+1]
done
fail2ban配置和良好实践:
//count how password as root failed
cat /var/log/secure.1 | grep "Failed password for root" --count
//check the list for analyst
cat /var/log/secure.1 | grep "Failed password for root"
//setting fail2ban copy for local configuration
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
//open the configuration file and edit some secu
sudo nano /etc/fail2ban/jail.local
maxfailures = 5 //here you have to set to 56 as you said
bantime = 600 //here time during second try login
ignoreip = 192.168.0.0/16 //add the lan and ip online static
[mail]
enabled = false //true if you want to alert you IP blacklisted banned ...
//log traffic
cat /var/log/fail2ban.log
[ssh]
//network protocole protection & supervision
enabled = true
port = ssh,sftp
filter = sshd
logpath = /var/log/auth.log
maxretry = 6
//enable fail2ban
systemctl enable fail2ban
//start fail2ban
systemctl start fail2ban
注意:虽然 expect
有其自己的软件包,但 expect
已经在我的SLES碱基安装中...不知道Rhel是否也是如此...?/p>
请查看如何使用密码自动化SSH的答案。
我认为您可能可以从该帖子中重新使用expect
脚本来模拟单个失败的登录,例如:
- 将
pass
设置为伪装值,或者根本不设置 - 如果您不设置
pass
,请删除send -- "$passr"
子句 - 如果远程系统重新提交了密码的X时间,则提供
expect/send
命令的多个副本(一些附加功能将生成一些expect
相关错误,但仍会导致失败的SSH登录)
)
对于我的一个遥控器主机之一,我提示3次输入密码,然后再返回命令提示。
我对以下测试进行了搅拌:
$ cat sshtest
#!/usr/bin/expect -f
set pass xyz
set server myremotehost
set name bob
spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "r"
expect "*?assword:*"
send -- "r"
expect "*?assword:*"
send -- "r"
expect "*?assword:*"
send -- "r"
expect "*?assword:*"
send -- "r"
interact
和运行脚本的结果:
$ sshtest
spawn ssh bob@myremotehost
Password:
Password:
Password:
Permission denied (publickey,keyboard-interactive).
send: spawn id exp4 not open
while executing
"send -- "r""
(file "sshtest" line 16)
如果您没有足够的 expect/send
对,则将用Password:
提示符将其搁浅,因此我添加了一些额外的expect/send
对,进而生成了输出的最后4行。[我不使用expect
,所以可能有更优雅的方法来做到这一点... ymmv。]
显然,您的主要脚本可以调用此脚本并在后台进行呼叫,并使用输出(>/dev/null 2>&1
??)
我还在远程主机上验证了失败的登录名已在/var/log/warn
和/var/log/messages
中记录。