Python Forum

Full Version: how to connect over network and get the task done
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I have written a code snippet here. Where I wanted to connect a remote network, execute my script which is present in that system and get the output of that script.
import paramiko

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = 'x.x.x.x', port = 22, username = 'admin', password = 'pass')
stdin, stdout, stderr = ssh.exec_command('python /home/maiya/sample.py', get_pty = True)
print(stdout.readlines())
ssh.close()
what could be the issue in above code?, as am getting an error authentication failed.
Error:
PS C:\Users\maiya\test> python connect.py Traceback (most recent call last): File "C:\Users\maiya\test\connect.py", line 6, in <module> ssh.connect(hostname = 'x.x.x.x', port = 22, username = 'admin', password = 'pass') File "C:\Users\maiya\AppData\Roaming\Python\Python310\site-packages\paramiko\client.py", line 435, in connect self._auth( File "C:\Users\maiya\AppData\Roaming\Python\Python310\site-packages\paramiko\client.py", line 766, in _auth raise saved_exception File "C:\Users\maiya\AppData\Roaming\Python\Python310\site-packages\paramiko\client.py", line 753, in _auth self._transport.auth_password(username, password) File "C:\Users\maiya\AppData\Roaming\Python\Python310\site-packages\paramiko\transport.py", line 1564, in auth_password return self.auth_handler.wait_for_response(my_event) File "C:\Users\maiya\AppData\Roaming\Python\Python310\site-packages\paramiko\auth_handler.py", line 245, in wait_for_response raise e paramiko.ssh_exception.AuthenticationException: Authentication failed. PS C:\Users\maiya\test>
Any suggestion would really help. Thanks a lot

Regards,
maiya
Try to connect with allow_agent=False perhaps. Otherwise check that you can connect the remote host without Python, perhaps from Putty in Windows.
(Jul-09-2022, 07:23 AM)Gribouillis Wrote: [ -> ]Try to connect with allow_agent=False perhaps. Otherwise check that you can connect the remote host without Python, perhaps from Putty in Windows.

Connection is there, however allow_agent = False helps actually.

How to run the python script over ssh?
(Jul-09-2022, 09:55 AM)maiya Wrote: [ -> ]
(Jul-09-2022, 07:23 AM)Gribouillis Wrote: [ -> ]Try to connect with allow_agent=False perhaps. Otherwise check that you can connect the remote host without Python, perhaps from Putty in Windows.

Connection is there, however allow_agent = False helps actually.

How to run the python script over ssh?

I got it!!! Thanks.