scutil¶ ↑
Description:¶ ↑
Scutil (pronounced “scuttle”) is a small Ruby library that makes using Net::SSH to execute commands on remote systems even more convenient.
It does this in three ways:
First, it defines Scutil.exec_command which abstracts the callbacks and other setup necessary to connect to a system, execute a command, and capture the output and return value of that command. You can roughly think of it as a generic “Capistrano lite” in this regard.
Second, it tracks the connections used on all systems and reuses these connections where ever possible.
Finally, scutil takes away the burden of managing data transfers over PTY connections. It automatically requests PTYs where needed and makes them “binary safe” (this functionality is configurable and can be disabled). PTYs are needed for some curses-based programs and, most importantly in scutil’s context, for sudo. A full discussion on PTYs is beyond the scope of this documentation.
The “automatic” part of PTY requests comes from a regex in Scutil.exec_command. Basically, if sudo is at the start of the command to be executed, scutil will request a PTY. This regex is configurable through :scutil_pty_regex
. You can force a PTY request by specifying :scutil_force_pty
in the various options arguments.
All of this syntactic sugar can be used as a simple class method with Scutil.exec_command, as an instantiable class with Scutil::Exec, or as a mixin with the module Scutil.
You can now you scutil to manage you Net::SCP connections as well. Two wrapper functions will be defined if you have Net::SCP installed.
See Scutil::Exec for more details.
Scutil works great with Rails to perform tasks via SSH.
Particularly when used with Delayed::Job, Resque et al.
Synopsis:¶ ↑
You can use scutil in a few different ways, for more usage examples see Scutil.
require 'scutil' # Class method executed immediately: Scutil.exec_command('servername1', 'username', 'command', nil, { :keys => '~mas/.ssh/id_rsa', :scutil_verbose => true }) # Object to be used and reused: exec = Scutil::Exec.new('servername2', 'mas') exec.exec_command("ls -l /") return_value = exec.exec_command("grep -q ...") # Tar up /usr/src/linux on the remote machine and write it to # /var/tmp/linux.tar.gz on the local machine. Hostname, error # message, and return value in the exception: begin exec.exec_command('sudo tar -C /usr/src -czf - linux', '/var/tmp/linux.tar.gz') rescue Scutil::Error => err puts "Message: " + err.message puts "Hostname: " + err.hostname puts "Exit status: #{err.command_exit_status}" end # Capture command output in a string: require 'stringio' command_output = StringIO.new Scutil.exec_command('servername1', 'sudo cat /root/secrets.txt', command_output, nil, { :password => 'myPassw0rd' }) puts command_output.string
Installation:¶ ↑
gem install scutil
License:¶ ↑
The MIT License (MIT)
Copyright © 2013 by Marc Soda
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.