はじめに
SoftLayerでは、Standard Hardware FirewallやDedicated Hardware Firewallなどを購入することで、ユーザーのPublic VLANにパケットが届く前に、不正なアクセスをブロックすることができます。以下のような操作でFirewallのアクセスログを確認することができるのですが、、、これを1つ1つのIPに対して手動で実施していくのは大変です。。。。
ということで、IPアドレスを指定すれば、それに紐づいているアクセスログを出力するサンプルスクリプトを作ってみましたのでご紹介したいと思います。
サンプルコード
getSyslogsByIP.py
import SoftLayer
import sys
import pprint
from prettytable import PrettyTable
pp = pprint.PrettyPrinter(indent=4)
argv=sys.argv
if ( len(argv) != 2 ):
print("Usage: python %s <Your IP>." % argv[0])
exit(10)
targetip = argv[1]
LIMITSIZE=500
client = SoftLayer.create_client_from_env()
myip = client['Network_Subnet_IpAddress'].getByIpAddress(targetip)
if (myip is None) or (myip == "") :
print("You don't have %s in your account." % (targetip))
exit(20)
_offset=0
alllists=[]
while True:
lists = client['Network_Subnet_IpAddress'].getSyslogEventsSevenDays(id=myip['id'], limit=LIMITSIZE, offset=_offset)
alllists = alllists + lists
_offset = _offset + LIMITSIZE
if len(lists) < LIMITSIZE :
break
if (alllists ==[]):
print("No records for this IP")
exit(0)
alllists = sorted(alllists, key= lambda x: x['createDate'], reverse=False)
table = PrettyTable(['createDate',
'sourceIpAddress',
'sourcePort',
'destinationIpAddress',
'destinationPort',
'protocol',
'eventType'])
for list in alllists:
table.add_row([
list['createDate'],
list['sourceIpAddress'],
list['sourcePort'],
list['destinationIpAddress'],
list['destinationPort'],
list['protocol'],
list['eventType']
])
print(table)
使い方と出力結果
# python getSyslogsByIP.py <IP Address>
のように使ってください。このスクリプトだと、getSyslogEventsSevenDays
を使っているので、7日分のアクセスログが出力されます。もし1日分だけでよいのであれば、getSyslogEventsOneDay
を使えば良いでしょう。
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Subnet_IpAddress