Python uses the Paramiko module to write scripts for remote server operations.

Source: Internet
Author: User

Python uses the Paramiko module to write scripts for remote server operations.

Introduction:
Paramiko is a python (2.2 or higher) module that implements secure (encryption and authentication) connection to remote machines following the SSH2 protocol.
Install the required software package:
Http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.5.tar.gz
Http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz

tar zxvf pycrypto-2.5.tar.gzcd pycrypto-2.5python setup.py buildpython setup.py installtar zxvf paramiko-1.7.7.1.tar.gzcd paramiko-1.7.7.1python setup.py buildpython setup.py install

Simple script writing:
Manage a single server:

Script 1: query the disk usage of 172.16.22.23

#!/usr/bin/python import paramiko hostname="172.16.22.23" port=22 username="root" password="larryroot" if __name__=="__main__":     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command("df -Th")     print stdout.read()     s.close()


Script 2: Execute the corresponding command on the remote server

#!/usr/bin/python #by larry #2011/01/30 import sys import paramiko  hostname=sys.argv[1] command = " ".join(sys.argv[2:]) port=22 username="root" password="larryroot" if __name__=="__main__":     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command(command)     print stdout.read()     s.close()

Usage:

Python single1.py IP address command [root @ localhost ~] # Python single1.py 172.16.22.23 df-TH
Filesystem  Type   Size  Used Avail Use% Mounted on/dev/sda2   ext3   13G  6.0G  5.7G 52% //dev/sda1   ext3   104M  12M  87M 13% /boottmpfs    tmpfs   61M   0  61M  0% /dev/shm/dev/sda4   ext3   7.6G  465M  6.8G  7% /data/dev/sdb1   ext3   32G  5.9G  25G 20% /autocd[root@localhost ~]# python single1.py 172.16.22.23 free -mtotal    used    free   shared  buffers   cachedMem:      114    112     2     0     26     35-/+ buffers/cache:     50     64Swap:     1027     0    1027

Script 3: manage multiple servers: query the disk usage of the corresponding servers in the ip list in batches

#!/usr/bin/python #by larry #2011/01/30 import paramiko port=22 username="root" file=open("ip.list") for line in file:     hostname=str(line.split("\t")[1])     password=str(line.split("\t")[4]).strip()     print "##########################",hostname,"########################"     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command("df -Th")     print stdout.read()     s.close() file.close()

Usage:

[root@localhost ~]# python ssh.py
############################ 172.16.22.22 ########################Filesystem  Type  Size Used Avail Use% Mounted on/dev/sda2   ext3   12G 5.6G 5.3G 52% //dev/sda1   ext3   99M  12M  83M 13% /boottmpfs    tmpfs   58M   0  58M  0% /dev/shm/dev/sda4   ext3  7.1G 443M 6.3G  7% /data/dev/sdb1   ext3   30G 5.5G  23G 20% /autocd############################ 172.16.22.23 ########################Filesystem  Type  Size Used Avail Use% Mounted on/dev/sda2   ext3   15G 2.6G  11G 19% //dev/sda1   ext3   99M  12M  82M 13% /boottmpfs    tmpfs   60M   0  60M  0% /dev/shm/dev/sda4   ext3   33G 377M  31G  2% /data

Ip. list file content:

dx   172.16.22.22  22  root  larryrootwt   172.16.22.23  22  root  larryroot

Script 4: similar to script 2, execute corresponding commands on all remote servers

#!/usr/bin/python #by larry #2011/01/30 import paramiko import sys port=22 username="root" command = " ".join(sys.argv[1:]) file=open("ip.list") for line in file:     hostname=str(line.split("\t")[1])     password=str(line.split("\t")[4]).strip()     print "##################",hostname,"######################"     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command(command)     print stdout.read()     s.close() file.close()

 
Usage:

Python ssh. py command

Here, the paramiko module of python can be used to conveniently manage servers. The files will be uploaded and downloaded later.

SSH
The following shows how to log on to the server in batches using the ssh dsa or rsa public key to execute the command:

#! /Usr/bin/python #2012/02/02 by larry import paramiko import sys, OS port = 22 username = "larry" key_file = "~ /. Ssh/authorized_keys "know_host ="/home/larry /. ssh/known_hosts "command = "". join (sys. argv [1:]) #### obtain the command line parameter file = open ("ip. list ") for line in file: hostname = str (line. split ("") [1]) #### print the ip field "############################### ######", hostname, "####################################### ####### "s = paramiko. SSHClient () s. set_missing_host_key_policy (paramiko. autoAddPolicy () s. load_system_host_keys (know_host) s. connect (hostname, port, username, key_file) stdin,stdout,sterr;s.exe c_command (command) print stdout. read (). strip () s. close () file. close ()

 
Run the python script:

python sshkey.py df -h
################172.16.22.22########################Filesystem      Size Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0014G 3.5G 9.7G 27% //dev/mapper/VolGroup00-data116G  47G  64G 43% /data/dev/cciss/c0d0p1   99M  13M  82M 14% /boottmpfs         5.9G   0 5.9G  0% /dev/shm

Related Article

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.