Created
June 29, 2011 14:37
-
-
Save sugar84/1053955 to your computer and use it in GitHub Desktop.
Create aliase for access remote servers by pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use common::sense; | |
use YAML::Tiny; | |
use Data::Dumper; | |
my $conf = YAML::Tiny->read("config.yml")->[0]; | |
my $template = 'alias ssh_<name>=\'expect -c "spawn ssh <user>@<host>;' . | |
'expect password: {send <passw>\r}; interact"\''; | |
my $output_file = "ssh_expect.sh"; | |
my $header = "#!/bin/sh"; | |
my $comp_templ = compile_template( $template ); | |
my @content; | |
for my $key (keys %$conf) { | |
my $data = $conf->{$key}; | |
$data->{'name'} = $key; | |
push @content, do_template( $comp_templ, $data ); | |
} | |
open my $fh, ">", $output_file | |
or die "cannot open output_file! $!"; | |
local $, = "\n"; | |
print $fh $header, @content; | |
sub do_template { | |
my ($template, $data) = @_; | |
my ($first, $str, $key); | |
my @keys = @{$template->{'keys'}}; | |
for my $piece (@{$template->{'pieces'}}) { | |
if (!$first) { | |
$str .= $piece; | |
$first++; | |
} | |
else { | |
$key = shift @keys; | |
$str .= $data->{$key} . $piece; | |
} | |
} | |
return $str; | |
} | |
sub compile_template { | |
my $template = shift; | |
my @pieces = split /<\w+>/, $template; | |
my @keys = $template =~ /<(\w+)>/g; | |
return {pieces => \@pieces, keys => \@keys}; | |
} | |
__END__ | |
$ cat config.yml | |
server1: | |
host: "server1.ru" | |
user: "user1" | |
passw: "pass1" | |
server2: | |
host: "server2.ru" | |
user: "user2" | |
passw: "pass2" | |
server3: | |
host: "server3.ru" | |
user: "user3" | |
passw: "pass3" | |
$ cat ssh_expect.sh | |
#!/bin/sh | |
alias ssh_server3='expect -c "spawn ssh [email protected];expect password: {send pass3\r}; interact"' | |
alias ssh_server2='expect -c "spawn ssh [email protected];expect password: {send pass2\r}; interact"' | |
alias ssh_server1='expect -c "spawn ssh [email protected];expect password: {send pass1\r}; interact"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment