How to run a shell script over ssh with resource(.txt files) in one machine and the script in another machine?

StackOverflow https://stackoverflow.com/questions/21308621

Question

I want to run a shell script using SSH which takes resource from other machine while the script is in some other machine, all on the same network. I don't want to copy the resource to the local machine.

Note: The shell script takes .txt file as input

Was it helpful?

Solution

If you have script.sh on server1 and file.txt on server2, you can connect through ssh to server1, and then do:

[user@server1]$ ssh user@server2 "cd mydir && cat file.txt" | ./script.sh

OTHER TIPS

Try this:

 ssh USER_NAME@HOST_ADDRESS "BASH_SCRIPT_FILE_PATH"

You will need to provide password whenever required.

If your script is in Machine A, you can't run that on Machine B without copying it over. First, copy the script over to Machine B using scp

[user@machineA]$ scp /path/to/script user@machineB:/home/user/path

Then, just run the script

[user@machineA]$ ssh user@machineB "/home/user/path/script"

This will work if you have given executable permission to the script.

OR

Try this one..

<hostA_shell_prompt>$ ssh user@hostB "ls -la"

That will prompt you for password, unless you have copied your hostA user's public key to the authorized_keys file on the home of user .ssh's directory. That will allow for passwordless authentication (if accepted as an auth method on the ssh server's configuration)

I not fully understand your question. Other answers gave "How to run remote script?"

But i think question is Remote script has to take remote file, even I not sure about this

  1. Login Remote PC using ssh.
  2. Install sshfs if not installed .
  3. Then mount other remote machine directory which has the file you want to use in script to local directory. This can be done using sshfs
  4. Then run the script with file from locally mounted directory
  5. Then unmount the directory when you finished.

Somewhat large procedure.

Mounting remote directory with sshfs

man sshfs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top