# ssh login using script

**URL:** <https://community.unix.com/t/ssh-login-using-script/288369>\
**Category:** Red Hat\
**Created:** [May 20, 2011, 2:07am UTC](https://community.unix.com/t/ssh-login-using-script/288369 "2011-05-20T02:07:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaincv](https://community.unix.com/letter_avatar/ajaincv/32/6_22734026db12803ebb3e059622ac3534.png) [@ajaincv](https://community.unix.com/u/ajaincv)\
**Post date:** [May 20, 2011, 2:07am UTC](https://community.unix.com/t/ssh-login-using-script/288369/1 "2011-05-20T02:07:14Z")

</div>

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.

```nohighlight
#!/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

---

<div class="post-metadata">

**Author:** ![rrstone](https://community.unix.com/letter_avatar/rrstone/32/6_22734026db12803ebb3e059622ac3534.png) [@rrstone](https://community.unix.com/u/rrstone)\
**Post date:** [May 30, 2011, 10:13am UTC](https://community.unix.com/t/ssh-login-using-script/288369/2 "2011-05-30T10:13:37Z")

</div>

I would suggest to use keys authentication , instead of fighting expect scripts .

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_22734026db12803ebb3e059622ac3534.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [May 30, 2011, 10:25am UTC](https://community.unix.com/t/ssh-login-using-script/288369/3 "2011-05-30T10:25:36Z")

</div>

Very good set of examples on setting up and using ssh keys

[ssh-keygen: password-less SSH login](http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html)
