santosh
santosh

Reputation: 575

how to run a local script in remote server using expect and bash script

I want to automate my support work. 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 machine. I don't want to upload the script.Till now I have made a script that will help to connect to the server but i am not able to do any any other operations through. Is it possible to run a local script on the remote server?

My script:

!/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

Upvotes: 0

Views: 5076

Answers (3)

alinsoar
alinsoar

Reputation: 15793

You can do it so:

ssh user@remote-ssh-server "commands of script" 

If you keep the script into a file, you can do so:

ssh user@remote-ssh-server << End
paste the script here
End

Here are a few details.

  1. Log in with "ssh your_user@ssh_server ls /"

This command will log in, list the root directory, and closes the connection.

  1. After the 1st pass succeeds, try to replace "ls /" with your script.

  2. After the 2nd step succeeds, start a ssh agent.

Do not pass to step 2 before to finish the first step.

Use a ssh tutorial like this one -- this will help you

http://support.suso.com/supki/SSH_Tutorial_for_Linux

Upvotes: 2

ruiqi
ruiqi

Reputation: 1

ssh user@server bash < /path/to/local/script.sh

Upvotes: 0

ilazgo
ilazgo

Reputation: 650

Your script structure should be:

  1. Connect via SSH.
  2. CRUD operations
  3. Exit SSH.

The script with CRUD operations has to be on the remote computer, because you can't run any script that isn't on the machine where you want to run it.

Upvotes: 0

Related Questions