环境:windows
语言:python3
#! python3
# -*- coding: utf-8 -*-
import sys
import os
import paramiko
import re
def ssh2(ip,username,passwd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=10)
ssh.close()
print("%s\tSUCCESS\n" %(ip))
except :
print("%s\tError\n" %(ip))
if __name__=='__main__':
#Chcp 65001
os.system('chcp 65001')
##print (sys.stdout.encoding)
#reload(sys)
#sys.setdefaultencoding('utf-8')
##print (sys.getdefaultencoding())
#sys.stdout.reconfigure(encoding='UTF-8')
##print("参数个数:%s\n" %(len(sys.argv)))
if len(sys.argv) < 2:
print("缺少参数:Bat CMD like【%s】\n" %('python test.py 测试环境服务器列表文件.txt'))
else:
print("脚本名:%s\n" %(str(sys.argv[0])))
print("服务器列表文件:%s\n" %(sys.argv[1]))
fileHandler = open (str(sys.argv[1]), "r", encoding='utf-8')
while True:
# Get next line from file
line = fileHandler.readline()
# If line is empty then end of file reached
if not line :
break;
strArr = re.split(r"[ ]+", line.strip())
username = strArr[1]
passwd = strArr[2]
ip = strArr[0]
des = "" #服务器描述
if len(strArr)>3:
des = strArr[3]
print(des)
ssh2(ip,username,passwd)
# Close Close idle
fileHandler.close()
打开IDLE,cmd中输入idle