user9280049
user9280049

Reputation:

How can I use ssh with python and linux to connect to windows and run commands?

I wrote a generic script on Python that supports Windows and Unix ssh connection from Unix.

When I try Unix command from Unix to create dir in Windows, I get exit 53

/usr/bin/ssh2 --password pass -l admin ip_address mkdir "C:\Temp\ALEX_TEST_EX" &

When I write only /usr/bin/ssh2 --password pass -l admin ip_address, it is Ok. I login to Windows

When I try C:\Temp\ALEX_TEST_EX on this machine manually it is also Ok.

What is problem?

I also try to use with Python ssh2 command like

import paramiko
ssh = paramiko.SSHClient()
ssh.connect("ip_address", username="admin", password="pass")

but I get exceptions

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/python3.5/site-packages/paramiko/client.py", line 402, in connect
    self, server_hostkey_name, server_key
  File "/python3.5/site-packages/paramiko/client.py", line 768, in missing_host_key
   'Server {!r} not found in known_hosts'.format(hostname)
paramiko.ssh_exception.SSHException: Server 'X.Y.Z.W' not found in known_hosts

Upvotes: 1

Views: 5005

Answers (1)

Stephen Rauch
Stephen Rauch

Reputation: 49774

You have not configued the client machine to allow you to know the server you are trying to connect to. You can either configure the client, or as a work around you can set the MissingHostKeyPolicy on paramiko like:

Code:

To warn when host not in known hosts:

ssh.set_missing_host_key_policy(paramiko.WarningPolicy)

To auto add host to know hosts:

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)

Full Code:

import paramiko

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

ssh.connect("localhost", username="admin", password="pass")

Upvotes: 1

Related Questions