Ruby 脚本 - Telnet 在登录期间挂起

标签 ruby scripting telnet

我正在尝试使用 ruby​​ 进行一些基本脚本编写,通过 telnet 登录到 Windows 计算机,并使用 dos 命令行 ftp 拉取一些文件。当我手动执行此操作时,一切都会顺利进行,但是当我通过 ruby​​ 尝试时,我在登录调用中收到错误。

这是我的完整测试程序:

require 'net/telnet'
tn = Net::Telnet::new("Host"=>"xxx.xxx.xxx.xxx", "Timeout"=>25, "Output_log"=>"output_log.log", "Dump_log"=> "dump_log.log", "Prompt"=>"C:.*>")
tn.login("administrator", "xxxxxxx") {}
tn.cmd('dir')
exit

output_log 的内容不会泄露任何错误:

Trying 208.10.202.187...
Connected to 208.10.202.187.
Welcome to Microsoft Telnet Service 
login: administrator
password: 
*===============================================================
Welcome to Microsoft Telnet Server.
*===============================================================
C:\Documents and Settings\Administrator>

dump_log 也是如此,它具有非常相似但格式不正确的内容。当我运行该程序时,它会停留一段时间,然后输出以下错误:

PS C:\code\tools\deployment> ruby test.rb
C:/Ruby/lib/ruby/1.8/net/telnet.rb:551:in `waitfor': timed out while waiting for more data (Timeout::Error)
        from C:/Ruby/lib/ruby/1.8/net/telnet.rb:685:in `cmd'
        from C:/Ruby/lib/ruby/1.8/net/telnet.rb:730:in `login'
        from test.rb:3

这让我怀疑 telnet 类无法识别命令提示符。我已经为 Prompt 参数尝试了几种不同的正则表达式字符串,包括默认值,但似乎没有任何帮助。

最佳答案

我认为提示字段需要一个正则表达式,而不是一个字符串 尝试一下

tn = Net::Telnet::new("Host"=>"xxx.xxx.xxx.xxx", "Timeout"=>25,
"Output_log"=>"output_log.log", "Dump_log"=> "dump_log.log",
"Prompt"=> /C:.*>/)

关于Ruby 脚本 - Telnet 在登录期间挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966820/

相关文章:

ruby-on-rails - 如何在Rails中创建连接表记录,有很多: through relationship

ruby - || 的多个 case/when 条件

scripting - 在 IIS 应用程序池中获取工作进程

mysql - 将 .sql 查询保存为 .sql 文本文件

Ruby:#map 对于 bang 方法通常没有意义,是吗?

ruby - 树顶语法无限循环

regex - Bash 正则表达式在句子中查找特定单词

javascript - Nodejs telnet登录无法输入密码

linux - 如何通过 ssh 或 telnet 将终端大小更改发送到命令行应用程序?

amazon-web-services - 如何连接到远程 hashcorpVault 服务器