Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2012, 02:18 PM
amb1s1 amb1s1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 25 amb1s1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old August 11th, 2012, 03:48 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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

Reply With Quote
  #3  
Old August 11th, 2012, 05:49 PM
amb1s1 amb1s1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 25 amb1s1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old August 11th, 2012, 06:26 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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
>>> 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Running a for loop in pair using one list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap