Paramiko Module of Python Standard Library Series

Keywords: ssh Python Linux sftp

Paramiko is a Python SSHv2 protocol that provides client and server functionality. Although it uses a Python C to extend low-level encryption paramiko itself is a pure Python interface around the concept of SSH networking.

Install Paramiko

pip3 install paramiko

After installation, enter the python interpreter import module. If the import succeeds, the installation succeeds or the installation fails.

What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
C:\Users\Administrator>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko

Connect using username and password

# Import paramiko module
>>> import paramiko
# Create SSHClient objects
>>> ssh = paramiko.SSHClient()
# If it's a new host connection, yes/no will appear, and AutoAddPolicy will automatically fill in yes's
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connecting servers
>>> ssh.connect(hostname='192.168.56.100', port=22, username='root', password='123456')
# Execution of orders
>>> stdin, stdout, stderr = ssh.exec_command('df -h')
# Get the right output
>>> result = stdout.read()
# Close the connection
>>> ssh.close()
# Obtained values
>>> result
b'Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        48G  3.3G   45G   7% /\ndevtmpfs        984M     0  984M   0% /dev\ntmpfs           993M     0  993M   0% /dev/shm\ntmpfs           993M  8.9M  984M   1% /run\ntmpfs           993M     0  993M   0% /sys/fs/cgroup\n/dev/sda1       197M  137M   60M  70% /boot\ntmpfs           199M     0  199M   0% /run/user/0\n'

Connecting with a key

We need to do ssh-key authentication before using the aspect connection of the key. The steps are as follows:

[root@linux-node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
[root@linux-node1 ~]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[root@linux-node1 ~]# chmod 600 ~/.ssh/authorized_keys

Then download / root/.ssh/id_rsa and copy it to the E: python-intensive-training directory system. Here is the paramiko connection script under windows.

#!/use/bin/env python
# _*_ coding: utf-8 _*_

import paramiko

# Files with specified keys
private_key = paramiko.RSAKey.from_private_key_file('E:\python-intensive-training\id_rsa')

# Create SSH objects
ssh = paramiko.SSHClient()

# Allow connections to hosts that are not in the know_hosts file, or a new machine will let you type yes/no when it connects.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connecting servers
ssh.connect(hostname='192.168.56.100', port=22, username='root', pkey=private_key)

# Execution of orders
stdin, stdout, stderr = ssh.exec_command('df -h')
"""
stdin: Standard input
stdout: standard output
stderr: Error Output
"""

# Get the correct result of command execution
result = stdout.read()

# Close the connection
ssh.close()

# Output execution results
print(str(result, encoding='utf-8'))

If you are still confused in the world of programming, you can join our Python learning button qun: 784758214 to see how our predecessors learned! Exchange experience! I am a senior Python development engineer, from basic Python script to web development, crawler, django, data mining and so on, zero-based to the actual project data have been collated. To every Python buddy! Share some learning methods and small details that need attention. Click to join us. python learner gathering place

File upload and download

#!/usr/bin/python

import paramiko

t = paramiko.Transport(('192.168.56.100', 22))

t.connect(username="root", password="123456")

sftp = paramiko.SFTPClient.from_transport(t)

# Remote directory
remotepath = '/tmp/id_rsa'

# Local files
localpath = 'id_rsa'

# Upload files
sftp.put(localpath, remotepath)

# Download files
# sftp.get(remotepath, localpath)

# Close the connection
t.close()

Posted by gjdunga on Tue, 20 Aug 2019 00:05:42 -0700