使用paramiko批量更改root密碼

要求:root密碼都一樣,提前建好一個存放IP的文件

#vim /home/remote_ssh.py

#!/usr/bin/env python

import sys #定義執行程序需要幾個參數的模塊
import paramiko #遠程ssh
import os #判斷文件是否存在的模塊
import getpass #讓用戶輸入密碼的模塊
import threading #多線程模塊

def remote_ssh(host,pwd,comm):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #回答yes
ssh.connect(host,username='root',password=pwd)
stdin,stdout,stderr = ssh.exec_command(comm)
out = stdout.read() #定義標準輸出
err = stderr.read() #定義錯誤輸出
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表示包含程序在內的參數個數
print "Usage:{0} ipfile 'command'".format(sys.argv[0])
sys.exit(1)
if not os.path.isfile(sys.argv[1]): #如果IP文件不存在
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' #修改root密碼爲88888888

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章