当提示输入shost密码到localhost时,如何将回车添加到bash?

时间:2017-09-08 16:39:06

标签: bash

我是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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

注意:虽然expect位于自己的软件包中,expect已经在我的SLES基础安装中了......不知道RHEL是否也是如此...... ?

看看这个answer on how to automate SSH with a password

我认为您可能会重新使用该帖子中的expect脚本来模拟单个失败的登录信息,例如:

  • pass设置为虚假价值或根本不设置
  • 如果您未设置pass,请删除send -- "$pass\r"条款
  • 如果远程系统重新提示X次输入密码,则提供expect/send个命令的多个副本(一些额外的操作会产生一些expect相关错误,但仍会导致ssh登录失败)< / LI>

对于我的一个远程主机,在我返回命令提示符之前,我提示输入密码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中登录失败的登录信息。