Getting Started with the Paramiko module in Python two: Log on to a bulk host with a profile

Source: Internet
Author: User

Before I wrote an article about the simple function of Paramiko module in Python, I will continue to introduce some usage of this module to you today.

Today, mainly using Python to read the configuration file to log in to the bulk of the host, and execute the shell command on the host, nonsense not to say, directly on the code, we can look at:

#!/usr/bin/env python    
import paramiko    
import os    
import datetime from    
configparser import Configparser    
configfile= ' Config.ini '
config=configparser ()    
config.read (configfile)    
hostname1= '. Join ( Config.get (' IP ', ' IPAddress '))    
address=hostname1.split (';')    
Print address    
username= ' root '
password= ' abc123 '
port=22
local_dir=
'/tmp/' remote_dir= ' /tmp/test/'
if __name__== "__main__": for IP in address    
        :    
                paramiko.util.log_to_file (' Paramiko.log ')    
                S=paramiko. Sshclient ()    
                s.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())    
                S.connect (hostname=ip,username=username,password=password)    
                stdin,stdout,stderr= S.exec_command (' free;ifconfig;df-h ')    
                print stdout.read ()    
                s.close ()

The contents of the Config.ini configuration file are attached below:

[IP]

IPAddress = 74.63.229.*;69.50.220.*

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.