2016-04-27 52 views
1

以下代碼在秒組中查找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」。有人能幫我嗎 ?

+0

嘗試打印字符串,而不是一個元組 –

+0

使用'印刷海峽的(東西)',而不是'print something' – EbraHim

+0

正如鏈接問題所寫,爲什麼不使用Python 3呢? – hyst329

回答

2

更改此

tab = [str(rule.to_port), "0.0.0.0/0", str(securityGroup.name), str(getTag(connection, instanceId.split(':')[1]))] 

tab = [str(rule.to_port), "0.0.0.0/0", str(securityGroup.name), tuple(list(i.encode('UTF8') for i in getTag(connection, instanceId.split(':')[1])[0:2]) + [getTag(connection, instanceId.split(':')[1])[2]])] 
+0

這工作正常,但最後'RegionInfo'丟失 –

+0

我以爲你不會想這(無需更改)被轉換成任何類型。 – AceLearn

+0

我的意思是我只是需要擺脫'u',你的代碼看起來很好,但是'RegionInfo'沒有在輸出中打印出來。其餘的都沒問題.. –

2

希望這個作品

import unicodedata 
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),unicodedata.normalize('NFKD', getTag(connection, instanceId.split(':')[1])).encode('ascii','ignore')] 
         data.append(tab) 
    except Exception,e: 
     pass 
+0

這是不按預期工作,只是突破了第一個循環..沒有數據沒有任何東西 –