0

I have the following list

['200', '530', '540']

The size of the list is dynamic. It depends from the output of ssh_conn.rec. What I need to is to grab those values and run the following command for each value in the list

ssh_conn.send('show running-config crypto map | i 200\n')
ssh_conn.send('show running-config crypto map | i 530\n')
ssh_conn.send('show running-config crypto map | i 540\n')

I have the filling I can use loops but I am not entirely sure how to do it Below the code:

#!/usr/bin/env python

import paramiko
import time
import re

# Variables
host = xxxx = 'xxxxx'

# Create instance of SSHClient object
ssh = paramiko.SSHClient()

# Automatically add untrusted hosts
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Automatically add untrusted hosts
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# initiate SSH connection
ssh.connect('xxxxx', port=22, username='xxxx', password='xxxxx', look_for_keys=False, allow_agent=False)
print "SSH COnnection established with %s" % host

# Use invoke_shell to establish an 'interactive session'
ssh_conn = ssh.invoke_shell()
print "Interactive SSH session established"

print "Give the name of the 3PPartner\n"
partner = raw_input('>')

# Commands prompted
ssh_conn.send('\n')
ssh_conn.send('enable\n')
time.sleep(.5)
ssh_conn.send('xxxxx\n')
time.sleep(.5)
ssh_conn.send("terminal pager 0\n")
time.sleep(.5)
ssh_conn.send('show running-config crypto map | i ' + str(partner) + '\n')
time.sleep(1)

output = ssh_conn.recv(65535)
print output

crypto_list = re.findall("OUTSIDEMAP (\d+) match",output)
print crypto_list

Output

python IPSEC_config_attributes.py 
SSH COnnection established with XXXX
Interactive SSH session established
Give the name of the 3PPartner

>XXXX
Type help or '?' for a list of available commands.
XXXX/pri/act> 
XXXX/pri/act> enable
Password: ************
XXXX/pri/act# terminal pager 0
XXXX/pri/act# show running-config crypto map | i XXX
crypto map OUTSIDEMAP 200 match address XXXX
crypto map OUTSIDEMAP 530 match address XXXX
crypto map OUTSIDEMAP 540 match address XXXX
XXXX/pri/act# 
['200', '530', '540']
200
530
540
Logged out of device XXXXXXXX

Thanks

13
  • show more context code, show how ssh_conn was defined Commented Oct 27, 2017 at 15:03
  • Added into description Commented Oct 27, 2017 at 15:07
  • I don't see the output of a file from where you need to is to grab those values Commented Oct 27, 2017 at 15:14
  • Sorry, is not a file is ssh_conn.recv output. Updated the description Commented Oct 27, 2017 at 15:17
  • 1
    So what exactly prevens you from doing for i in [200, 540, 530]: then newline and the command ? Or you could use while my_list: loop and at the end of each loop pop first item from the list, kinda like circular buffer. If the list is dynamic, as you say, and the items added to the tail of the list, then it makes sense to use while loop. At least in my humble opinion Commented Oct 27, 2017 at 15:24

1 Answer 1

1

The programming concept for being able to do something multiple times to different values is iteration. In Python, you can easily iterate over many things, such as lists, lines in a file, or, in this case, regular expression matches.

In the simplest examples of iteration, you use the "for item in list" construct to have a variable, item, that receives each value in the list. To execute a command for each match, you would use something like this:

crypto_list = re.findall("OUTSIDEMAP (\d+) match",output)
for match in crypto_list:
    ssh_conn.send('show running-config crypto map | i ' + match + '\n')
Sign up to request clarification or add additional context in comments.

1 Comment

code-only answers are not always helpful. Please consider adding an explanation of what the code does and why it answers the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.