Possible Duplicate:
How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

I want to connect to a remote server through ssh. i have to also provide password within that script. after connecting to the remote server i want to run the serverstartup script. please provide sample example.



1 Answer 1


You can write a script using expect tool.

In Redhat, expect package comes by default. But in Ubuntu you need to install it separately.

You can check this by using commands:

$ rpm -qa | grep expect for Redhat

$ dpkg -l expect for Ubuntu

The following script will do your work:


spawn ssh username@hostname
expect "username@hostname's password: "
send "password\r"
exec <path_of_init_script>

where username is the actual username by which you are logging in, hostname may be hostname or ip address, and password is the actual password.

This way you can log into the box and run the script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.