当前位置:   article > 正文

python telnet登录发送命令_Telnet发送命令,然后读取响应

python通过telnet发行命令

我发现,如果不向cmd方法提供块,它将返回响应(假设telnet没有向您请求其他任何东西)。您可以一次发送所有命令(但将所有响应捆绑在一起)或执行多个调用,但必须执行嵌套块回调(否则我无法执行)。在require 'net/telnet'

class Client

# Fetch weather forecast for NYC.

#

# @return [String]

def response

fetch_all_in_one_response

# fetch_multiple_responses

ensure

disconnect

end

private

# Do all the commands at once and return everything on one go.

#

# @return [String]

def fetch_all_in_one_response

client.cmd("\nNYC\nX\n")

end

# Do multiple calls to retrieve the final forecast.

#

# @return [String]

def fetch_multiple_responses

client.cmd("\r") do

client.cmd("NYC\r") do

client.cmd("X\r") do |forecast|

return forecast

end

end

end

end

# Connect to remote server.

#

# @return [Net::Telnet]

def client

@client ||= Net::Telnet.new(

'Host' => 'rainmaker.wunderground.com',

'Timeout' => false,

'Output_log' => File.open('output.log', 'w')

)

end

# Close connection to the remote server.

def disconnect

client.close

end

end

forecast = Client.new.response

puts forecast

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/418621
推荐阅读
相关标签
  

闽ICP备14008679号