且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ssh远程服务器登录脚本

更新时间:2023-12-05 13:59:52

您应该看一下Net::SSH的一些示例,它基本上打开了一个连接并为您提供了一个块,可以执行所需的任何命令,例如:

You should look at some of the examples for Net::SSH, it basically opens a connection and gives you a block to execute whatever command you like, e.g.:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
  output = ssh.exec!('ls')
  puts output
end

(摘录自此处)

我什么都没做,但是你明白了.有了这种结构,您就可以在远程服务器上运行任何您喜欢的程序并解析其输出.由于您可能对公钥授权感兴趣,因此应该查看

I haven't done anything with it, but you get the idea. Given this construct you can run whatever program you like on the remote server and parse its output. As you are probably interested in public key authorization you should have a look at the answer to this question, it will show you how to specify a key-file.