I've recently made a move into the UNIX management side of things at $WORK. It's a mess: we have hundreds of UNIX machines, my user name is different on a lot of them, many require various different options to ssh to work properly, etc.
So, I whipped up a little tool today that lets me store the profiles of remote hosts in ~/.sshc/ using YAML format. Then, I can connect as simply as typing:
sshc myhost
And the proper username and parameters will be loaded from the profile. Yes, I could just use
PuTTY on Windows, but I needed something scriptable (from other apps) and that would work on non-Win machines and from within Cygwin.
It's MIT licensed, but if you do find it useful, I'd appreciate knowing about it. ;-)
Update: before making use of this, be sure to read afresh1's excellent comments regarding OpenSSH's built-in equivalents. The script below is only really useful if you (like me) have a non-OpenSSH-based commercial client.
#!/usr/bin/perl
package main;
use strict; use warnings;
use vars qw[$VERSION $RELEASE];
#=====================================================================
+=========
# $Id: sshc.pl,v 1.1 2007/09/05 16:45:56 dpmeyer Exp $
# Stores configs for SSH-ing to various servers
#---------------------------------------------------------------------
+---------
# Created 09/05/07 10:34:19 CDT by Darren Meyer <darren.meyer@gmail.co
+m>
# See POD after __END__ for Usage and Changelog
#---------------------------------------------------------------------
+---------
# NOTES: this is released under an MIT license, see POD for details
#=====================================================================
+=========
BEGIN {
## Version will be based on release+CVS version number
$RELEASE=0;
$VERSION=sprintf '%d.%d.%03d',$RELEASE,q$Revision: 1.1 $=~/(\d+)\.
+(\d+)/;
}
use YAML::Syck qw[LoadFile];
use File::Spec::Functions qw[catdir catfile];
use Getopt::Long;
my $config_path=catdir($ENV{HOME},'.sshc');
my $hostname = shift @ARGV;
GetOptions( 'config-file|f=s' => \$config_path );
die "Idiot: you need to supply a hostname to connect to\n"
unless defined $hostname && length $hostname;
Connect();
sub Connect {
my $config_file = catfile($config_path,$hostname);
die "I don't have a config file for the host '$hostname'\n"
unless -f $config_file;
my $config = LoadFile($config_file);
system(
'ssh',
@{$config->{options}},
$config->{user}.'@'.(defined $config->{host} ? $config->{host}
+ : $hostname),
(defined $config->{command} ? $config->{command} : @ARGV),
);
print STDERR "Exited with $?\n";
}
__END__
=head1 NAME
sshc.pl - Connect to an SSH server based on a YAML profile
=head1 DESCRIPTION
Connects to a host-profile specified in ~/.sshc/I<profile-name>, e.g.:
sshc mywebserver
It is usually expected that the profile name will match the name of th
+e
target host (the machine you're SSH-ing to), but this can be overridde
+n
by the C<host:> element in the profile.
Profile files are YAML files that specify, at minimum, a user name and
options to pass to the C<ssh> command:
---
user: myusername
options: []
The profile can also specify a command to run upon connection, and a
hostname to use (which defaults to the name of the profile file):
---
user: myusername
options:
- '-2'
command: 'uname -a'
host: areallylong.fqdn.com
See YAML documentation at http://yaml.org for details on constructing
+a valid
YAML document.
Commands to run, if not provided in the profile, will be taken from C<
+@ARGV>.
See L</USAGE>
=head1 USAGE
sshc profilename [-f config-file-dir] [-- command]
=over 8
=item profilename
The name of a file in ~/.sshc (or whichever directory is specified by
C<config-file-dir>) from which to read the connection data. This will
usually be the hostname, and if a host is not specified in the file,
sshc will assume that it is the hostname.
=item config-file-dir
The directory in which to look for profile files. By default, this is
~/.sshc, but this can be overridden using the C<-f> command-line switc
+h
=item command
If no command is specified by the profile, then this command will be
run after connected to the remote host. B<NOTE:> the double-dash
(C<-->) before the command is required if the command will contain
any dash (C<->) characters; the author recommends that the double-dash
always be used when specifying a command.
=back
=head1 LICENSE
The MIT License
Copyright (c) 2007 Darren Meyer <darren.meyer@gmail.com>
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 s
+ell
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 include
+d in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRES
+S OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT
+Y,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHAL
+L THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISIN
+G FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN
THE SOFTWARE.
=head1 CVS CHANGELOG
$Log: sshc.pl,v $
Revision 1.1 2007/09/05 16:45:56 dpmeyer
Initial import of unix script project
<–radiant.matrix–>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet