首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python中的多个其他条件

python中的多个其他条件
EN

Stack Overflow用户
提问于 2018-04-19 09:00:04
回答 2查看 82关注 0票数 0

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

代码语言:javascript
运行
复制
##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 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-19 09:14:03

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

在以下代码段中出现了语法错误:

代码语言:javascript
运行
复制
"credentials = username:passowrd1" + "\n" + 

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

查找中断、继续和其他语句将非常有用,您可以在循环中使用它,就像我在回答中使用它们一样。

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

代码语言:javascript
运行
复制
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"
票数 0
EN

Stack Overflow用户

发布于 2018-04-19 09:12:19

您可以使用一个for循环和一个其他方法来完成这个任务:

代码语言:javascript
运行
复制
for password in list_of_password:
    ...
    "credentials = username:" + password + "\n"
    ...
    if '1 devices were successfully added/updated' in p1:
        ...
        break
else:
    print "Unsuccessfull"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49916887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档