ssh login using script

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat ssh login using script
# 1  
Old 05-20-2011
ssh login using script

Hello All,

I am trying to login on server using ssh script & expect.Login to server successful but after login when i hit enter it gets hanged & when pressing CTRL +C it logs me out from the server.
Scripts are as below.

Code:
#!/bin/bash

FILE=login.txt

CONNECT=sshlogin.exp

SERVERNAME=$1

myserver=""

myuser=""

mypassword=""

exec 3<&0

exec 0<$FILE

while read line

do

MyServer=$(echo $line | cut -d'|' -f1)
        MyUser=$(echo $line | cut -d'|' -f2)
        MyPassword=$(echo $line | cut -d'|' -f3)
        if [ "$SERVERNAME" == "$MyServer" ];
        then
           echo "Running ssh $MyUser@$MyServer..."
          $CONNECT $MyPassword $MyServer $MyUser
        fi
done
#exec 0<&3
#echo "$SERVERNAME not found in login.txt file"
###############


#!/usr/bin/expect -f
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set username [lrange $argv 2 2]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $username@$ipaddr
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof


Kindly help me out in this.

Regards

Ankit

Moderator's Comments:
Mod Comment Please start using [CODE] tags for source listings, console output, ...

Last edited by pludi; 05-20-2011 at 03:21 AM..
# 2  
Old 05-30-2011
I would suggest to use keys authentication , instead of fighting expect scripts .
# 3  
Old 05-30-2011
Very good set of examples on setting up and using ssh keys

ssh-keygen: password-less SSH login
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automate OTPW login to ssh via bash script

Hello everyone. I'm a Linux novice trying out a lot of bash scripting lately, as it is so very addictive. Lately I have been setting up one of my boxes remotely and have been hardening it as much as possible. Please allow me to explain the scenario, as it does tend to become a little... (1 Reply)
Discussion started by: instro
1 Replies

2. Shell Programming and Scripting

Expect script ssh passwordless login

how can i use an expect script to do a passwordless ssh login and run a script on a bunch of remote hosts? I do not want to use public key authentication as this would require me to setup the keys on the clients etc.... (2 Replies)
Discussion started by: tdubb123
2 Replies

3. HP-UX

telnet login successful ,but ssh can not login

why I can login by telnet using root account but when i use login by ssh using root account it is not successful ,is it different password i am sure ssh service is started (2 Replies)
Discussion started by: alert0919
2 Replies

4. Shell Programming and Scripting

Need help on ssh login script to cisco ios

I'm trying to write a login script to ssh into a cisco switch that will run some command remotely. Similar to this expect script located here: SSH login expect shell script to supply username and password However, that script does not work with cisco ios. Anyway know what the best way to... (1 Reply)
Discussion started by: streetfighter2
1 Replies

5. Shell Programming and Scripting

SSH login with user name and script.

I want to login to server using ssh or telnet and execute one command then exit to the shell Please let me know how to write script for this? (1 Reply)
Discussion started by: svenkatareddy
1 Replies

6. Shell Programming and Scripting

bash ssh login script

hello anyone have done ssh login script without "expect" (automatic login from host A / user b to Host B / user b without enter any passwords)? cheers (2 Replies)
Discussion started by: modcan
2 Replies

7. Shell Programming and Scripting

bash script for ssh login-

hi. I need a bash script which can login to an other mashin via SSH and then run some commands and then return the result to my mashine. I dont know where to begin, I think first I will need a ssh connection, dont know how to make it, then , do I need a ftp connection between the 2 mashins to... (5 Replies)
Discussion started by: big_pil
5 Replies

8. UNIX for Advanced & Expert Users

Can we launch a shell script automatically upon ssh login?

Greetings all, I'll just like to know if it is possible to launch a shell script automatically upon a user's successful login into ssh from a remote host, without adding a command parameter to the ssh command... ie. after keying in ssh username@host (not ssh username@host "command") and upon... (1 Reply)
Discussion started by: rockysfr
1 Replies

9. UNIX for Advanced & Expert Users

Use of sudoer with ssh login shell script (KSH)

Greetings all, I'm in the midst of writing a login component for a series of shell scripts. What my login script does is this: 1. Prompt for username and read in username 2. Prompt for destination host and read in destination host 3. run ssh username and destination host 4. After user keys... (0 Replies)
Discussion started by: rockysfr
0 Replies

10. Shell Programming and Scripting

How can we ssh login with script?

Hi all, Normally, we give ip address to login with ssh like ssh 172.168.0.1 And we can give login name to ssh like ssh -l root 172.10.0.21 Then the shell asks the password, we enter the password and login to the system. While using the script file, we are not able to supply the password to... (5 Replies)
Discussion started by: pcsaji
5 Replies
Login or Register to Ask a Question