The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Running a for loop in pair using one list
Discuss Running a for loop in pair using one list in the Python Programming forum on Dev Shed. Running a for loop in pair using one list Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 11th, 2012, 02:18 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 25
Time spent in forums: 4 h 25 m 28 sec
Reputation Power: 0
|
|
|
Running a for loop in pair using one list
Hi, I'm doing a script that will ssh into a Cisco routers and run 10 commands. I have one list with all of the router's ips, something like this:
Code:
d = ['sb21 192.168.1.1','sb22 192.168.1.15', 'sb23 192.168.1.1','sb24 192.168.1.17', 'sb25 192.168.1.22','sb22 192.168.1.35','sb26 192.168.1.40']
for i in d:
deviceName, ip = i.split()
print ('connecting to ' +deviceName+' '+ip)
#after this is the ssh script using pexpect and the script that send the command
This devices are locate in different location like: sb21 and sb22 are in NY and sb23 and sb24 are in Texas. I want to separate them base on location. Something like this:
Code:
d = ['sb21 192.168.1.1','sb22 192.168.1.15', 'sb23 192.168.1.1','sb24 192.168.1.17', 'sb25 192.168.1.22','sb22 192.168.1.35','sb26 192.168.1.40']
for i in d:
For location in ny
Statements
For location in texas
Statements
All of the locations have two routers. I want to able to run the script without having to do multiple loop for connection and sending command to the router. Thanks in advance. I'm new with scripting and in the last few weeks I learn a lot and I'm pretty sure that I need years to be comfortable with scripting.
Last edited by amb1s1 : August 11th, 2012 at 02:19 PM.
Reason: Wrong code tag
|

August 11th, 2012, 03:48 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
My suggestion:
Code:
d = ['sb21 192.168.1.1','sb22 192.168.1.15', 'sb23 192.168.1.1','sb24 192.168.1.17', 'sb25 192.168.1.22','sb22 192.168.1.35','sb26 192.168.1.40']
for i in d:
deviceName, ip = i.split(' ')
if deviceName in ['sb21', 'sb22']:
print ('New York')
elif deviceName in ['sb23', 'sb24']:
print ('Texas')
print ('connecting to ' +deviceName+' '+ip)
#after this is the ssh script using pexpect and the script that send the command
|

August 11th, 2012, 05:49 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 25
Time spent in forums: 4 h 25 m 28 sec
Reputation Power: 0
|
|
|
Thanks for your reply, but we are talking about a lot of devices. I have to run this script every Friday for 30 locations.
|

August 11th, 2012, 06:26 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Well I'll give it one more go
Code:
d = ['sb21 192.168.1.1','sb22 192.168.1.15', 'sb23 192.168.1.1','sb24 192.168.1.17', 'sb25 192.168.1.22','sb22 192.168.1.35','sb26 192.168.1.40']
NewYork = []
Texas = []
for i in d:
deviceName, ip = i.split(' ')
if deviceName in ['sb21', 'sb22']:
NewYork.append((deviceName, ip))
elif deviceName in ['sb23', 'sb24']:
Texas.append((deviceName, ip))
print(NewYork)
print(Texas)
for (d, ip) in NewYork:
print(ip)
# do something exciting..
for (d, ip) in Texas:
print(ip)
# do something exciting..
Output:
Code:
>>>
[('sb21', '192.168.1.1'), ('sb22', '192.168.1.15'), ('sb22', '192.168.1.35')]
[('sb23', '192.168.1.1'), ('sb24', '192.168.1.17')]
192.168.1.1
192.168.1.15
192.168.1.35
192.168.1.1
192.168.1.17
>>>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|