python中的多个else条件
我有一个python脚本,它将读取文件中的每个IP,并使用密码在该IP上安装代理,有5-6个密码,如果一个密码不起作用,它应该逐个尝试使用其他所有密码。 这是我的剧本:python中的多个else条件,python,python-2.7,Python,Python 2.7,我有一个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 U
##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
您可以使用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"
你应该迭代你的密码列表,如果成功的话,你应该跳出这个循环 以下代码段中存在语法错误:
"credentials = username:passowrd1" + "\n" +
这不应以+
结尾,因为您没有将任何其他内容连接到字符串
这将有助于您查找可以与循环一起使用的内容,正如我在答案中使用的那样
我已经删除了你的所有评论,并添加了我自己的评论来解释逻辑
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"
在你的问题中没有问题。很抱歉,我如何才能包含多个其他密码来运行5-6个密码的脚本如果一个密码不起作用,它应该为其他密码运行,最后一个密码应该为所有密码运行。你可能希望对所有密码执行
循环,如果任何密码成功,你将中断else
您可能希望打印字符串“Unsuccessful”
。for else可能是一个好方法,但请将您的行固定在credential=…
list\u密码是数组?@AkashSarpate在Python中我们没有数组,只有列表后面是else条件:print(“{}:Ip已尝试:”。format(line))在脚本中,您在继续之后没有包含其他内容。请检查我上面的脚本,并确认如果ips:print(“{}:Ip已尝试:”.format(line))#继续其他:对于密码中的密码:f3=打开(“/root/nix\u bsd\u mac\u inventory-master/inventory.cfg”,“w”)print”使用第一个密码检查:“print”处理Ip:“+lineI我已根据您的建议进行了更改,出现错误:[root@10nix_bsd_mac_inventory-master]#python s2.py文件“s2.py“,密码中密码的第43行:^IndentationError:应为缩进块您没有正确复制它。这是有关缩进的语法错误。您应该修复缩进。缩进错误已修复,但由于其他条件,它正在按预期运行,您能检查我的上述脚本并给出您的输入吗