Automated O & M tools

Source: Internet
Author: User

Paramiko

The paramiko module is used to connect to a remote server and perform related operations based on SSH. This module can be used to perform commands or file operations on remote servers. It is worth noting that fabric and ansible use paramiko for internal remote management.

1. Install paramiko

pip3 install paramiko

2. Use paramiko

Sshclient

Import paramiko # create an SSH object SSH = paramiko. sshclient () # Allow connection to host SSH that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoaddpolicy () # connect to the server ssh. connect (hostname = 'c1 .salt.com ', Port = 22, username = 'wupeiqi', password = '000000') # Run stdin, stdout, stderr = ssh.exe c_command ('LS') # obtain the command result = stdout. read () # Close the SSH connection. close ()
Connect to a remote host based on the user name and password
Import paramiko private_key = paramiko. rsakey. from_private_key_file ('/home/auto /. SSH/id_rsa ') # create an SSH object SSH = paramiko. sshclient () # Allow connection to host SSH that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoaddpolicy () # connect to the server ssh. connect (hostname = 'c1 .salt.com ', Port = 22, username = 'wupeiqi', key = private_key) # Run stdin, stdout, stderr = ssh.exe c_command ('df ') # obtain the command result = stdout. read () # Close the SSH connection. close ()
Connect to a remote host based on the public key

 Sftpclient

Import paramiko transport = paramiko. transport ('hostname', 22) transport. connect (username = 'wupeiqi ', password = '000000') SFTP = paramiko. sftpclient. from_transport (Transport) # Set location. upload py to the server/tmp/test. pysftp. put ('/tmp/location. py', '/tmp/test. PY ') # download remove_path to the local local_pathsftp.get ('remove _ path', 'local _ path') transport. close ()
Upload Based on user name and password
Import paramiko private_key = paramiko. rsakey. from_private_key_file ('/home/auto /. SSH/id_rsa ') Transport = paramiko. transport ('hostname', 22) transport. connect (username = 'wupeiqi ', pkey = private_key) SFTP = paramiko. sftpclient. from_transport (Transport) # Set location. upload py to the server/tmp/test. pysftp. put ('/tmp/location. py', '/tmp/test. PY ') # download remove_path to the local local_pathsftp.get ('remove _ path', 'local _ path') transport. close ()
Upload Based on the key

3. Demo

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import paramikoimport uuidclass sshconnection (object): def _ init _ (self, host = '2017. 16.103.191 ', Port = 22, username = 'wupeiqi', Pwd = '000000'): Self. host = Host self. port = port self. username = username self. pwd = PWD self. _ k = none def create_file (Self): file_name = STR (UUID. uuid4 () with open (file_name, 'w') as F: f. write ('SB ') return file_name def run (Self): Self. connect () self. upload ('/home/wupeiqi/tttttttttttt. py') self. rename ('/home/wupeiqi/tttttttttttt. py', '/home/wupeiqi/ooooooooo. PY) self. close () def connect (Self): Transport = paramiko. transport (self. host, self. port) transport. connect (username = self. username, password = self. PWD) self. _ transport = transport def close (Self): Self. _ transport. close () def upload (self, target_path): # connect and upload file_name = self. create_file () SFTP = paramiko. sftpclient. from_transport (self. _ transport) # change location. upload py to the server/tmp/test. py sftp. put (file_name, target_path) def Rename (self, old_path, new_path): SSH = paramiko. sshclient () ssh. _ transport = self. _ transport # Run Command cmd = "Mv % S % s" % (old_path, new_path,) stdin, stdout, stderr = ssh.exe c_command (CMD) # obtain the command result = stdout. read () def cmd (self, command): SSH = paramiko. sshclient () ssh. _ transport = self. _ transport # execute the command stdin, stdout, stderr = ssh.exe c_command (command) # obtain the command result = stdout. read () return result HA = sshconnection () Ha. run ()
Demo

 

Automated O & M tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.