在ruby下使用ssh方式连接linux服务器
1 require 'net/ssh'
2 require 'net/sftp'
3
4 host = hostip
5 username = username
6 password = password
7
8 #~ ##显示文件和系统版本
9 server_cmd1 = 'ls -l'
10 server_cmd2 = 'cat /etc/issue'
11
12 # 需要安装以下组件
13 #gem install net-ssh
14 #gem install net-sftp
15
16
17 Net::SSH.start(host, username, :password => password) do |ssh|
18 puts ssh.exec!(server_cmd1)
19 puts ssh.exec!(server_cmd2)
20
21
22 ssh.sftp.connect do |sftp|
23 Dir.foreach('.') do |file|
24 puts file
25 end
26 end
27 end
类似文章:http://www.infoq.com/cn/articles/ruby-file-upload-ssh-intro