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.*