2017-10-27 14 views
0

출력의 값 내가 OUTSIDEMAP 후 숫자 값을 잡기 위해 필요파이썬 - 잡아 내 .send에서 paramiko 모듈에 파이썬 스크립트를 실행하고 있고 내가이 출력을 가지고 .recv

crypto map OUTSIDEMAP 10 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 20 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 21 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 23 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 25 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 26 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 27 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 430 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 550 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 660 match address XXXXXXXXXXX 
crypto map OUTSIDEMAP 800 match address XXXXXXXXXXX 

(10 , 20, 21 등). 분할 방식을 시도했지만 표시되는 행 수가 언제든지 변경 될 수 있으므로 목록 값 위치를 수정하지 않아도됩니다.

아이디어가 있으십니까? 코드

#!/usr/bin/env python 

import paramiko 
import time 

# Variables 
host = xxxxxx = 'xxxxxx' 

# 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('xxxxxx', port=22, username='xxxxx', 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('xxxxxxxn') 
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) 

# Edit output 
c_map = output.split(' ') 
print c_map[25] 
print c_map[31] 
print c_map[37] 


# Close session 
ssh.close() 
print 'Logged out of device %s' %host 
+0

왜 정규식을 피하려고합니까? – voiDnyx

+0

잊어 버려. 정규식으로 가기도 행복 –

답변

0

아래

는 항상 OUTSIDEMAP 후 필드인가? 이 경우, 분할()에 대한 당신의 생각은 다음과 같이 작동 할 수 있습니다 :

output = "crypto map OUTSIDEMAP 23 match address XXXXXXXXXXX" 
outputList = output.split(" ") 

index = outputList.index("OUTSIDEMAP") 

if index > -1: 
    numValue = outputList[index+1] 
    print(numValue) 

출력 :

23 

편집 :

함께 그 하나 개의 긴 문자열이, 당신이 사용할 수있는 경우 :

output = "crypto map OUTSIDEMAP 10 match address XXXXXXXXXXX\ 
crypto map OUTSIDEMAP 20 match address XXXXXXXXXXX\ 
crypto map OUTSIDEMAP 21 match address XXXXXXXXXXX\ 
crypto map OUTSIDEMAP 23 match address XXXXXXXXXXX" 


outputList = output.split(" ") 
indices = [i for i, x in enumerate(outputList) if x == "OUTSIDEMAP"] 

print([outputList[x+1] for x in indices]) 

출력 :

['10', '20', '21', '23'] 

열거 형은 인덱스와 값의 목록 튜플을 구성합니다. [(0,"crypto"),(1, "map)...]과 같이 목록 내 용은 x 값이 OUTSIDEMAP 인 경우 i으로 목록을 채우므로 발생 빈도가 OUTSIDEMAP 인 목록을 얻습니다. 그런 다음 1을 추가하여 찾고있는 번호가있는 입력란을 가져옵니다. 또한 정규식

:

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

출력 :

['10', '20', '21', '23'] 
+0

부분적으로 작동합니다. > XXXXXX 3PPartner 의 이름을 지정 를 설립 ~/github에 $ sudo는 파이썬 IPSEC_config_attributes.py SSH 연결이 10.42.19.58 대화 SSH 세션을 설정 : 내가 11 페데리코의 @의 페데리코이 있어야합니다 단지 첫 번째 값을 인쇄 로그 아웃 된 장치 xxxx –

+0

@Federi가 긴 문자열 하나를 사용하면 내 대답을 편집했습니다. – voiDnyx

+0

Yeap! 그것은 작동합니다! 너는 남자 야! 이제 코드의 의미를 이해해야합니다. D –