Operations 7 min read

Automate Linux Server Tasks with Python: A Quick‑Start Guide to Paramiko

This guide introduces the Python Paramiko library for SSH and SFTP, showing how to install it, create SSHClient and SFTPClient objects, execute remote commands, transfer files, and properly manage connections on Linux servers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Automate Linux Server Tasks with Python: A Quick‑Start Guide to Paramiko

Overview

Paramiko is a Python library implementing the SSHv2 protocol, built on the cryptography package. It provides two main high‑level classes: SSHClient for executing remote commands and SFTPClient for file transfer.

Using SSHClient

Install with pip install paramiko. Typical workflow:

Create an SSHClient instance.

Set a policy for unknown host keys (e.g., AutoAddPolicy).

Connect to the remote host with connect(), supplying hostname, port, username, password or key.

Execute a command with exec_command() and read stdout / stderr.

Close the client.

# -*- coding: utf-8 -*-
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='192.168.23.134', port=22,
               username='ftoz', password='123456')

stdin, stdout, stderr = client.exec_command('ls')
print(stdout.read().decode('utf-8'))

client.close()

connect() parameters: hostname – target address. port – default 22. username, password – password authentication. pkey or key_filename – private‑key authentication. timeout – optional TCP timeout. allow_agent, look_for_keys – enable SSH agent or key lookup in ~/.ssh. compress – enable compression.

set_missing_host_key_policy() options: AutoAddPolicy – automatically add unknown host keys to the local HostKeys object. WarningPolicy – issue a Python warning but accept the key. RejectPolicy – reject unknown keys (default).

Using SFTPClient

For file operations, obtain a Transport object, authenticate, then create an SFTPClient from it.

import paramiko

t = paramiko.Transport(('192.168.23.134', 22))
t.connect(username='ftoz', password='123456')
sftp = paramiko.SFTPClient.from_transport(t)

# Upload a local file
sftp.put('F:/S12312.txt', '/home/ftoz/zxc12312.txt')

# Download the same file
sftp.get('/home/ftoz/zxc12312.txt', 'F:/S12312.txt')

t.close()

Common SFTPClient methods: put(localpath, remotepath, callback=None, confirm=True) – upload a file; confirm triggers a stat() check. get(remotepath, localpath, callback=None) – download a file. mkdir(path, mode=511) – create a directory. remove(path) – delete a file. rename(oldpath, newpath) – rename a file or directory. stat(path) – retrieve file attributes. listdir(path='.') – list directory contents.

Always close the SSHClient or Transport after operations to release resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationSSHParamikoSFTP
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.