如何在python中检查多个else条件

2024-09-30 06:23:39 发布

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

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

p = subprocess.Popen(["./linux_autodisc"], 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()
                        else: 
                             print "Unsuccessfull"

如何为5-6个密码包含多个else-to-run脚本,最后一个else应该返回trued all password而不是successful。你知道吗


Tags: 代码ip脚本密码代理linuxstdoutline
1条回答
网友
1楼 · 发布于 2024-09-30 06:23:39

为了保持代码更清晰,我建议您提取如下函数:

    def try_password(password):
      ``code that uses the password``

    for password in password_array:
       successful = try_password(password)
       if successful:
          break

else语句的堆积将使您的代码更难阅读/理解,并且会使将来的更改复杂化。你知道吗

这是我对你剧本的建议:

##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)

password_list = ["password1","password2"]

##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
                for password in password_list:
                    if try_password(password):
                        print ('Sucessfull Completed Ip: ' +line)
                        f6 = open("/root/nix_bsd_mac_inventory-master/SucessfullIp.txt","a")
                        f6.write("\n"+line)
                        f6.close()
                        break
                    else:
                        print "Unsuccessfull for {}".format(password) ## dont break and check others

## Extracted function to use password
## This could perhaps be optimized by extracting repetative tasks (depends on usage/performance)
def try_password(password):
    f3 = open("/root/nix_bsd_mac_inventory-master/inventory.cfg", "w")
    print "Processing Ip: " + line

    ##here we are giving credentials                
    write_string = "[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:{}"
    f3.write(write_string.format(password))
    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())
    return '1 devices were successfully added/updated' in p1

相关问题 更多 >

    热门问题