Get the App
SLTechnology News&Howtos  ›  Servers  › 

Linux remotely logs in to ssh using the secret key on Python to execute shell

Shulou Source: shulou.com Published: 2022-06-02 11:56:32 09月17日 Update

Single login:

#! / bin/bash#-*-coding: utf-8-*-import paramikossh = paramiko.SSHClient () key = paramiko.AutoAddPolicy () ssh.set_missing_host_key_policy (key) pkey = paramiko.RSAKey.from_private_key_file ('/ root/.ssh/id_rsa') ssh.connect ('103.11.30.2, 22,' root', pkey, timeout=5) stdin, stdout Stderr = ssh.exec_command ('ls') print (stdout.read (). Decode ()) print (stderr.read ()) # close the connection ssh.close ()

Read multiple IP and log in line by line, as follows

#! / usr/bin/python#-*-coding: utf-8-*-import time,shutil Osimport paramikoappname = "root" ssh = paramiko.SSHClient () key = paramiko.AutoAddPolicy () ssh.set_missing_host_key_policy (key) pkey = paramiko.RSAKey.from_private_key_file ('/ root/.ssh/id_rsa') uptime = time.strftime ("% Y%m%d") warpath= "/ var/tmp/cslc/aw/update/" + uptimedef update_all_aw (): for line in open ("awip.conf"): print line ssh.connect (line, 22, appname Pkey, timeout=5) stdin, stdout, stderr = ssh.exec_command ('python / var/tmp/awpy/createcopy.py') print (stdout.read (). Decode () print (stderr.read ()) ssh.close () print ("+ next +") return

Awip.conf files can prevent multiple IP addresses.

Reference:

Log in to the host with python key

Pythonparamiko logs in to ssh through key files and has heard of sftp uploading files.

Python uses the paramiko library to remotely and securely connect to SSH

Tags: File login multiple key utf-8 login security host address multiple reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi macOS Shulou Technology NVidia