Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.


share|improve this question

marked as duplicate by jasonwryan, Renan, Gilles, warl0ck, manatwork Dec 7 '12 at 11:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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.

share|improve this answer

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