Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use python to manage servers

Shulou Source: shulou.com Published: 2022-06-02 11:23:42 09月15日 Update

#! / usr/bin/env python#coding:utf-8'''Paramiko install 1, install, download 1, download and install pycrypto-2.6.1.tar.gz (apt-get install python-dev), enter, python setup.py build [compile], python setup.py install [install]-"import Crypto 2, download and install paramiko-1.10.1.tar.gz, enter Python setup.py build [compiled] Python setup.py install [install]-- "import paramiko'''# specifies username password login import paramiko# instantiates an object ssh = paramiko.SSHClient () # verifies yes/nossh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) when ssh logs in # specifies the ip port password of the specified host ssh.connect ('192.168.1.108, 22,' alex', '123') # executes the command stdin, stdout Stderr = ssh.exec_command ('df') # read execution result print stdout.read () # disconnect ssh.close () # Login with key Private key login''before executing python, we now do some preparatory work on linux to generate the key ssh-keygen-t rsa remote copy own public key to specify the private key path to the other machine ssh-copy-id-I ~ / ssh/id_rsa.pub wupeiqi@192.168.159.129'''import paramiko# private_key_path =' / home/tom/.ssh/id_rsa'# to take out the private key key = paramiko.RSAKey.from_private_key_file ( Private_key_path) # the following is the same as logging in with a password ssh = paramiko.SSHClient () ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) ssh.connect ('182.92.219.96' 22, 'wupeiqi', pkey=key) stdin, stdout, stderr = ssh.exec_command (' df') print stdout.read () ssh.close ()

Tags: Login password key method user user name compiler host public key command instance other party object machine step port result path preparation work Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft OPPO Reno MariaDB Shulou Information