Login with Shell Script.

Dear All,

I need to create a shell script which will login to a unix system with user root. I also need to supply the password for root through script only instead of entering it manually.

After i am logged in to the system i need to excute all the necessary commands.

so far i have done this..but it asks for password to be entered.

ssh -l root HOSTNAME/IP
echo "WELCOME to ROOT"
exit

Any help would be appreciated.
Thanks in Advance.:slight_smile:

something like this,

#! /usr/bin/perl

use Expect;

my $script = "ssh -x -l root IPADDRESS ls";

my $command = Expect->spawn($script);

my $pid = $command->pid();
my $password = "password";

$command->log_stdout(1);

my $timeout=7200;
while (
$command->expect($timeout,
                -re => 'want to continue connecting',
                -re => 'password:',
                -re => 'Secure connection to \S+ refused',
                -re => '^OKc',
             ) ){
        $command->clear_accum();
    if( not defined($command->exp_match_number) ){
                        print "Error: output not expected, aborting connection\n";
                        #print "#### String received:\n";
                        print $command->exp_before, "\n";
                        $command->hard_close;
                        $retval = 1;
                        print "Retval ->$retval<-\n";
                        return $retval;
                }

                if( $command->exp_match_number == 1 ){
                        # The authentication was not working, tell ssh "yes"
                        # we want to connect
                        print $command "yes\r";
                        next;
                }
                elsif( $command->exp_match_number == 2 ){
                        # Looking for password
                        print $command "$password\r";
                        next;
                }
                elsif( $command->exp_match_number == 3 ){
            print $command->exp_before, "\n";
                        $retval = 0;
                        last;
                }
                elsif( $command->exp_match_number == 4 ){
                        last;
                }
}
$exp_error = $command->exp_error();

if ( defined ($exp_error) ){
        print "Exp Error:$exp_error:\n";
}

Thanks for the reply..

can you please explain the script in short..

Do you have expect installed .... Also check if you have autoexpect installed

type autoexpect and do what do regularly ... Once done ... edit script.exp to accommodate your requirements.

Automating Tasks with EXPECT

sure.

Expect is a module through which you automate the interactions.
It will spawn and execute the script.

$command->expect($timeout,
                -re => 'want to continue connecting',
                -re => 'password:',
                -re => 'Secure connection to \S+ refused',
                -re => '^OKc',
             ) )

This is just creating object for expect module, in which you have to give list of regular expression in which format it interacts with you to get the input..

Then it spawns and executes command given (ssh command ), and it matches each interaction message with the given regular expression, and sets the variable exp_match_number with corresponding value.

so if exp_match_number has the value 1, then interaction message matches with the
''want to continue connecting'' then you can give your input by writing into the expect object like
print $command "yes\r".

so this provides the data for interactions.

Hope this helps.

ssh shell means secure shell. A password in a plain text script isn't, really. A more secure route is generating keys and using an authorized_keys file containing public keys of trusted users on every system (man ssh).

here is the script i am using.

#!/usr/bin/expect

set timeout 3

if {[llength $argv] == 0} {
  puts "usage: automate_ssh.sh <username> <password> <ip> [command]"
  exit 1
}

set command [lindex $argv 3]
set ip [lindex $argv 2]
set password [lindex $argv 1]
set username [lindex $argv 0]

spawn ssh $username@$ip $command

expect "(yes/no)?" {
send "yes\r"
}

expect "password:" {
send "$password\r"
}

interact

Thanks for this lovely script. it solved my purpose.

can you plzz explain the above script in short.
Thanks.