Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy script to remote and execute OR create command object from local script #103

Closed
ptg1792 opened this issue Mar 10, 2023 · 3 comments
Closed

Comments

@ptg1792
Copy link

ptg1792 commented Mar 10, 2023

Hi there,

First of all thank you so much for creating this library. This is awesome, really easy to use. We are able to use it with minimal efforts. Now we are stumbling upon a use case where we need to execute a script on the remote server which is present at local.
I saw the Bash script on the fly part https://www.npmjs.com/package/ssh2shell#bash-scripts-on-the-fly- and tried to read the file and create the command array on the go but wasn't able to. :(
Is there any way, we can directly copy the file to remote server or command array can take input of local shells script?

Please let me know if any further information is required.

@cmp-202
Copy link
Owner

cmp-202 commented Mar 10, 2023

Hi,
Thanks for the kind feedback.
You could try something like loading the script content into a variable and then add a command "printf " + script + " > myScript.sh to create the script on the host.
Follow the on-the-fly example for running the script.

The other option is to copy the script to the host using another package and then run it with ssh2shell.

@cmp-202
Copy link
Owner

cmp-202 commented Mar 13, 2023

Remember host.debug (process information) and host.verbose (more content) to fault find.
I usually use one or the other.

You could try SCP to transfer from local to host while connected.
I have not tested this.

SCP while connected:
SCP help

//Just a script
host.commands= ["scp myscript.sh " + host.server.username + "@" + host.server.host + ":/some/remote/directory",
  "sudo chmod 700 /some/remote/directory/myscript.sh",
   "./some/remote/directory/myscript.sh"]

//a folder
host.commands= ["scp -r scripts " + host.server.username + "@" + host.server.host + ":/some/remote/directory/scripts",
   "sudo chmod -R 700 /some/remote/directory/scripts"]

Authentication could require handling.

@ptg1792
Copy link
Author

ptg1792 commented Mar 30, 2023

I apologize for delayed response on this. I was able to do a workaround through this problem which worked for me.
I read the local script file & encoded into Base64 to avoid any special characters.
Then with it I formed a command at the runtime to execute at the target:

echo "<>" | base64 -d | bash

and if you have command line inputs:
echo "<>" | base64 -d | bash -s

This worked for me without physically copying the script on target as well as running into any special character issues with echo or printf statement. Hopefully it can help others who are looking to solve the same problem.

@ptg1792 ptg1792 closed this as completed Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants