#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep 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
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep 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.
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    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
    >>>

IMN logo majestic logo threadwatch logo seochat tools logo