sshfindip Python版本,通过IP查找~/.ssh/config 主机配置项


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#根据主机ip查找openSSH配置文件~/.ssh/config对应主机配置项
#自用sshfindip命令python实现版本
#原始sshfindip为shell脚本实现,参看:/v/bin/sshfindip
import sys
import os
import re

sshConfigFile="~/.ssh/config"
sshConfigFile=os.path.expanduser(sshConfigFile)

if len(sys.argv)<2 or (len(sys.argv)==2 and (str(sys.argv[1]).lower()=="--help" or str(sys.argv[1]).lower()=="-h")):
	print("""
	/v/bin/sshfindip.py
	使用主机IP或部分IP关键字查找主机配置项信息,查找 ~/.ssh/config 文件内容.
	注:当用不完全IP关键字匹配时,会输出包含此IP关键字的多台主机信息

Usage:
        sshfindip.py [ipstr]
Example:
        sshfindip.py 192.168.1.1

	""")
	sys.exit(0)
else:
	ipstr=str(sys.argv[1]).strip()


ipstr=ipstr.replace(".","\.").replace("-","\-")
foundCount=0

with open(sshConfigFile,"r") as read_f:
	Tag=False
	findIp=False
	allHostInfo=[]
	HostInfo=[]
	for num,line in enumerate(read_f.readlines(),1):
		if Tag==True and re.match(r'[ ]*Host ',line,re.I):
			Tag=False
			if findIp==True:
				allHostInfo.append(HostInfo)
				findIp=False
		if Tag==False and re.match(r'Host .*',line,re.I):
			Tag=True
			HostInfo=[]
			HostInfo.append(line)
			continue
		if Tag==True:
			if re.match(r'.*[^#]HostName [ ]*'+ipstr+'.*',line,re.I):
				findIp=True
				foundCount+=1
			HostInfo.append(line)

for Host in allHostInfo:
	print("".join(Host))

if foundCount>1:
	print("共找到主机 %s 个"%foundCount)

print("\nsshfind执行完毕;Python版本:%s"%sys.version)