Visit Our UNIX and Linux User Community


Login with Shell Script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Login with Shell Script.
# 1  
Old 09-24-2009
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.Smilie
# 2  
Old 09-24-2009
something like this,
Code:
#! /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";
}

# 3  
Old 09-24-2009
Thanks for the reply..

can you please explain the script in short..
# 4  
Old 09-24-2009
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
# 5  
Old 09-24-2009
sure.

Expect is a module through which you automate the interactions.
It will spawn and execute the script.
Code:
$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.
# 6  
Old 09-24-2009
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).
# 7  
Old 09-24-2009
login script using expect

here is the script i am using.

Code:
#!/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


Previous Thread | Next Thread
Test Your Knowledge in Computers #463
Difficulty: Medium
chrony is an implementation of the Network Time Protocol (NTP) and is a replacement for the ntpd.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for login user and email

Guys please help me I have a linux class and I want to write a shell script who shows which user loged in and show the process that are active in his/her shell in another text file and email that file to root just when the user loged out Thanks every bod (1 Reply)
Discussion started by: hamedk1122
1 Replies

2. Shell Programming and Scripting

Script as login shell (passing args to login shell)

Hello all, for security reasons my compagny imposes that my script be launch remotly via ssh under the users login shell. So serverA launches the ssh command to serverB which has a local user with my script as a login shell. Local script works like a charm on his own. serverB$ grep... (20 Replies)
Discussion started by: maverick72
20 Replies

3. Shell Programming and Scripting

How to login as a different user inside a shell script?

hi, i want to login as a different user inside a shell script and then call another shell script from that script. how to do that? original script : script_A.sh so when the script_A.sh is called , i want to login as a different user and then call another shell script(script_B.sh) from... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

Shell script for user login information.

Hi Gurus, I need help in writing a script which should say which user has used or logged in in the server from past one month using FTP or TELNET and the output should be of the form Username Service NumberofTimes Date. Thanks in Advance. ---------- Post updated at 04:01 PM... (1 Reply)
Discussion started by: rama krishna
1 Replies

5. Shell Programming and Scripting

Automate CVS login using shell script

Hi, Can anyone pls help me to automate login to cvs. I basically want to login to cvs and update a file. the script always gets to the login and returns the prompt for a password. Is there any way to send the password in the script itself. Here is the script: #!/bin/ksh... (0 Replies)
Discussion started by: raghu_shekar
0 Replies

6. Shell Programming and Scripting

Help with Unix bash shell script login

Hi, I am a complete Unix novice and need some help with creating a login shell script. I have created a file with user details i.e. PIN, name etc and require help in recalling the specified details from the file and being prompted for a password on login. Any help would be very much appreciated.... (0 Replies)
Discussion started by: tdsrogers
0 Replies

7. UNIX for Dummies Questions & Answers

How to authenticate sybase login thru shell script ?

Hi All, This is my first post. Need your favour guys. I need to authenticate syabse login/password thru shell script. I am getting the ID & Pwd form user and storing it in variable.. But how to authenticate and echo user back if the id/pwd doesnt work. isql -U$1 -P$2 -S$3 ?? thanks in... (3 Replies)
Discussion started by: libin4u2000
3 Replies

8. Shell Programming and Scripting

login from a shell script?????

Any help on this ..... its a bit urgent !!!! Hi Can anybody provide info about the following??? i want to issue su (switch user) command from within a shell script how to take the password without user intervention from the shell script only???? i.e using apssword which is already... (2 Replies)
Discussion started by: skyineyes
2 Replies

9. Shell Programming and Scripting

remote-login via Shell-Script

Hello all, I would like to login from one unix-system with a (tcsh)-script to an other unix-system.The login-procedure and the transmission of username, resp. password runs via ssh. The problem is after logging onto the remote server once "Enter" has to be pressed, before one gets to the... (1 Reply)
Discussion started by: Juergen Paepke
1 Replies

10. UNIX for Dummies Questions & Answers

remote login via shell script

is it possible for me to have a shell script log me in to a telnet session? i have in mind something along the lines of % telnet host < script, where the first two lines of script will be username and pass followed by a list of commands to execute on the remote host. this doesn t work as... (4 Replies)
Discussion started by: lethe
4 Replies

Featured Tech Videos