Suggest design to accomodate non-unix platforms ?

R

Richard Shea

On a *nix box this is a reasonable bit of Python :

cmd = "ssh -o StrictHostKeyChecking=no -i %s %s@%s '%s' > %s" % (key,
user, dns, "echo CONNECTION READY", tmp_file)
result = os.system(cmd)

.... on a Windows box it will fail because 'ssh' isn't part of Windows.

There *are* ways of achieving the equivalent functionality in Windows,
eg

putty.exe -ssh user@host

.... and that's only one of them.

So I'm interested in suggestions/examples where a user can update a
config file to specify by which means they want (in this case) the ssh
functionality to be supplied.

I'm thinking of something in a config file like this ...

ssh_dropin = {exec: 'putty.exe -ssh %s@%s', args:['auser','somehost']}

.... which I think would work and be sufficiently flexible to deal with
alternatives to putty.exe but is there a more established (...
better !) way of doing this stuff ?

Thanks

Richard.
 
I

Irmen de Jong

... which I think would work and be sufficiently flexible to deal with
alternatives to putty.exe but is there a more established (...
better !) way of doing this stuff ?

Perhaps install Cygwin and use its ssh.exe?
Or use the paramiko library? (which, I believe, implements SSH directly)

Irmen
 
R

Richard Shea

Perhaps install Cygwin and use its ssh.exe?
Or use the paramiko library? (which, I believe, implements SSH directly)

Irmen

OK I didn't know about Paramiko, that looks good - I will investigate.
I didn't want to go to as far as Cygwin because it's a bit much to
make that a requirement when installing a library.

More generally though I'd be interested to see patterns which would
allow a plugin alternative to be used for a system utility on which a
library is dependent.

I've realised that my example isn't much good because the values in
the args list would in fact have to equate to the variables already in
place within the relevant function - it's pretty late where I am so I
probably shouldn't be trying to think up new ideas but I'd be
interested to hear from others about how they might do this.
 
T

tinnews

Richard Shea said:
On a *nix box this is a reasonable bit of Python :

cmd = "ssh -o StrictHostKeyChecking=no -i %s %s@%s '%s' > %s" % (key,
user, dns, "echo CONNECTION READY", tmp_file)
result = os.system(cmd)

... on a Windows box it will fail because 'ssh' isn't part of Windows.
ssh isn't guaranteed to be part of Unix/Linux systems surely.
 
G

Grant Edwards

ssh isn't guaranteed to be part of Unix/Linux systems surely.

It's not absolutely, 100%, iron-clad guaranteed, but

* it's generally a pretty safe bet

* it's usually trivial to install if it's not there
 
R

Richard Shea

ssh isn't guaranteed to be part of Unix/Linux systems surely.

Ha ! Ok I didn't know that (or more to the point hadn't really thought
about it). But anyway my more general point was looking for a design
pattern which would accommodate the use of system utilities across
multiple platforms which has sufficiently similar functionality that
they were functionally identical but which would have to be invoked in
different ways
 
M

Miki Tebeka

So I'm interested in suggestions/examples where a user can update a
config file to specify by which means they want (in this case) the ssh
functionality to be supplied.
You can do something like that (it's called a factory):

COMMANDS = {
'win32': 'win32 command goes here',
'linux2': 'linux command goes here',
'darwin': 'OSX command goes here',
}
def get_command():
return COMMANDS.get(sys.platform)
 
S

Steven D'Aprano

You can do something like that (it's called a factory):

COMMANDS = {
'win32': 'win32 command goes here',
'linux2': 'linux command goes here',
'darwin': 'OSX command goes here',
}
def get_command():
return COMMANDS.get(sys.platform)


Your suggestion is good, but I think your terminology is off. COMMANDS is
a dispatch table, or a lookup table, and get_command merely does a look-
up in that table.

A factory dynamically builds a new object on request, rather than always
return the same old object time and time again. So a factory might look
like this:

def get_command(flag1=True, flag2=False, flag3=True):
cmd_parts = [COMMANDS.get(sys.platform)]
if flag1:
cmd_parts.append('--some_option')
else:
cmd_parts.append('--another_option')
cmd_parts.append('-x')
if flag2:
cmd_parts.append('spam')
else:
cmd_parts.append('ham')
if flag3:
cmd_parts.append('--foo=bar')
cmd = ' '.join(cmd_parts)
return cmd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top