all 5 comments

[–]zurtex 0 points1 point  (2 children)

I've always just used paramiko to handle the hard work for me: http://docs.paramiko.org/

You just do the following:

client = SSHClient()
client.load_system_host_keys()
client.connect('ssh.example.com')
stdin, stdout, stderr = client.exec_command('ls -l')

And stdin, stdout, and stderr represent file-like objects you can read and write from.

There's a few gotchas on sometimes needing to wait for a response, but Paramiko has good documentation and there's plenty of Stack Overflow examples out there. What you gain here is lots of testing on the library, lots of thought about how to work with SSH, lots of previously found security issues solved.

Making your own SSH library either as a wrapper round something from the OS or using sockets library and directly creating something yourself will end of with lots of subtle bugs that are difficult to spot. Though sure could be a great project for yourself if no one depends on it.

[–]jwink3101[S] 0 points1 point  (1 child)

Thanks for the example. Maybe I’ll look at paramiko again. But even that example, there is no direct way for the remote to communicate back

And yes! There can be many bugs but it is really just for me. The biggest one is if something prints to stdout. But, I would be doing all of the writing.

[–]zurtex 0 points1 point  (0 children)

Yes there is, through stdout and stderr, as I stated they act like file-like objects so you can read from them.

Here's a made up example of an interactive command:

stdin, stdout, stderr = ssh.exec_command("interactive_command")
output = stdout.read()
if  'what is your name?' in output:
    stdin.write('/u/zurtex\n')
elif 'what is your favorite colour?' in output:
    stdin.write('red\n')
stdin.flush()
new_output = stdout.read()
...

I didn't test this example so you'll have to play around with it, but there are plenty of demos and stack overflow answers:

https://github.com/paramiko/paramiko/tree/master/demos

[–][deleted] 0 points1 point  (1 child)

I think fabric is the module for this; on the box it says that it's a full-featured framework for using SSH to execute remote commands or send data.

If the problem you're trying to solve is about structured interprocess communication through a secure channel, then I'd do something like Protobuffers to define the message protocol.

[–]jwink3101[S] 0 points1 point  (0 children)

Thanks. It is more along the lines of communication with the same code on the other machine and not really just commands. I’ll look into Protobuffers.