Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to make a script that will connect to a server and do some CRUD operation on the server and again return to local. I do not want to upload the script to server..

I have made a script that will help to connect to the server but I am not able to do any any operation through the help of the script.

please make my doubt clear regarding the fact that weather its possible to run a local script (as in my case ) in the remote server.

If yes how please specify the script also for which I will be grateful to you .

till now my script is

#!/usr/bin/expect
set ip neviss
set user user
set password 1234
spawn ssh "$user\@$ip"
expect "Password:"
send "$password\r";
interact ( after this line any command is not getting executed) 
ls -lrt 
share|improve this question

3 Answers 3

Try something like this:

#!/usr/bin/expect
spawn ssh usert@server
expect "user@server's password:" {send "$password\r"}
expect "$ " {send "hostname\n"}
expect "$ " {send "ls -alrth\n"}
expect "$ " {send "exit\n"}

Not sure if it is the most efficient way of doing it, but it seems to work in my test. (spaces after $)

Also, you can put this at the top of your script "exp_internal 1".

This will aid in the debugging process!

share|improve this answer
    
many many thanks sir for answering my query.but can you just explain more like how does it work and what i need to right at the expect "#" {send "hostname\n"} i mean hostname . please reply i am in middle of a problem .please help . –  user1760929 Nov 26 '12 at 5:36
    
I tried in the above process its not working for me ...please help .... –  user1760929 Nov 26 '12 at 5:42
    
Hey sorry for late response. From what I understand you are basically trying to tell the script what to 'expect' after issuing each command. So for instance after sending the password, you would be at a normal command prompt. Now where you might be having a tough time right now is at the pound "#" sign. If you are using root then use the pound "#" sign. If you are using a regular user then use the dollar "$" sign. Regular users use the "$" sign on their command prompt. I will modify the script above –  bourne Nov 27 '12 at 13:35
    
Weird. I tested this myself just now and it doesn't seem to work as a regular user. If you do it as root it works as expected. I will keep playing with it. –  bourne Nov 27 '12 at 14:02
    
Okay sorry I figured it out. Something stupid. Add a space after the "$".. "$ ". I also found something else very useful.. will add above –  bourne Nov 27 '12 at 14:37

You don't need to call Interact in order to send a command. Interact waits for your input instead.

Here's a working example for running a "test.sh" script in the remote server (also calling a couple of other commands just to illustrate the idea), which also solves the user/root different prompt problem. Remember to make test.sh executable.

#!/usr/bin/expect

set prompt "(%|#|\\$) $"
set ip neviss
set user user
set password 1234

spawn ssh "$user\@$ip"
expect "assword:"
send "$password\r";
expect -re $prompt
send -- "ls -l\r"
expect -re $prompt
send -- "./test.sh\r"
expect -re $prompt
send -- "sleep 5\r"
expect -re $prompt
send -- "exit\r"
share|improve this answer

You can let ssh do most of the work. Assuming your local script is a bash script and it's in a file named "local.script":

#!/usr/bin/expect
set ip neviss
set user user
set password 1234
set script local.script

# here I spawn a shell that will run ssh and redirect the script to ssh's stdin

spawn sh -c "ssh $user@$ip bash < $script"

expect "Password:"
send "$password\r"

# and just wait for the script to finish
expect eof

Note that @ is not a special character in Tcl, and it does not require escaping.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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