Hi,

I want to write a python program for connect to remote server (using ssh) and execute the command and return back with output. Here is my code:

import sys
import os
from subprocess import Popen, PIPE, STDOUT
from time import sleep
from os import waitpid, execv, read, write

 #Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"
ssh = subprocess.Popen(['ssh', 'uname@hostname',  COMMAND],
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)


result = ssh.stdout.readlines()
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

If I execute this code it will ask for password. I want to run the code so that script should not ask password.

pexpect module will support for linus not windows. Could you please help me on this.

If you want more info let me know.

Thanks,
Mahadev

@Gribouillis: Thanks for the info. I have installed paramiko even though "imopt paramiko" command throws error as below.

>>> import paramiko
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win-amd64\egg\paramiko\__init__.py", line 69, in <module>
  File "build\bdist.win-amd64\egg\paramiko\transport.py", line 32, in <module>
  File "build\bdist.win-amd64\egg\paramiko\util.py", line 32, in <module>
  File "build\bdist.win-amd64\egg\paramiko\common.py", line 98, in <module>
  File "build\bdist.win-amd64\egg\paramiko\rng.py", line 23, in <module>
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ut
il\randpool.py", line 30, in <module>
    import Crypto.Random
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\__init__.py", line 28, in <module>
    from Crypto.Random import OSRNG
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\OSRNG\__init__.py", line 34, in <module>
    from Crypto.Random.OSRNG.nt import new
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\OSRNG\nt.py", line 28, in <module>
    import winrandom
ImportError: DLL load failed: %1 is not a valid Win32 application.
>>>

Could you please give suggest some other alternative solution?

Thanks

The correct way to install paramiko is by typing

pip install paramiko

in a terminal (right now it may fail because pypi is having an outage as I'm writing this post)

Hi Gribouillis,

I am using 2.7.3 version of python

D:\softwares\paramiko-1.7.5>python --version
Python 2.7.3

If I tried to install paramiko as you suggested it's throws below error.

D:\softwares\paramiko-1.7.5>pip install paramiko
'pip' is not recognized as an internal or external command,
operable program or batch file.

Hi,

Now I have installed pip then I tried to install paramiko but it throws error as below.

C:\Python27\Scripts>pip install paramiko
Requirement already satisfied (use --upgrade to upgrade): paramiko in c:\python2
7\lib\site-packages\paramiko-1.7.5-py2.7.egg
Requirement already satisfied (use --upgrade to upgrade): pycrypto>=1.9 in d:\us
erdata\mhn\application data\python\python27\site-packages (from paramiko)

Please help me on this.

From your previous error message, it seems that there is a problem with your PyCrypto module. Try to run

python -c "import Crypto.Random; print Crypto.__version__"

It should print 2.6.1. If it does not work, you can try

pip install --upgrade pycrypto

Try again the previous command. If it doesn't work, you can manually remove the Crypto directory and any reference to Crypto in the site-packages directory and try again.

Then try

python -c "import paramiko; print(paramiko.__version__)"

On my (linux) computer, it prints 1.10.1. You may have an old version on your computer.
Run

pip install --upgrade paramiko

and try again the previous command until it works

Edited 6 Months Ago by Gribouillis

Hi Gribouillis,

I have tried what you suggested but it returns the error

D:\PHOTONS\SPRINT_1606>python -c "import Crypto.Random; print Crypto.__version__
"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\__init__.py", line 28, in <module>
    from Crypto.Random import OSRNG
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\OSRNG\__init__.py", line 34, in <module>
    from Crypto.Random.OSRNG.nt import new
  File "D:\userdata\mhn\Application Data\Python\Python27\site-packages\Crypto\Ra
ndom\OSRNG\nt.py", line 28, in <module>
    import winrandom
ImportError: DLL load failed: %1 is not a valid Win32 application.





D:\PHOTONS\SPRINT_1606>pip install --upgrade pycrypto
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connecti
on broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connect
ion.VerifiedHTTPSConnection object at 0x00000000036B5048>, 'Connection to pypi.p
ython.org timed out. (connect timeout=15)')': /simple/pycrypto/
Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connecti
on broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connect
ion.VerifiedHTTPSConnection object at 0x00000000036B5160>, 'Connection to pypi.p
ython.org timed out. (connect timeout=15)')': /simple/pycrypto/
Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connecti
on broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connect
ion.VerifiedHTTPSConnection object at 0x00000000036B5278>, 'Connection to pypi.p
ython.org timed out. (connect timeout=15)')': /simple/pycrypto/
Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connecti
on broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connect
ion.VerifiedHTTPSConnection object at 0x00000000036B5400>, 'Connection to pypi.p
ython.org timed out. (connect timeout=15)')': /simple/pycrypto/
Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connecti
on broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connect
ion.VerifiedHTTPSConnection object at 0x00000000036B5588>, 'Connection to pypi.p
ython.org timed out. (connect timeout=15)')': /simple/pycrypto/

Actuall I have replaced my code with below mentioned code (Using plink) in my python script :

os.system("plink.exe -ssh -pw PASSWORD USER@IP_ADDRESS 'telnet 0 XXXX;','LT_START;'")

It will connect to remote server (IP_ADDRESS) and it will execute first command (telnet 0 XXXX;) then it won't execute 2nd command (LT_START;).

I can keep both the commands in text file and run, but my requirement is I should pass commands as mentioned in the command (Do not pass commands through any file).

The best thing to do is to try again with pip. Yesterday there was some problems with the pypi site, and you may have a bad internet connection or something like that, but

pip install --upgrade pycrypto

must work. You can not replace the functionalities of paramiko with calls to os.system.

Edited 6 Months Ago by Gribouillis

Hi Gribouillis,

Thanks for your continues support. I have one doubt could you please clarify me.

Is this possible to do ssh to server1 and do telnet in server2 from server1 and execute command in server2 in a single command?

This article has been dead for over six months. Start a new discussion instead.