Topic
No replies
mysceen_dw
mysceen_dw
1 Post
ACCEPTED ANSWER

Pinned topic setup ssh for multiple servers and multiple users

‏2012-09-25T15:22:10Z |
Hi
I have 3 machines and access to them using 3 users. I'm trying to write an expect script to setup ssh on all these machines for all these users, so that:
machine_a access machine_a, machine_b and machine_c with root, user_1 and user_2
and the same for all other machines.
I wrote this Perl script but only the 1st iteration of the loop is working. Can someone help me out here? Thank you in advance

  1. ***************** Script starts here ************
$text = $text = "rm -r ~/.ssh
ssh-keygen
";
open( FILE, ">keyg.sh" );
print FILE $text;
close( FILE );

$text = "#!/usr/bin/expect -f
set timeout -1
spawn \"$whereAreWe/keyg.sh\"
expect \"Enter file in which to save the key\"
send \"\\r\"
expect \"empty for no passphrase\"
send \"\\r\"
expect \"Enter same passphrase again\"
send \"\\r\"
expect eof
";

open( FILE, ">init_expect_keyg.sh" );
print FILE $text;
close( FILE );

system( "chmod 700 init_expect_keyg.sh keyg.sh" );
  1. Generate ssh keys
system( "./init_expect_keyg.sh" );

@servers = ( $d, $w, $s );
@users = ( "root", $user1, $user2 );
@users_pwd = ( $root_pwd, $user1_pwd, $user2_pwd );

for ( $i=0; $i < 3; $i++ )
{
for ( $j=0; $j < 3; $j++ )
{
if ( $current_server eq @servers$i ) # To avoid deleting .ssh folder
{
$text = "cat ~/.ssh/id_rsa.pub | ssh " . @users$j . "\@" . @servers$i . " 'cat >> .ssh/authorized_keys'\n";
}
else
{
$text = "cat ~/.ssh/id_rsa.pub | ssh " . @users$j . "\@" . @servers$i . " 'rm -r ~/.ssh; mkdir ~/.ssh; cat >> .ssh/authorized_keys'\n";
}
open( FILE, ">set_ssh_1_line.sh" );
print FILE $text;
close( FILE );
$text = "#!/usr/bin/expect -f
set timeout -1
spawn \"$whereAreWe/set_ssh_1_line.sh\"
expect \"continue connecting (yes/no)\"
send \"yes\\r\"
expect \"" . @users$j . "\@" . @servers$i . "\'s password:\"
send \"" . @users_pwd$j . "\\r\"
expect eof
";
open( FILE, ">init_expect.sh" );
print FILE $text;
close( FILE );

system( "chmod 700 set_ssh_1_line.sh init_expect.sh" );
system( "./init_expect.sh" );

#unlink( "init_expect.sh" );
#unlink( "set_ssh_1_line.sh" );
}
}