使用 SSH 和 Ruby 连接到交换机
我需要使用 SSH 和 Ruby 连接到 Cisco 交换机.我遇到的问题是身份验证不同.使用 PuTTY,我喜欢这样:输入登录身份"(任何值,无一致性),然后它会要求输入用户名"和密码",如下图所示:
I need to connect to a Cisco Switch with SSH and Ruby. The problem I'm having is that the authentication is different. With PuTTY I do like this: enter the 'login as' (any value, no consistence) then it asks for 'User Name' and 'Password', like this screen:
交换机配置中的SSH User Authentication by Password"选项已禁用,无法启用.
The option "SSH User Authentication by Password" in the switch config is disabled and enabling it is not an option.
我最后的两次尝试:
1 使用网络 SSH
Net::SSH.start(CISCO, USER) do |ssh|
ssh.exec("echo hi")
end
结果:Net::SSH::AuthenticationFailed
Result: Net::SSH::AuthenticationFailed
2 使用 Net-SSH-Telnet
2 Using Net-SSH-Telnet
tn = Net::SSH::Telnet::new("Host" => CISCO,
"Timeout" => 60,
"Prompt" => /^\login as:/ )
tn.cmd("\n") { |c| print c }
tn.cmd("#{USER}\n") { |c| print c }
tn.cmd("#{PASS}\n") { |c| print c }
tn.print("echo oi") { |c| print c }
tn.close
结果:Net::SSH::AuthenticationFailed
Result: Net::SSH::AuthenticationFailed
我想我需要用服务器打开一个通道并输入登录名、用户名和密码作为命令,但每次我都收到 AuthenticationFailed 错误.有什么解决办法吗?
I guess I need to open a channel with the server and enter the login as, username and password as commands, but every time I get the AuthenticationFailed error. Any solution?
您有两个选择:
设置 SSH 密钥:这种方式非常理想,因为它不需要硬编码密码.为此,您需要生成密钥并编辑您的 SSH 配置以进行无密码登录.这是一个示例教程,如何:设置 ssh 密钥
Setup SSH keys: This way is ideal because it doesn't require a hard coded password. To do this you will need to generate the keys and edit your SSH config for password-less log in. Here is an example tutorial, HOWTO: set up ssh keys
硬编码您的密码:您可以通过在 SSH 连接中为您的用户添加密码来实现这一点
Hardcoding your password: You can achieve this by adding the password for your user in your SSH connection
硬编码示例:
Net::SSH.start(CISCO, USER, :password => PASSWORD) do |ssh|
ssh.exec("echo hi")
end