我编写了一个期望脚本,该脚本非常适合在我们的网络上测试密码,直到它在我的列表中循环了很多次,然后调试输出显示没有任何内容可以匹配。
这是一些调试,显示缓冲区中有数据,然后下一个生成没有数据:
ssh: connect to host 12.23.34.56 port 22: No route to host
expect: does "ssh: connect to host 12.23.34.56 port 22: No route to host\r\r\n" (spawn_id exp69) match glob pattern "(yes/no)? "? no
"assword: "? no
"]# "? no
"oute to host"? yes
expect: set expect_out(0,string) "oute to host"
expect: set expect_out(spawn_id) "exp69"
expect: set expect_out(buffer) "ssh: connect to host 12.23.34.56 port 22: No route to host"
spawn ssh -o PreferredAuthentications=password -o NumberOfPasswordPrompts=3 [email protected]
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {26418}
expect: does "" (spawn_id exp70) match glob pattern "(yes/no)? "? no
"assword: "? no
"]# "? no
"oute to host"? no
"onnection refused"? no
"isconnected from"? no
expect: timed out
exp69 之后的每个 spawn_id 在 does "" match 部分中都没有任何内容。
我认为这与缓冲区有某种关系,但我已经尝试过:
match_max 200000
match_max 600000
这似乎没有什么区别。我已经删除了真实的 ip 并将其更改为 xxxx 我实际上并没有测试 xxxx (但 12.23.34.56 确实滑入了我的服务器列表中进行检查)
该脚本本身正在运行 Expect 并循环访问另一个名为“servers.txt”的文件,并尝试逐行在该服务器上执行一系列命令。它记录哪些有效,哪些无效。这是脚本中的内容:
#!/usr/bin/expect
# where to log info, open "writable", this is our homegrown log
# unlike the others that follow
set logfile [open "passcheck-results" "w"]
# clobber and log
log_file -noappend passcheck-logfile
# disable user viewing of process as it happens
# 1=on, 0=off
# disables screen output only, recommended 0
log_user 1
# enable verbose debugging, from expect
# 1=on, 0=off
# useful for debugging the script itself, recommended: 0
exp_internal -f debug.log 0
# default waits for 10s, and if no response, kills process
# too low and machines may not respond fast enough
# in particular real timeouts are not registered if too low
# set to -1 for infinite, real timeouts can take 60s or more
# instead of waiting 60s for EVERY failure, set this lower
# recommend the 10s default
set timeout 10
# if you do not get all the response, you expect, increase buffer
match_max 600000
# get by argv functions instead
# set nohistory save on CLI/bash
set passwords { password1 password2 password3 }
# open the list of servers to process
# warning, no error checking on file open
set input [open "servers.txt" "r"]
# loop over the list of servers with prompt logic
foreach ip [split [read $input] "\n"] {
# slowing it down a bit
sleep 2
# had to change =password to get results I wanted
# loop over line of servers
spawn ssh -o PreferredAuthentications=password \
-o NumberOfPasswordPrompts=[llength $passwords] admin@$ip
# verify where exactly to reset this count
set try 0
# account for other possibilities
expect {
"(yes/no)? " {
# new host detected
sleep 1
send "yes\r"
exp_continue
}
"assword: " {
if { $try >= [llength $passwords] } {
puts $logfile "Bad_Passwords $ip"
#send_error ">>> wrong passwords\n"
exit 1
}
sleep 1
send [lindex $passwords $try]\r
incr try
exp_continue
}
"\]\# " {
puts $logfile "succeeded_$try $ip"
sleep 1
send "exit\r"
}
"oute to host" {
puts $logfile "No_Route_to_Host $ip"
}
"onnection refused" {
puts $logfile "Refused $ip"
}
"isconnected from" {
puts $logfile "Disconnected $ip"
}
timeout {
puts $logfile "timed_out_or_fail $ip"
}
}
}
答案1
您可能用完了文件描述符。您可能需要在每次生成时调用 close ,这样您就不会耗尽资源。
答案2
你是对的,exp_internal -f passcheck-debug.log 1 向我指出了没有足够 pty 的问题。我补充道:
close -i $spawn_id
wait -nowait
并持续到那时之后。我必须添加 -nowait 因为如果它没有 spawn_id 它将永远等待。