SoFunction
Updated on 2024-11-19

Python paramiko Usage Code Summary

1、User name, password login mode

import paramiko
.log_to_file('') # Record log files
ssh = ()
try:
  ssh.set_missing_host_key_policy(())
  ('', username='work', password='***')
  cmd = 'ls' # Linux naming that needs to be implemented
  stdin, stdout, stderr = ssh.exec_command(cmd) # Structure after execution of the command
  print(())
  print(().decode())
except Exception as e:
  print("%s:%s" % (e.__class__, e))
finally:
  # Close
  ()

2、Security-free login method

import paramiko
ssh = ()
SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #Local key file path

try:
  key = .from_private_key_file(SSH_PRIVATE_KEY) # When there is no decryption code
  #key = .from_private_key_file(SSH_PRIVATE_KEY, password='******') # With decryption password.

  ssh.load_system_host_keys() # Authentication via known_hosts can be done with this, if known_hosts file is not defined you need to define known_hosts.
  #ssh.set_missing_host_key_policy(()) # Authentication via public (doesn't need to be present in the known_hosts file)
  (hostname='', port=22, username='root', pkey=key)
  stdin, stdout, stderr = ssh.exec_command("ps")
  # Get command results
  result = ()
  # Printout
  print(())
except Exception as e:
  print("%s:%s" % (e.__class__, e))
finally:
  # Close
  ()

Note: Methods for generating passwords

A. Enter the local ssh folder cd .ssh/

B. Using ssh-keygen to produce local public and private keys ssh-keygen

xueerhuan@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub

C. Copy the contents of the generated id_rsa.pub file to the target machine's .ssh/authorized_keys on the target machine, if there is no authorized_keys, create your own. But note that the permissions of authorized_keys are generally 600

Alternatively, public key copying can be accomplished locally with a single command. The user accessed after ssh-copy-id is the user to be supported for password-free login.

morra@ubuntu:~/.ssh$ ssh-copy-id "[email protected]"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/morra/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.42 (192.168.1.42)' can't be established.
ECDSA key fingerprint is SHA256:/ufx+/OLtdsYy7vsdk4KDu9xJsBp6zHonRAf2jjT0GI.
Are you sure you want to continue connecting (yes/no)? n^H
Please type 'yes' or 'no': yes
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Password:

Number of key(s) added: 1

Now try logging into the machine, with:  "ssh '[email protected]'"  and check to make sure that only the key(s) you wanted were added.

# Go to the target machine and check the authorized_keys file.
localhost:.ssh morra$ cat authorized_keys 

3、Password to upload files

import os
import paramiko
ssh = ()
SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #Local key file path
key = .from_private_key_file(SSH_PRIVATE_KEY)
.log_to_file('')

ssh = ()
ssh.set_missing_host_key_policy(())
('', username='root', password='***')
t = ssh.get_transport()
sftp = .from_transport(t)
d = ("", "/home/work/.ssh/")
print(d)

4、Upload files without password

import os
import paramiko
ssh = ()
SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #Local key file path
key = .from_private_key_file(SSH_PRIVATE_KEY)
.log_to_file('')
ssh = ()
ssh.set_missing_host_key_policy(())
(hostname='', port=22, username='root', pkey=key)
t = ssh.get_transport()
sftp = .from_transport(t)
d = ("", "/home/work/.ssh/")
print(d)

This is the whole content of this article.