To change the root password in bulk using Paramiko

Source: Internet
Author: User

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



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.