问题
I'm going to place both my question and answer here, since I had a very difficult time figuring it out. (Also I started out trying to install the "ssh" library, but it has been superceded by the "paramiko" library of the same function. I may have missed a replacement here, please forgive if I have.)
Question: I need to install the "paramiko" library on a windows 7 system.
Pip and easy_install both give errors when trying to install the "Crypto" module.
I headed down a number of blind alleys involving Visual C and Visual Studio without success.
Answer:(in two parts) 1) Install the crypto library from a binary, like from here:
http://www.voidspace.org.uk/python/modules.shtml#pycrypto
This installs the crypto library, but with a different capitalization than the paramiko library expects. So I learned a little trick over here:
http://stackoverflow.com/questions/19623267/importerror-no-module-named-crypto-cipher
with the answer by user "pho" to add these lines to the python program:
import crypto
import sys
sys.modules['Crypto'] = crypto
Now I am able to install, and run, the paramiko library. I hope this helps others find the solution more quickly.
回答1:
Answer:(in two parts) 1) Install the crypto library from a binary, like from here:
http://www.voidspace.org.uk/python/modules.shtml#pycrypto note: the "paramiko" library has replaced "ssh" so this answer reflects that
This installs the crypto library, but with a different capitalization than the paramiko library expects. So I learned a little trick over here:
http://stackoverflow.com/questions/19623267/importerror-no-module-named-crypto-cipher
with the answer by user "pho" to add these lines to the python program:
import crypto
import sys
sys.modules['Crypto'] = crypto
Now I am able to install, and run, the paramiko library. I hope this helps others find the solution more quickly.
That was a good start. My next stopper was figuring out how to get the correct keys. If you are like me, you use putty and it's keygen utility for key management. And it works great! But it keeps it's private keys in a file different that what paramiko expects. But puttygen also provides a solution to this.
Open your *.ppk file in puttygen. To to Conversions->Export OpenSSH Key Save your private key as id_rsa (in .ssh directory, of course). (I am not sure the default name is required. Feel free to try something else and add a comment.)
Now paramiko will be able to automatically find it.
And here is my Resulting Script.
import sys
import crypto
sys.modules['Crypto'] = crypto
import paramiko
knownHosts = 'C:/Users/Skip Huffman/.ssh/known_hosts'
keyFileName = 'C:/Users/Skip Huffman/.ssh/id_rsa'
hostName = "mcsremotetest1.cnn.vgtf.net"
userName = "<username matching keypair>"
client = paramiko.SSHClient()
client.load_system_host_keys(knownHosts)
client.connect(hostName, username=userName)
stdin, stdout, stderr = client.exec_command('ls')
print "Standard Error: ", stderr.readlines()
print "Standard Output: ", stdout.readlines()
With a proper matching keypair this should now just work. It does for me. (substitute the proper username of course.)
来源:https://stackoverflow.com/questions/24514571/difficult-to-install-python-ssh-library-on-windows-ssh-has-been-replaced-with