I need to display the ip address after address: . Everything else needs to be trimmed. What code will most optimally solve my problem?
show interface PPPoE0
id: PPPoE0
index: 0
type: PPPoE
description: Internet (NetFriend)
interface-name: PPPoE0
link: up
connected: yes
state: up
mtu: 1400
tx-queue: 1000
address: 46.42.50.121
mask: 255.255.255.255
global: yes
defaultgw: yes
priority: 1000
security-level: public
auth-type: PAP, CHAP, MS-CHAP, MS-CHAPv2
remote: 46.42.48.1
uptime: 45562
session-id: 23430
fail: no
via: GigabitEthernet0/Vlan2
last-change: 45562.183918
(config)> exit
Core::Configurator: Bye.
You can use a simple Regular Expression to search for all matching IPs in a string.
The regex query for an IP Adress would be : (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})If you want to learn what the RegEx query does you can see the full explanation here.
The code would look something like
import re
text = """
show interface PPPoE0
id: PPPoE0
index: 0
type: PPPoE
description: Internet (NetFriend)
interface-name: PPPoE0
link: up
connected: yes
state: up
mtu: 1400
tx-queue: 1000
address: 46.42.50.121
mask: 255.255.255.255
..................
"""
regex_pattern_ip = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
ip = regex_pattern_ip.search(text)[0]
print(ip)
Hope this helped
I don't know about most optimal, but one way to do it is to split by lines, find the one containing address, and extract the text from it:
def getIp(text):
lines = text.split("\n")
for line in lines:
line = line.replace(" ").replace("\t")#in case it has newlines or spaces in front, not sure based off question
if line.startswith("address:"):
return line[8:]
raise Exception("Address line not found")
Maybe not optimal, but will work:
print(s[s.find("address: ") + 9: s.find("mask")].strip())
Related
Need some advises on how I can extract values from output execution of command. Snippet output below. The output generates so much info, but I just need to extract the value of machine, state and address as per snippet below.
I would like to have the output that have list of machine, state and address
machine state address
0 started 1.9.10.34
0/kvm/0 started 1.9.10.21
xxxxx xxxxxxx xxxxxxx
This is the code I used.
for line in stdout:
line = line.strip()
if not line:
continue
#if line.startswith("0"):
machine_id, state, instance_id = line.split()[0:3]
print(f"Machine ID: {machine_id}, State: {state}, Address: {address}")
f.write(f"Machine ID: {machine_id}, State: {state}, Address:
{address}\n")
Please advise how can I only extract the info related to machine, state and address only. Thank you.
I have a string:
[3016] - Device is ready...
[10ice is loading..13] - v3[3016] - Device is ready...
[1r 0.[3016] - Device is ready.
Everything except '[3016] - Device is ready...' is 'noise'
The key word here is "Device is ready"
3016 - timestamp in msec. I need to extract '3016' from string for further operations
Tried following:
if "Device is ready" in reply:
# set a pattern for extracting time from the result
found = re.findall("\[.*\]", reply)
# Cut timestemp from reply
x = [tm[1:-1] for tm in found]
in case the reply was 'clean' ([3016] - Device is ready...) it's ok, but if there is 'noise' in reply then it doesn't work. Can someone point me in the right direction or perhaps assist with the code? Thanks in advance
If there is a single key, and it should precede the marker Device is ready, you can capture the digits first.
\[(\d+)].*\bDevice is ready\b
The pattern matches:
\[(\d+)] Capture 1+ digits between square brackets in group 1
.* Match 0+ times any char
\bDevice is ready\b and then Device is ready
Regex demo | Python demo
import re
strings = [
"[3016] - Device is ready...",
"[10ice is loading..13] - v3[3017] - Device is ready...",
"[1r 0.[3018] - Device is ready.",
"[1r 0 - Device is ready. [3019]",
]
pattern = r"\[(\d+)].*\bDevice is ready\b"
for s in strings:
match = re.search(pattern, s)
if match:
print(match.group(1))
Output
3016
3017
3018
You should use a regex group () to extract the number. found will be a list of all the numbers found inside []:
if "Device is ready" in reply:
# set a pattern for extracting time from the result
found = re.findall("\[(\d+)\]", reply)
print(found[0])
What's tha best way to parse addresses like these with Node:
Address: 'Yaseen Burlingame Center, 1722 Gilbreth Rd, Burlingame, CA 94010, USA'
Address: 'Hub 925, 5341 Owens Ct, Pleasanton, CA 94588, USA'
Address: 'Young Ave Parking, Young Ave, Half Moon Bay, CA 94019, USA'
Without knowing anything more about exactly what you need to parse out the string, and based purely on the format of it, split() would be a good place to start.
const parts = address.split(',');
parts[0] // Yaseen Burlingame Center
parts[1] // 1722 Gilbreth Rd
...
Note - you would have some extra whitespace in each part that you can remove via trim()
I'm using snmp to go through a switch and get some info.
i use:
for searching Names :
Oid: iso.3.6.1.2.1.2.2.1.2
for searching a Single Vlan:
Oid: iso.3.6.1.4.1.9.9.68.1.2.2.1.2
What i want is for multiple Vlans what is the oid.
Does anyone knows?
The Cisco OID referred to in your post will give you the VLAN-id associated on each interface on the device. For example snmpwalk yields following (so you can get all VLANs associated with the interfaces on the device)
# snmpwalk -v2c -c public 172.23.219.36 1.3.6.1.4.1.9.9.68.1.2.2.1.2
..
SNMPv2-SMI::enterprises.9.9.68.1.2.2.1.2.436217344 = INTEGER: 101
Taking ifIndex from VLAN 101 one can find the associated interface
# snmpwalk -v2c -c public 172.23.219.36 1.3.6.1.2.1.2.2.1.2.436217344
..
IF-MIB::ifDescr.436217344 = STRING: Ethernet1/20
However if you are only interested in getting VLANs on the system you may want to consider looking at Q-BRIDGE-MIB (https://www.rfc-editor.org/rfc/rfc4363). Any of these OIDs will get all the VLANs on the system whether associated with an interface or not via Q-BRIDGE MIB (dot1qFdbTable)
snmpwalk -v2c -c public 172.23.219.36 1.3.6.1.2.1.17.7.1.2
SNMPv2-SMI::mib-2.17.7.1.2.1.1.2.1 = Counter32: 0
SNMPv2-SMI::mib-2.17.7.1.2.1.1.2.101 = Counter32: 0
SNMPv2-SMI::mib-2.17.7.1.2.1.1.2.201 = Counter32: 0
SNMPv2-SMI::mib-2.17.7.1.2.1.1.2.301 = Counter32: 0
I am trying to remove from a text file the following string as displayed by vim
^[[38;1H^[[K^[[7m71%^[[27m^[[38;1H^[[38;1H^[[K
in this text files i have 7m1000 entries
meaning
^[[38;1H^[[K^[[7m71%^[[27m^[[38;1H^[[38;1H^[[K
^[[38;1H^[[K^[[7m72%^[[27m^[[38;1H^[[38;1H^[[K
^[[38;1H^[[K^[[7m73%^[[27m^[[38;1H^[[38;1H^[[K ...
^[[38;1H^[[K^[[7m1000%^[[27m^[[38;1H^[[38;1H^[[K
I tried with cat/grep/sed..
I tried with the following script
def Process(data):
text = data.split()[0]
#print repr(text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
data.split()[0]= text
return data
Producing
:python Clo.py
IP: 138.42.153.194->10.132.136.42, protocol 6, [38;1H[K[7m86%[27m[38;1H[38;1H[KTCP: sport 3389, dport 58187, seq 978549389, ack 33554488, flags 0x0018 ( ACK PSH), urgent data 0, Flow fastpath, session 911218, wqe index 487973 packet 0x0x80000000416988e6, Packet info: len 107 port 17 interface 17 vsys 0, Packet from interface 256 forwarded to DP0 for tunnel encap
would it be possible to remove ["'\x1b[38;1H\x1b[K\x1b[7m######%\x1b[27m\x1b[38;1H\x1b[38;1H\x1b[KTCP:] directly from VI?
the solution for me was
:%s/^[.*^[//g