ssh through "expect" in shell script

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

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.

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.

1 Like

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.

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 .

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:

$ 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.
1 Like

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:

      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"
        }
      }
1 Like

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

1 Like

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.

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

1 Like

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

1 Like