Requirement: The root password is the same. Create a file to store the IP in advance
#vim /home/remote_ssh.py
#! / usr / bin / env python
import sys #Define a module that requires several parameters to execute the program
import paramiko #remote ssh
import os #module to determine if the file exists
import getpass #module for users to enter password
import threading #Multithreaded module
def remote_ssh (host, pwd, comm):
ssh = paramiko.SSHClient ()
ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) #Answer yes
ssh.connect (host, username = ‘root’, password = pwd)
stdin, stdout, stderr = ssh.exec_command (comm)
out = stdout.read () #define standard output
err = stderr.read () #define error output
if out:
print "Host {0} output is: \ n {1}". format (host, out)
if err:
print "Host {0} error is: / n {1}". format (host, err)
ssh.close ()
if name == ‘main’:
if len (sys.argv)! = 3: # sys.argv represents the number of parameters including the program
print "Usage: {0} ipfile‘ command ’". format (sys.argv [0])
sys.exit (1)
if not os.path.isfile (sys.argv [1]): #If the IP file does not exist
print "No such file:", sys.argv [1]
sys.exit (2)
passwd = getpass.getpass("Please input password:")
ipfile = sys.argv[1]
command = sys.argv[2]
with open(ipfile) as fileobj:
for line in fileobj:
ip = line.strip()
t = threading.Thread(target=remote_ssh,args=(ip,passwd,command))
t.start()
#python /home/remote_ssh.py /home/ipaddr.txt ‘echo 88888888 | passwd --stdin root’ #Modify the root password to 88888888
Change root passwords in batch using paramiko