SSHkit is a tool that is part of Capistrano which is a remote deployment tool written in ruby.
This article is aimed at providing a brief overview of SSHkit. To work with SSHkit we first need to install capistrano.
[root@devbox ~]# yum install ruby
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
ruby x86_64 2.0.0.598-25.el7_1 base 67 k
Installing for dependencies:
ruby-irb noarch 2.0.0.598-25.el7_1 base 88 k
ruby-libs x86_64 2.0.0.598-25.el7_1 base 2.8 M
rubygem-bigdecimal x86_64 1.2.0-25.el7_1 base 79 k
rubygem-io-console x86_64 0.4.2-25.el7_1 base 50 k
rubygem-json x86_64 1.7.7-25.el7_1 base 75 k
rubygem-psych x86_64 2.0.0-25.el7_1 base 77 k
rubygem-rdoc noarch 4.0.0-25.el7_1 base 318 k
rubygems noarch 2.0.14-25.el7_1 base 212 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package (+8 Dependent packages)
Total download size: 3.7 M
Installed size: 13 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): ruby-irb-2.0.0.598-25.el7_1.noarch.rpm | 88 kB 00:00:01
(2/9): rubygem-json-1.7.7-25.el7_1.x86_64.rpm | 75 kB 00:00:00
(3/9): rubygem-psych-2.0.0-25.el7_1.x86_64.rpm | 77 kB 00:00:00
(4/9): rubygem-rdoc-4.0.0-25.el7_1.noarch.rpm | 318 kB 00:00:01
(5/9): rubygem-io-console-0.4.2-25.el7_1.x86_64.rpm | 50 kB 00:00:03
(6/9): rubygems-2.0.14-25.el7_1.noarch.rpm | 212 kB 00:00:00
(7/9): rubygem-bigdecimal-1.2.0-25.el7_1.x86_64.rpm | 79 kB 00:00:11
(8/9): ruby-2.0.0.598-25.el7_1.x86_64.rpm | 67 kB 00:00:16
(9/9): ruby-libs-2.0.0.598-25.el7_1.x86_64.rpm | 2.8 MB 00:00:19
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 193 kB/s | 3.7 MB 00:00:19
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ruby-libs-2.0.0.598-25.el7_1.x86_64 1/9
Installing : rubygem-psych-2.0.0-25.el7_1.x86_64 2/9
Installing : rubygem-bigdecimal-1.2.0-25.el7_1.x86_64 3/9
Installing : rubygem-io-console-0.4.2-25.el7_1.x86_64 4/9
Installing : rubygem-json-1.7.7-25.el7_1.x86_64 5/9
Installing : rubygem-rdoc-4.0.0-25.el7_1.noarch 6/9
Installing : ruby-irb-2.0.0.598-25.el7_1.noarch 7/9
Installing : ruby-2.0.0.598-25.el7_1.x86_64 8/9
Installing : rubygems-2.0.14-25.el7_1.noarch 9/9
Verifying : rubygems-2.0.14-25.el7_1.noarch 1/9
Verifying : rubygem-rdoc-4.0.0-25.el7_1.noarch 2/9
Verifying : rubygem-psych-2.0.0-25.el7_1.x86_64 3/9
Verifying : rubygem-bigdecimal-1.2.0-25.el7_1.x86_64 4/9
Verifying : rubygem-io-console-0.4.2-25.el7_1.x86_64 5/9
Verifying : rubygem-json-1.7.7-25.el7_1.x86_64 6/9
Verifying : ruby-libs-2.0.0.598-25.el7_1.x86_64 7/9
Verifying : ruby-2.0.0.598-25.el7_1.x86_64 8/9
Verifying : ruby-irb-2.0.0.598-25.el7_1.noarch 9/9
Installed:
ruby.x86_64 0:2.0.0.598-25.el7_1
Dependency Installed:
ruby-irb.noarch 0:2.0.0.598-25.el7_1 ruby-libs.x86_64 0:2.0.0.598-25.el7_1 rubygem-bigdecimal.x86_64 0:1.2.0-25.el7_1 rubygem-io-console.x86_64 0:0.4.2-25.el7_1
rubygem-json.x86_64 0:1.7.7-25.el7_1 rubygem-psych.x86_64 0:2.0.0-25.el7_1 rubygem-rdoc.noarch 0:4.0.0-25.el7_1 rubygems.noarch 0:2.0.14-25.el7_1
Complete!
[root@devbox ~]# gem install capistrano
Fetching: net-ssh-3.2.0.gem (100%)
Successfully installed net-ssh-3.2.0
Fetching: net-scp-1.2.1.gem (100%)
Successfully installed net-scp-1.2.1
Fetching: sshkit-1.11.2.gem (100%)
Successfully installed sshkit-1.11.2
Fetching: airbrussh-1.1.0.gem (100%)
Successfully installed airbrussh-1.1.0
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: rake-11.2.2.gem (100%)
Successfully installed rake-11.2.2
Fetching: capistrano-harrow-0.5.3.gem (100%)
___ _ ___ ___ ___ _____ ___ _ _ _ ___
/ __| /_\ | _ \_ _/ __|_ _| _ \ /_\ | \| |/ _ \
| (__ / _ \| _/| |\__ \ | | | / / _ \| .` | (_) |
\___/_/ \_\_| |___|___/ |_| |_|_\/_/ \_\_|\_|\___/
Learn about our web-based collaboration and
automation platform for Capistrano: hrw.io/auto-cap
Successfully installed capistrano-harrow-0.5.3
Fetching: capistrano-3.6.1.gem (100%)
Successfully installed capistrano-3.6.1
Parsing documentation for net-ssh-3.2.0
Installing ri documentation for net-ssh-3.2.0
Parsing documentation for net-scp-1.2.1
Installing ri documentation for net-scp-1.2.1
Parsing documentation for sshkit-1.11.2
Installing ri documentation for sshkit-1.11.2
Parsing documentation for airbrussh-1.1.0
Installing ri documentation for airbrussh-1.1.0
Parsing documentation for i18n-0.7.0
Installing ri documentation for i18n-0.7.0
Parsing documentation for rake-11.2.2
Installing ri documentation for rake-11.2.2
Parsing documentation for capistrano-harrow-0.5.3
Installing ri documentation for capistrano-harrow-0.5.3
Parsing documentation for capistrano-3.6.1
Installing ri documentation for capistrano-3.6.1
8 gems installed
[root@devbox ~]#
Capistrano is a powerful automation tool but here I'll be providing a brief overview of SSHkit only. You can refer to further documentation on capistrano here.
SSHKit provides certain methods to allow for instructions/commands to be executed on remote machines.
These methods are as follows:
execute(): the workhorse that runs the commands on your server
upload(): uploads a file from your local computer to your remote server
capture(): executes a command and returns its output as a string
puts(): writes the output returned by capture() to the screen
background(): runs a command in the background
test(): can be used for control flow since it works like the test command-line utility in Unix and returns false if its expression exits with a non-zero value
Example 1: Use SSHkit to get listing of files & directories in current working directory:
#!/usr/bin/ruby
require 'sshkit'
require 'sshkit/dsl'
include SSHKit::DSL
on '192.168.44.137' do
puts capture(:ls, '-l')
end
Example 2: Run commands on multiple servers:
#!/usr/bin/ruby
require 'sshkit'
require 'sshkit/dsl'
include SSHKit::DSL
on %w{192.168.44.135 192.168.44.137} do |host|
puts capture(:uptime)
puts capture(:uname, '-a')
end
Example 3: Run a command as a different user:
#!/usr/bin/ruby
require 'sshkit'
require 'sshkit/dsl'
include SSHKit::DSL
on '192.168.44.137' do |host|
as 'james' do
puts capture(:whoami)
puts capture(:who)
end
end
Conclusion:
This article barely touches the surface when it comes to the full capabilities provided by capistrano. I'll definitely try to post more detailed articles exploring further functionality in the future.
No comments:
Post a Comment