Saturday, January 24, 2009

ssh script

Before executing this script, you need to know some details,

What Is Expect?

Expect is a UNIX automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. It uses UNIX pseudo terminals to wrap up sub-processes transparently, allowing the automation of arbitrary applications that are accessed over a terminal. With Tk, interactive applications can be wrapped in X11 GUIs. Expect has regular expression pattern matching and general program capabilities, allowing simple scripts to intelligently control programs such as telnet, ftp, and ssh, all of which lack a programming language, macros, or any other program mechanism. The result is that Expect scripts provide old tools with significant new power and flexibility.

The Script
**********
When a server keeps prompting for password at SSH attempts in spite of setting up RSA/DSA keys, this script can be used to overcome that issue. Make sure that the script has 700 permission as it will contain your password in plain text.
-------------------------------------------------------------------------
#! /usr/bin/expect


# Edit the following line - $USER@$SERVER
spawn ssh your-useid@your-server-name

# First time connection will print out some text for
# which one needs to type 'yes' to continue
# Comment these two lines after the first attempt
expect "*Are you sure you want to continue connecting*"
send "yes\r"

# Put the password here
expect "*assword*"
send "YOUR-PASSWORD\r"

# Start interacting
interact
--------------------------------------------------------------------------------------

The three commands send, expect, and spawn are the building power of Expect. The send command sends strings to a process, the expect command waits for strings from a process, and the spawn command starts a process.

The spawn Command

The spawn command starts another program. The first argument of the spawn command is the name of a program to start. The remaining arguments are passed to the program.


Thanks and Regards,
Sylesh H

No comments: