2

I have two machines, A and B. Both machines can be either Windows or Linux machines, but - for simplicity - I know beforehand which is which. I also know a username and a password for the remote machine B (no other authentication method). So I have four possible combinations:

A=Windows  B=Windows
A=Windows  B=Linux
A=Linux    B=Windows
A=Linux    B=Linux

I am looking for a small python script (or better: standard library) with which it is possible to execute a remote command on B from A. To be precise: On machine A I start a python script to run some command cmd on the remote machine B. Is there a generic way to do so in python?

For the Linux->Linux combination I could think using ssh for example and subprocess, but I probably run into problems with the username/password authentication.

For the Windows->Windows combination there is a tool called psservice which I already have been using (together with subprocess).

Any idea how to solve this problem in a most pythonic way? Does not need to be one function, it can be implemented in four different ways. And, if possible, without the use of third-party libraries (which is somewhat inconsistent, as psservice is already a third-party library I am using...).

Alex
  • 34,021
  • 64
  • 178
  • 371
  • Why not just use `ssh` for everything? Keeps it simple. – Gillespie Nov 21 '14 at 15:10
  • Are there any other useful suggestions (which actually work) and which do NOT include `paramiko` (which does not work for me...)? – Alex Nov 21 '14 at 18:06
  • Did you ever get ssh working at the commandline between windows and linux? Because that's step #1. If you can't even get ssh working at the commandline, paramiko isn't going to work and other suggestions aren't going to be easier for you; they are going to be even more difficult. For example, you can use sockets, snmp, http, to communicate between computers but all of those require even more work. – Gillespie Nov 21 '14 at 19:31
  • Also, you can use ssh with python without paramiko. There are other libraries or you could just do it manually with Popen: http://python-for-system-administrators.readthedocs.org/en/latest/ssh.html – Gillespie Nov 21 '14 at 19:37

2 Answers2

1

I suggest using ssh/sftp, via the paramiko library.

All major ciphers and hash methods are supported. SFTP client and server mode are both supported too.

And it is cross-platform.

Corey Goldberg
  • 53,391
  • 24
  • 118
  • 137
  • ... and it does not work..., see http://stackoverflow.com/questions/27065241/paramiko-authenticationexception-issue-ii – Alex Nov 21 '14 at 16:54
1

Try paramiko. I am using it and it is very simple to use.

EDIT 1

host='IP'
username='user'
password='password'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(host, username, password)

stdin, stdout, stderr = ssh.exec_command("pwd")
stdout.read()
ssh.close()

EDIT 2:

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=password)

stdin, stdout, stderr = ssh.exec_command('pwd')

stdout.read()
ssh.close()

EDIT 3 Here also a solution using private/public keys:

key = paramiko.RSAKey.from_private_key_file(keyDest, keyPass)
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(host, username=user, pkey=key)

stdin, stdout, stderr = ssh.exec_command('pwd')
stdout.read()
ssh.close()

Both, EDIT 1 and EDIT 3 works fine in my script..

BartRising
  • 11
  • 2
  • Ok, I will try. I have it already installed, by some coincident. But I get an error on the third line: `gaierror: [Errno 10109] getaddrinfo failed` – Alex Nov 21 '14 at 15:19
  • What are the exact requirements to use paramiko? What must be installed on what machine? – Alex Nov 21 '14 at 15:21
  • Well before using paramiko, make sure you can actually ssh from the command-line first. Make sure each machine is running an ssh server. – Gillespie Nov 21 '14 at 15:21
  • Next error: `AuthenticationException: Authentication failed.` while absolutely sure to have used the correct username/password combination. Maybe I need to restart one/both machines...? – Alex Nov 21 '14 at 15:26
  • Does it matter when domain usernames/passwords are used? – Alex Nov 21 '14 at 15:32
  • I think it has to be `ssh.connect(host, username = username, password = password)`; but no, I still get the same authentication error. It works fine with `PsExec`, though. So the username and the password are correct. – Alex Nov 21 '14 at 16:30
  • I posted a solution with private/public key... try this – BartRising Nov 21 '14 at 16:49
  • What is key dest and key pass? – Alex Nov 21 '14 at 16:53
  • KeyDest - destination of your private key (C://..), KeyPass - password for your private key, if you have one. – BartRising Nov 21 '14 at 16:57
  • Do you refer to keys on machine `A` or machine `B`? How to create them and where to find them? – Alex Nov 21 '14 at 17:04
  • You "should" have a private key on your machine where you are running the code. The public key should be on the machine you want connect to. But I think you have to google first and read about private- and public keys, for e.g. how they work or how they are generated. After that I am sure you will know what you have to do with both keys ;-) – BartRising Nov 21 '14 at 17:13
  • But why is the username/password authentication not working as it should be? Maybe paramiko has a bug? I am trying to debug paramiko myself to see the cause of this issue. Thanks so far... – Alex Nov 21 '14 at 17:14
  • [link]http://stackoverflow.com/q/20538685/4279118 look here. Did you installed PyCrypto and ecdsa? Maybe that is why the authentication does not work.. – BartRising Nov 21 '14 at 17:21
  • Requirement already satisfied for both packages. Is there a way to know what other packages are required to be installed? Or do I just start 'guessing' here or learn the complete source code of paramiko...? – Alex Nov 21 '14 at 17:30
  • The key suggestion also does not seem to work. I created a private RSA key in a file and gave the name of the file to `from_private_key_file`. Error this time: `paramiko.ssh_exception.AuthenticationException: Authentication failed.` Maybe paramiko is just a random error generator? – Alex Nov 21 '14 at 18:28