[英]Connect to a Switch with SSH and Ruby
I need to connect to a Cisco Switch with SSH and Ruby. 我需要使用SSH和Ruby连接到Cisco交换机。 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:
使用PuTTY我喜欢这样:输入'login as'(任何值,没有一致性)然后它会询问'User Name'和'Password',就像这个屏幕:
The option "SSH User Authentication by Password" in the switch config is disabled and enabling it is not an option. 禁用交换机配置中的“通过密码进行SSH用户身份验证”选项,并且无法启用它。
My two last tries: 我最近的两次尝试:
1 Using Net-SSH 1使用Net-SSH
Net::SSH.start(CISCO, USER) do |ssh|
ssh.exec("echo hi")
end
Result: Net::SSH::AuthenticationFailed 结果:Net :: SSH :: AuthenticationFailed
2 Using Net-SSH-Telnet 2使用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
Result: Net::SSH::AuthenticationFailed 结果:Net :: SSH :: 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. 我想我需要在服务器上打开一个频道并输入登录名,用户名和密码作为命令,但每次我收到AuthenticationFailed错误。 Any solution?
有解决方案吗
You have two choices: 你有两个选择:
Setup SSH keys : This way is ideal because it doesn't require a hard coded password. 设置SSH密钥 :这种方式非常理想,因为它不需要硬编码密码。 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配置以进行无密码登录。以下是一个示例教程, HOWTO:设置ssh密钥
Hardcoding your password : You can achieve this by adding the password for your user in your SSH connection 硬编码密码 :您可以通过在SSH连接中为用户添加密码来实现此目的
Hardcoding example: 硬编码示例:
Net::SSH.start(CISCO, USER, :password => PASSWORD) do |ssh|
ssh.exec("echo hi")
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.