#!/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 # 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, e.g.: sshc mywebserver It is usually expected that the profile name will match the name of the target host (the machine you're SSH-ing to), but this can be overridden by the C element in the profile. Profile files are YAML files that specify, at minimum, a user name and options to pass to the C 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 =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) 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 switch =item command If no command is specified by the profile, then this command will be run after connected to the remote host. B 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 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. =head1 CVS CHANGELOG $Log: sshc.pl,v $ Revision 1.1 2007/09/05 16:45:56 dpmeyer Initial import of unix script project