Visit Jeremy's Blog.
Go Back > Blogs > mathew_p_a
User Name


Rate this Entry

expect script vs authorized keys login

Posted 10-09-2009 at 09:28 AM by mathew_p_a

Most of the time, a linux script writer must have encountered reasons to automate the scripts that require login to the server, copying of data without the need of giving a password at the shell prompt. Most of the time, people will settle down with setting up the secure key access. ie, using authorized keys. But there is also another way of automating login to the servers. It is by using the expect script.

If expect is installed on your machine you can see it at /usr/bin/expect. If not, just install it using Yum

[root@centos ~]# yum install expect
Loading “fastestmirror” plugin
Loading mirror speeds from cached hostfile
* base:
* updates:
* addons:
* extras:
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
–> Running transaction check
—> Package expect.i386 0:5.43.0-5.1 set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

Package Arch Version Repository Size
expect i386 5.43.0-5.1 base 158 k

Transaction Summary
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)

Total download size: 158 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): expect-5.43.0-5.1. 100% |=========================| 158 kB 00:03
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: expect ######################### [1/1]

Installed: expect.i386 0:5.43.0-5.1

Simple! Now go ahead and put the script as follows

#!/usr/bin/expect -f

set timeout -1
spawn ssh -l mathew
expect “mathew\@’s password:”
send — “mathew\r”
Here , mathew is the username and is the server to which you want to login. I will explain the working of expect script in detail

set timeout -1
How much time the script have to wait. “-1″ means wait indefinitely since we are expecting to login to the server. If you want to use the script for some other purpose, better choose default, which is 10 secs, by not specifying the timeout line at all.

spawn ssh -l mathew
spawn will execute whatever command that is put after that in the line

expect “mathew@’s password:”
This is where the catch is . The expect will wait for a feedback similar to “mathew@’s password:”

send — “mathew\r”
Once the expect sees the specified keywords, the next send command will send the value in quotes (mathew) to the shell. The “\r” at the end is mandatory since it instruct to put a newline at the end after writing “mathew” to the prompt.

This will free the control from the script we just ran and give control back to the shell so that we can “interact”


Many people are having trouble setting up secure keys on their machine. It is very simple and here are the steps

Let us assume that the machine you want to login is the Server and the machine from where you login is the client

We will start with the Client. Do the following steps at the Client.

step 1) Create the keypair using dsa encryption. This can be done by passing the key encryption method type to ssh-keygen.

ssh-keygen -t dsa
Hit enter when asked for passphrase. The ssh-keygen program will generate a public and a private key. They are by default named as “” (public key) and private key as id_dsa and is stored in .ssh folder of your home directory by default. You need to safeguard your id_dsa file by encryption and other means like permission restrictions.

ON the server side

Step 1) cd .ssh/ (If the directory is not there , create one)

Step 2) Copy the generated in the first step to the Server and append it to the file authorized_keys in .ssh directory.

cat >> .ssh/authorized_keys
Step3 ) Make sure that the permissions on the authorized_keys file is “600″

chmod 600 authorized_keys
Remove the public key file that you have copied. The mistake that most people make is in forgetting the last step and also copying the private key instead of the public key file.

How to login from PUTTY using authorized keys.

When you login from a windows machine using PUTTy, Putty is your client.You will have to use the putty keygen tool to generate the keygen described in the first step.

Once the keygen is generated, you will have two files , the same as we have described in the first step. Copy the public file content to the Server and the private key need to be added to the putty session you are using to login. For this open PUTTy, goto Connection -> SSH -> Auth and browse to the private file you have generated. Now goto the login session and you will be able to login without any password.
Posted in Uncategorized
Views 1294 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 11:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration