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

Execute the scripts over ssh connection #162

Open
tareksalem opened this issue Jan 15, 2023 · 9 comments
Open

Execute the scripts over ssh connection #162

tareksalem opened this issue Jan 15, 2023 · 9 comments

Comments

@tareksalem
Copy link

I found the library is great and covers most of bash scripting, but you know most of the time when you do bash scripting, you do it on remote servers so how can I make the scripts that I write using this library to be executed over ssh, is there something like this for example?

script.SSH("connection here").EXEC()

thank you

@tareksalem tareksalem changed the title Execute the scripts on over ssh connection Execute the scripts over ssh connection Jan 15, 2023
@bitfield
Copy link
Owner

What about something like this:

script.Exec("ssh myhost ls").Stdout()

@tareksalem
Copy link
Author

thanks for your reply, is this work for executing sub commands on that ssh connection using builder pattern?

script.Exec("ssh yhost ls").FIle("filepath").Stdout()

will this work?

@bitfield
Copy link
Owner

We can execute commands remotely using Exec("ssh..."), and append the results to local files using AppendFile, but since the compiled script program is running locally, all of the script operations will apply locally, not on the remote machine—is that what you're asking?

@tareksalem
Copy link
Author

@bitfield
yes, I am asking about why the script operations are runing locally and there is no a mechanism to run them over ssh, I know that I can run command over ssh using Exec("ssh ...remoteCommands") but my question is, why not defining something like a context to tell script library that I am using ssh context now or local context, something like this

script.Context("ssh").ssh("ssh connection here").DoSomeWorkOnRemoteServer().Context("local").doSomeWorkLocally()

@bitfield
Copy link
Owner

Well, how could we do any work on the remote server? Only by executing code there. How can we get the code on to the remote server? We'd have to deploy a binary there. If we can do that, why bother with SSH? Now we can just do the work locally.

@tareksalem
Copy link
Author

@bitfield
mmm, actually it differs from use case to another, my question came from a use case, which is a golang service running and listening for new created servers then this golang service should install some libraries and do some work on these new created servers, the only way to do that is using ssh connection, also in all cases to deploy the binary code on the remote servers you still need to do ssh so why not performing your actions directly on that remote server using the syntax I suggested?

@gedw99
Copy link
Contributor

gedw99 commented Mar 14, 2023

Yeah I have similar use case

I am using script with nats

nats is the Transport , instead of ssh.

The binary ( that imports script ) needs to exist on both sides though !

I was thinking of add NATS as a formal transport provider to Script as a PR but my use case / prototype has not stabilised yet.

nats can stream anything.
You can even bootstrap the client binary onto the server via nats and then your set .

The data and ops are just topics in the same way that file paths are namespaces. It’s pretty nice

you can add nats AAA security too so that only certain user roles can run over certain topics and so certain file paths on the server.

hit me up if your curious for more info

@chrisgo
Copy link

chrisgo commented Jun 16, 2024

So basically, something that allows you to do it like this https://www.fabfile.org/

  • You would create your "script"
  • Then create a list of hosts & some basic auth to be able to get in (pubkey most likely)
  • Have a way to execute the entire "script" against the list of hosts

@bitfield
Copy link
Owner

I think we pretty much have this already, don't we? We can do something like:

script.File("hosts.txt").ExecForEach("ssh {{.}} ls").Stdout()

If we agree that deploying binaries to the remote hosts is out of bounds, but running plain shell commands and getting the output is okay, then yes, I think this is done.

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

4 participants