批量服务器执行命令

#这里配置服务器ip列表文件名,文件要求每行一个IP地址
HostListName=”hostapilist.txt”

#运行python程序时候 getpass.getpass(‘password: ‘) 函数提示输入密码

#!/usr/bin/python 

import paramiko
import time
import getpass

#config zone
HostListName="hostapilist.txt"

#read ip 
fileHandle = open ( HostListName ) 
print fileHandle.read() 
fileHandle.close()  

#input server passwd
password = getpass.getpass('password: ')



for line in open( HostListName ): 

    print "HOST_IP_Address: %s" %(line)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=line, username='root', port=22, password='%s' % password)
    stdin, stdout, stderr =   ssh.exec_command(''' ip a |grep "inet " | grep -v "127.0.0.1" ''') 

    print stdout.read()
    ssh.close()
    time.sleep(2)
文档更新时间: 2020-04-17 15:14   作者:月影鹏鹏