[Python] Python paramiko module Installation and Use

Source: Internet
Author: User
Introduction

You will find that the common solution requires the necessary configuration for the remote server. If there are only one or two remote servers, you should configure them one by one if there are n servers, or you need to use the code for the above operations, the above method is not very convenient.

Paramiko can be used to solve the above problems. Compared with the previous method, it only needs to install the corresponding software (Python and pycrypto) locally, and there is no configuration requirement for the remote server, complex connection operations are especially helpful for connecting multiple servers.

Install

There are two prerequisites for installing paramiko: Python and another module named pycrypto.

Standard Python modules are usually installed and only run in the root directory of the module:

Python setup. py build

Python setup. py install

Install pycrypto



wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz

tar -zxvf pycrypto-2.6.tar.gz

cd pycrypto-2.6/

python setup.py build && python setup.py install


Test:

Python> Import crypto

(Compile error: Error: Command 'gcc 'failed with exit status 1; this is because the python-dev software package is missing

Yum-y install Python-devel)

Paramiko Installation



wget http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz

tar xvzf paramiko-1.7.7.1.tar.gz

cd paramiko-1.7.7.1/

python setup.py build && python setup.py install


Crypto error: 'module' object has no attribute 'have _ decl_mpz_powm_sec'

Test:

Python> Import paramiko

(Crypto error: 'module' object has no attribute 'have _ decl_mpz_powm_sec'

Find/usr/lib/python2.7/Site-packages/crypto/util/number. py

Set if _ fastmath is not none and not _ fastmath. have_decl_mpz_powm_sec:

Commented out

# If _ fastmath is not none and not _ fastmath. have_decl_mpz_powm_sec:

)

Use 1. Execute the remote command:

[CC lang = "C"]

#! /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.exe c_command ("your command ")

Print stdout. readlines ()

Ssh. Close ()

[/CC]

2. upload files to remote

[CC lang = "C"]

#! /Usr/bin/Python

Import paramiko

T = paramiko. Transport ("an IP address", 22 ))

T. Connect (username = "username", password = "password ")

SFTP = paramiko. sftpclient. from_transport (t)

Remotepath = '/tmp/test.txt'

Localpath = '/tmp/test.txt'

SFTP. Put (localpath, remotepath)

T. Close ()

[/CC]

3. Remote File Download

[CC lang = "C"]

#! /Usr/bin/Python

Import paramiko

T = paramiko. Transport ("an IP address", 22 ))

T. Connect (username = "username", password = "password ")

SFTP = paramiko. sftpclient. from_transport (t)

Remotepath = '/tmp/test.txt'

Localpath = '/tmp/test.txt'

SFTP. Get (remotepath, localpath)

T. Close ()

[/CC]

Reproduced from: http://www.firefoxbug.net /? P = 1698

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.