# ssh through "expect" in shell script

**URL:** <https://community.unix.com/t/ssh-through-expect-in-shell-script/294954>\
**Category:** Shell Programming and Scripting\
**Created:** [September 8, 2011, 3:57pm UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954 "2011-09-08T15:57:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shekhar2010us](https://community.unix.com/user_avatar/community.unix.com/shekhar2010us/32/7167_2.png) [@shekhar2010us](https://community.unix.com/u/shekhar2010us)\
**Post date:** [September 8, 2011, 3:57pm UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/1 "2011-09-08T15:57:55Z")

</div>

Hi,

I am trying to use "Expect" in shell script to ssh and do some work in remote server but I am unable to connect. Here is the code I am using.

#save as test.sh

```nohighlight
set ip "10.10.10.10"
set username "uname"
set password "upass"
spawn ssh $username@$ip
expect "Password:"
send "$password\r"
#expect "->"
#send "ls\r"

```

Doing above gives me error:  
./test.sh[4]: spawn: not found  
couldn't read file "Password:": no such file or directory  
./test.sh[6]: send: not found

System Configuration:  
"Solaris 5.10"  
expect -v gives "expect version 5.31.2"

Another thing is I cannot find expect in /usr/bin/ or /usr/local/bin/ though "man expect" and "expect -v" is working.

Thanks for help.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 8, 2011, 4:24pm UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/2 "2011-09-08T16:24:56Z")

</div>

If you use ssh keys, you won't need to kludge in insecure stored-plaintext passwords in with a third-party utility unavailable on most machines.

---

<div class="post-metadata">

**Author:** ![shekhar2010us](https://community.unix.com/user_avatar/community.unix.com/shekhar2010us/32/7167_2.png) [@shekhar2010us](https://community.unix.com/u/shekhar2010us)\
**Post date:** [September 8, 2011, 4:33pm UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/3 "2011-09-08T16:33:52Z")

</div>

Thanks Corona, Can you let me know how to use ssh keys. Do you mean RSA keys?

---------- Post updated at 04:33 PM ---------- Previous update was at 04:32 PM ----------

Also, I cannot install OpenSSH in my system as I don't have all permission rights.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [September 8, 2011, 7:55pm UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/4 "2011-09-08T19:55:21Z")

</div>

There are heaps of articles on the web showing how to setup public/private RSA keys for ssh for example [Using SSH keys for Password-less login](http://www.topwebhosts.org/bbs/board.php?bo_table=tech_articles&wr_id=16) .

It dosn't necessarily require Openssh to use them, if your server already has a non-Openssh version of sshd that should be fine.

Once this is working you should be able to do:

```plaintext
$ ssh uname@$ip id

```

Without typing a password.

BTW There are a few reasons why the expect script didn't work:

1. Use `type expect` to see where it is being run from
2. You need `#!/path/to/expect` on the first line of your script - otherwise it will be run by sh not expect
3. ssh will not allow passwords to come from a non tty session like expect - it has been specially coded to disallow this.

---

<div class="post-metadata">

**Author:** ![hfreyer](https://community.unix.com/letter_avatar/hfreyer/32/6_449c533f586939b6e5f246632a46421f.png) [@hfreyer](https://community.unix.com/u/hfreyer)\
**Post date:** [September 9, 2011, 5:00am UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/5 "2011-09-09T05:00:59Z")

</div>

Can't confirm statement 3. of Chubler\_XL. I'm using expect for ssh connections.  
One might think of extending the expect command to handle the ssh login state machine,  
e.g. something like this:

```nohighlight
      expect {
        "continue connecting (yes/no)? " {
          exp_send "yes\r"
          exp_continue
        }
        -nocase "password:" {
          exp_send "$password\r"
          exp_continue
        }
        "Permission denied" {
          puts "\ninvalid user/password\n"
        }
        "->" {
          puts "\nssh login successful\n"
        }
      }

```

---

<div class="post-metadata">

**Author:** ![rohit22hamirpur](https://community.unix.com/letter_avatar/rohit22hamirpur/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit22hamirpur](https://community.unix.com/u/rohit22hamirpur)\
**Post date:** [September 9, 2011, 9:31am UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/6 "2011-09-09T09:31:49Z")

</div>

We can also generate the key through ssh-keygen command and copy that key on that server where you want to ssh.

---

<div class="post-metadata">

**Author:** ![shekhar2010us](https://community.unix.com/user_avatar/community.unix.com/shekhar2010us/32/7167_2.png) [@shekhar2010us](https://community.unix.com/u/shekhar2010us)\
**Post date:** [September 9, 2011, 10:47am UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/7 "2011-09-09T10:47:19Z")

</div>

Thanks Everyone.

I am trying ssh-keygen.

Home addresses after logging into local and remote hosts are:  
Local host: /mm/ums/home/user1/  
Remote host: /home/user2/  
The users are different, may be due to this I am getting error, I don't know.

I did the following steps:  
(1) ssh-keygen -t rsa in local host  
(2) Used default passphrase. id\_rsa and id\_rsa.pub are created in /mm/ums/home/user1/.ssh  
(3) ftp to remote host and copied id\_rsa.pub to /home/user2/.ssh/authorized\_keys with access 600.

Now when I am doing ssh user2@\<remote-host-ip\>, its asking me for password.  
Can someone tell me where I went wrong.

Thanks.

---

<div class="post-metadata">

**Author:** ![ahamed101](https://community.unix.com/user_avatar/community.unix.com/ahamed101/32/1118_2.png) [@ahamed101](https://community.unix.com/u/ahamed101)\
**Post date:** [September 9, 2011, 10:58am UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/8 "2011-09-09T10:58:40Z")

</div>

Try 644 for authorized keys and 600 for .ssh folder  
Check /var/log/auth.info for some information.

Also try ssh in debug mode using -v option

--ahamed

---

<div class="post-metadata">

**Author:** ![shekhar2010us](https://community.unix.com/user_avatar/community.unix.com/shekhar2010us/32/7167_2.png) [@shekhar2010us](https://community.unix.com/u/shekhar2010us)\
**Post date:** [September 9, 2011, 11:17am UTC](https://community.unix.com/t/ssh-through-expect-in-shell-script/294954/9 "2011-09-09T11:17:45Z")

</div>

Updates:

I did a mistake on creating a folder named authorized\_keys instead of appending the content of id\_rsa.pub into authorized\_keys..

Now, ssh is working without entering password. Thanks
