以下代碼在秒組中查找0.0.0.0/0,並在鬆弛狀態下打印。如何從輸出中去掉unicode'u'? Python
def getTag(connection, instanceId):
reservations=connection.get_all_instances(filters={'instance_id':instanceId})
for res in reservations:
for instance in res.instances:
if instance.state == "running":
return instance.tags['Name'],instance.private_ip_address,instance.region
else:
return None
#regions = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1','ap-southeast-1','ap-southeast-2','ap-northeast-1']
#regions = ['us-west-2','eu-central-1','ap-southeast-1'] ##Prod VPC
regions = ['us-west-2']
data = []
header = ["Port","Open For","Security group","Instance Details"]
for region in regions:
connection=ec2.connect_to_region(region)
sg = connection.get_all_security_groups()
try:
for securityGroup in sg:
for rule in securityGroup.rules:
if '0.0.0.0/0' in str(rule.grants):
for instanceid in securityGroup.instances():
instanceId=str(instanceid)
# print instanceId
tag = getTag(connection, instanceId.split(':')[1])
if tag is not None:
# print tab
tab =[str(rule.to_port),"0.0.0.0/0",str(securityGroup.name),str(getTag(connection, instanceId.split(':')[1]))]
data.append(tab)
except Exception,e:
pass
print tabulate(data, headers=header, tablefmt='simple')
cool_stuff = tabulate(data, headers=header, tablefmt='simple')
slack.chat.post_message('#scanbot','```'+cool_stuff+'```',username='AWS_security_group_scanner')
的問題是,這個打印:
Port Open For Security group Instance Details
------ ---------- --------------------------------- ----------------------------------------------------------------------------------
1433 0.0.0.0/0 default (u'ABCcorp', u'10.17.9.22', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'dev-lb05', u'10.17.5.249', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'dev-sdl-lb02', u'10.17.5.63', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'lbCasio', u'10.17.5.11', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'lb01', u'10.17.4.196', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'lb01', u'10.17.5.133', RegionInfo:us-west-2)
80 0.0.0.0/0 pdx01-ms-dev-elbs (u'gehc-lb01', u'10.17.5.222', RegionInfo:us-west-2)
443 0.0.0.0/0 pdx01-ms-dev-elbs (u'lb05', u'10.17.5.249', RegionInfo:us-west-2)
我想因爲它們與鬆弛的東西搞亂擺脫的「U」。有人能幫我嗎 ?
嘗試打印字符串,而不是一個元組 –
使用'印刷海峽的(東西)',而不是'print something' – EbraHim
正如鏈接問題所寫,爲什麼不使用Python 3呢? – hyst329