下面的脚本通过SSH
连接,并在hosts文件中执行CAT
命令并收集其中的所有行,之后我尝试将其分为两组IPS
和HOSTNAMES
数组,删除带有#
的注释中的所有内容,但这一在匹配regex时返回了组错误
脚本
#!/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime, date, time, timedelta
import socket
import paramiko
import sys
import re
# create script head
print ('-----------------------------------------------------------------------------------------')
print ('Initializing UP/DOWN script in: '+str(date.today()))
print ('-----------------------------------------------------------------------------------------')
Ips = {'192.168.0.254': {'customer' : 'server'}}
output = []
outfinally = []
arrayIPS = []
arrayHostnames = []
for ip in Ips:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, username='root', password="root")
output.append((ssh.exec_command('cat /opt/hosts.txt')[1]).read().decode())
ssh.close()
outfinally = [re.split(r'\s*#\s*',line) for line in output[0].splitlines()]
for rows in range(len(outfinally)):
reg = re.compile(r'^((1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))$')
arrayIPS.append(reg.match(outfinally[rows][0]).groups())
for rows in range(len(outfinally)):
reg = re.compile(r'^((?!^((1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))$).)*$')
arrayHostnames.append(reg.match(outfinally[rows][0]).groups())
for line in arrayIPS:
print(line)
输出
Traceback (most recent call last):
File "collect.py", line 40, in <module>
arrayHostnames.append(reg.match(outfinally[rows][0]).groups())
AttributeError: 'NoneType' object has no attribute 'groups'
hosts.txt
192.168.0.1 #SRVNET
192.168.0.253 #SRVDATA1
192.168.0.252 #SRVDATA2
SRV35 #SRVDATA3
目前没有回答
相关问题 更多 >
编程相关推荐