Using SSH in Python requires openssh, while OpenSSH relies on Paramiko modules, and Paramiko modules rely on Pycrypto modules, so to use SSH in Python, you need to install the module order first: Pycrypto- > ECDSA-> Paramiko
1. Installation Pycrypto
Installing this is troublesome, requires local compilation, to install VS or GCC also has a heap of configuration, not necessarily compile successfully. (The installation steps can be found on the Internet)
It is recommended that you download the compiled version directly: http://www.voidspace.org.uk/python/modules.shtml
Download directly and install both.
(Note: Crypto has Java and C + + editions)
2. Installation ECDSA
Many blogs do not mention this library, but when I perform Paramiko, I am prompted not to find the ECDSA module.
Download: https://pypi.python.org/pypi/ecdsa/0.9, extract to a directory, there is a setup.py in the directory.
Installation is simpler, under Windows directly after the unpacked directory execution: Python setup.py install
3. Install Paramiko
With the install ECDSA type, just open the download page very slowly ...
Download: https://github.com/paramiko/paramiko#,
Installation Steps with ECDSA
Note: 1, all the other installed Third-party libraries, if not specified after the installation of the Library directory, will be saved by default to%python_home%\lib\site-packages.
2, Python is sensitive to case, the module name is also.
3, Crypto can provide common encryption and decryption algorithms, such as: RSA, RC4, DSA, DES
Test code:
Copy Code code as follows:
#!/usr/bin/python
#-*-Coding:utf-8-*-
# Cp@chenpeng.info
Import Paramiko
Def MAIN ():
Host = "10.1.1.1″
Port = 22
user = "Root"
PSWD = "111222333″
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect (host, port, user, PSWD)
stdin, stdout, stderr = Ssh.exec_command (' ifconfig ')
Print Stdout.read ()
Ssh.close ()
#
If __name__== ' __main__ ':
Try
MAIN ()
Except Exception,e:
Print E
The output is as follows:
At present, mainly for the bulk of the implementation of multiple server SSH command, batch maintenance is more convenient.
Here are two codes for connecting to a Linux server using Paramiko
Mode one:
Copy Code code as follows:
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect ("An IP address", 22, "username", "password")
The second line of code above is to allow connections to hosts that are not in the Know_hosts file
Mode two:
Copy Code code as follows:
t = Paramiko. Transport (("host", "Port")
T.connect (username = "username", password = "password")
If you need to provide a key to connect to a remote host, the second line of code above can be changed to:
Copy Code code as follows:
T.connect (username = "username", password = "password", hostkey= "key")
Example:
Copy Code code as follows:
#!/usr/bin/python
Import Paramiko
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect ("An IP address", 22, "username", "password")
stdin, stdout, stderr = Ssh.exec_command ("Your command") print Stdout.readlines ()
Ssh.close ()
Downloading files on a Linux server
Copy Code code as follows:
#!/usr/bin/python
Import Paramiko
t = Paramiko. Transport (("host", "Port")
T.connect (username = "username", password = "password")
SFTP = Paramiko. Sftpclient.from_transport (t)
Remotepath= '/var/log/system.log '
Localpath= '/tmp/system.log '
Sftp.get (RemotePath, LocalPath)
T.close ()
Uploading files to a Linux server
Copy Code code as follows:
#!/usr/bin/python
Import Paramiko
t = Paramiko. Transport (("host", "Port")
T.connect (username = "username", password = "password")
SFTP = Paramiko. Sftpclient.from_transport (t)
Remotepath= '/var/log/system.log '
Localpath= '/tmp/system.log '
Sftp.put (Localpath,remotepath)
T.close ()