2015-05-18 40 views
2

我正在迭代IP地址列表并使用SSH登录它们。但是,其中一些没有SSH,而是只有telnet。我该如何判断:如果SSH失败,请重试,但使用telnet?预期脚本 - 如果SSH失败,请使用telnet

我知道SSH连接失败时会输出一条消息“连接被拒绝”,但是当我尝试使用它作为条件时,期望不起作用......在SSH尝试被拒绝后,脚本会中断。

下面的代码片段 - 这是行不通的:

foreach line $MACHINES { 
    spawn ssh -q [email protected]$line 
    expect {   
    -ex "ssh: connect to host $line port 22: Connection refused" { 
     spawn telnet -l [email protected]$line; continue 
    } 
    } 
} 

非常感谢你提前, d

+0

用'希望-d'运行脚本,看看什么是错的。 – pynexj

回答

-1

不要那样做。如果攻击者能够关闭该框上的ssh服务或者该框中间的人,他可以强制未加密的telnet并获取凭据以登录该服务器。

不要试图使用telnet作为ssh的后备。这样做的脚本可以很容易地从远程利用。

+0

为什么downvote?你可以解释吗? – hek2mgl

+0

这不是问题的答案*。这是一条评论。 –

+0

在我看来,这并不适用于安全相关的主题,因为不是每个人都阅读评论。事实上,我应该用粗体字来写这个。 – hek2mgl

0

在您期待之后有超时处理失败的连接。有些系统甚至可能不够友善,拒绝连接。以下是在不通知用户它的ssh产生超时的系统上测试的。

#!/usr/bin/env expect 

# invoke example ./telnet.exp 127.0.0.1 username password 
set IP [lindex $argv 0] 
set User [lindex $argv 1] 
set Password [lindex $argv 2] 

# Set timeout from default 10 seconds 
set timeout 5 
# connect without asking yes/no for known_hosts 
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]$IP 
# I'm not being cheeky, I left out the p/P because I avoid regex where possible 
expect { 
    -nocase "refused" { spawn telnet $IP } 
    timeout { spawn telnet $IP } 
    "assword:" { send "$Password\r" } 
} 
interact 

这是我到你的片段建议的变化(这是未经测试)

foreach line $MACHINES { 
    spawn ssh -q [email protected]$line 
    expect {   
    -ex "ssh: connect to host $line port 22: Connection refused" { 
     spawn telnet -l [email protected]$line; continue 
    } 
    timeout { 
     spawn telnet -l [email protected]$line; continue 
    } 
    } 
} 
0

请原谅我的迟到的反应;我感谢大家的贡献。

我最终成功地使用了这个小块。我相信“等待”命令用于从进程列表中删除SSH进程。

期待{

EOF {等待;产卵的telnet -l $ USER $行}

}