python中的多个else条件

2024-10-02 12:31:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python脚本从文件中读取每个IP并使用密码在该IP上安装代理,有5-6个密码,如果一个密码不起作用,它应该逐个尝试其他所有密码。 这是我的剧本:

##Reading values from SucessfullIp.txt
with open('/root/nix_bsd_mac_inventory-master/SucessfullIp.txt') as f:
    ips = set(line.rstrip() for line in f)

##Reading Unique Ip's values 
with open("/root/nix_bsd_mac_inventory-master/Unique.txt") as fp:
        for line in fp:
                line = line.rstrip()
                ## Comparing unique ip's if ip is already has scanned
                if line in ips:
                        print('{}: Ip is Already Tried: '.format(line))
                else:
                ##Creating inventory.cfg file on the fly for each ip
                        f3 = open("/root/nix_bsd_mac_inventory-master/inventory.cfg", "w")
                        print "Processing Ip: " + line
                        f3.write("[device42_access]"  + "\n" +
                        "base_url = https://1.8.0.3"  + "\n" +
                        "username = uname"  + "\n" +
                        "secret = abcd"  + "\n" +
                        "[discover]"  + "\n" +
                        "cpu= true"  + "\n" +
                        "hardware = true"  + "\n" +
                        "memory = true"  + "\n" +
                        "[access]"+ "\n" +
                        "credentials = username:passowrd1" + "\n" + ##here we are giving credentials and we have 5-6 passwords
                        f3.close()
                        p = subprocess.Popen(["./d42_linux_autodisc_v620"], stdout=subprocess.PIPE) ##This script will require inventory.cfg file created above
                        p1 = str(p.communicate())
                        if '1 devices were successfully added/updated' in p1:
                                print ('Sucessfull Completed Ip: ' +line)
                                f6 = open("/root/nix_bsd_mac_inventory-master/SucessfullIp.txt","a")
                                f6.write("\n"+line)
                                f6.close()
                        else:
                                print "Unsuccessfull"
                            ##here want it to check it with other passwords as well 

Tags: inipmastertxt密码macwithnix
2条回答

你应该迭代你的密码列表,如果一个成功的话就跳出循环。你知道吗

以下代码段中存在语法错误:

"credentials = username:passowrd1" + "\n" + 

这不应该以+结束,因为您没有将任何其他内容连接到字符串。你知道吗

查找可以与循环一起使用的break, continue, and else statements对您很有用,就像我在答案中使用它们一样。你知道吗

我已经删除了你的所有评论,并添加了我自己的评论来解释逻辑。你知道吗

with open("/root/nix_bsd_mac_inventory-master/Unique.txt") as fp:
    for line in fp:
        line = line.rstrip()
        if line in ips:
            print('{}: Ip is Already Tried: '.format(line))
            continue  # Continue means it will skip to the next password
        passwords = ['password1', 'password2', 'password3']
        for password in passwords:
            f3 = open("/root/nix_bsd_mac_inventory-master/inventory.cfg",
                      "w")
            print "Processing Ip: " + line
            f3.write("[device42_access]" + "\n" +
                     "base_url = https://1.8.0.3" + "\n" +
                     "username = uname" + "\n" +
                     "secret = abcd" + "\n" +
                     "[discover]" + "\n" +
                     "cpu= true" + "\n" +
                     "hardware = true" + "\n" +
                     "memory = true" + "\n" +
                     "[access]" + "\n" +
                     "credentials = username:" + password + "\n"  # Fixed typo here
            f3.close()
            p = subprocess.Popen(["./d42_linux_autodisc_v620"],
                                 stdout=subprocess.PIPE)
            p1 = str(p.communicate())
            if '1 devices were successfully added/updated' in p1:
                print('Sucessfull Completed Ip: ' + line)
                f6 = open("/root/nix_bsd_mac_inventory-master/SucessfullIp.txt", "a")
                f6.write("\n" + line)
                f6.close()
                break  # If successful it breaks, so don't need an else
            print "Password %s Unsuccessfull" % password
        else:
            # This happens when there are no more passwords to attempt
            print "No passwords were successful"

您可以使用for循环和单个else:

for password in list_of_password:
    ...
    "credentials = username:" + password + "\n"
    ...
    if '1 devices were successfully added/updated' in p1:
        ...
        break
else:
    print "Unsuccessfull"

相关问题 更多 >

    热门问题