All Python

 #1. Write a Python script to extract all IP addresses from a given text using regular expressions.


input='''Router# show versionCisco Internetwork Operating System Software

IOS (tm) GS Software (RSP-JV-M), Experimental Version 11.1(12816)

[getchell 108]

Copyright (c) 1986-1996 by cisco Systems, Inc.

Compiled Mon 03-Jun-96 11:39 by getchell

Image text-base: 0x600108A0, data-base: 0x60910000

ROM: System Bootstrap, Version 5.3(16645) [szhang 571], INTERIM SOFTWARE

Router uptime is 4 minutes

System restarted by reload

System image file is "slot0:dirt/vip2/master/rsp-jv-mz.960603", booted via tftp from 172.18.2.3

cisco RSP2 (R4600) processor with 24576K bytes of memory.

R4600 processor, Implementation 32, Revision 2.0

Last reset from power-on

G.703/E1 software, Version 1.0.

SuperLAT software copyright 1990 by Meridian Technology Corp).

Bridging software.

X.25 software, Version 2.0, NET2, BFE and GOSIP compliant.

TN3270 Emulation software (copyright 1994 by TGV Inc).

Primary Rate ISDN software, Version 1.0.

Chassis Interface g0/0 129.0.0.1'''


import re

IPs=re.findall('\d+\.\d+\.\d+\.\d+',input)

print(IPs)


#2. Implement a function to validate an email address using regex.

import re

address='mailme@gmail.com'

def validateemail(address):

    pattern=r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+.[a-zA-Z]{2,}'

    match=re.match(pattern,address)

    return bool(match)

print(validateemail(address))


#3. Write a function to parse a log file and extract lines that contain a specific keyword.


a='''2025-04-04 10:59:00 INFO Starting application

2025-04-04 11:00:10 ERROR Failed to connect to server

2025-04-04 11:01:20 WARN Disk space running low

2025-04-04 11:02:30 INFO Application stopped'''

def logfile(a):

    with open('createfile.txt','w') as f :

        f.write(a)

    with open('createfile.txt','r') as f:

        readfile=f.readlines()

        print("File content is ",readfile)

        for line in readfile:

            pattern=r'ERROR Failed to connect to server'

            match=re.findall(pattern,line)

            if match:

                print(match[0])

            else:

                print("string not found")

logfile(a)



#4. Implement a function to split a string by multiple delimiters using regex.

import re

text="hello,welcome|to;world:Mine"

def delimiters(text):

    pattern=r'[,:;|]'

    filter=re.split(pattern,text)

    filter1=' '.join(filter)

    print(filter1)


delimiters(text)


#5. Write a function to find all URLs in a given text using regular expressions.


text="""

Check out these websites:

https://www.example.com

http://www.testsite.org

Visit our blog at https://blog.example.net for more information.

"""


def urls(text):

    pattern = r'[http|https]+://[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z]{2,}'

    matches=re.findall(pattern,text)

    print(matches)


urls(text)



6. #{'s': 1, 'u': 1, 'c': 1, 'h': 1, 'i': 1, 't': 1, 'r': 1, 'a': 1}

from collections import Counter

a='Suchitra Kaunar'

count=dict(Counter(a))

print(count)



char_count={}

for char in a:

    if char in char_count:

        char_count[char]=+1

    else:

        char_count[char]=1

print(char_count)

7. print("hello")
import re
s='mymac is  01:05:06:af:01:00'
print(s)
pattern=r'((?:[0-9a-zA-Z]{2}[:-]){5}[0-9a-zA-Z]{2})'
mac=re.findall(pattern,s)
print(mac[0])

8. 

l1=[100,5,10]
l2=[300,6,9,87]
print(sorted(l1+l2))


'''
print("hello world")
import re
a="I like the code in Python"
c=re.sub("like","love",a)
print(a,c)


0-127
128-191
192-224


input="""2: enX0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq_codel state UP group default qlen 1000 
link/ether 0a:c2:8f:e0:2e:65 brd ff:ff:ff:ff:ff:ff 
inet 172.31.15.248/20 metric 100 brd 172.31.15.255 scope global dynamic enX0 
valid_lft 3036sec preferred_lft 3036sec 
inet6 fe80::8c2:8fff:fee0:2e65/64 scope link 
valid_lft forever preferred_lft forever 
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:9f:d7:b4:1e brd ff:ff:ff:ff:ff:ff 
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 
valid_lft forever preferred_lft forever """
IP="172.31.15.248"
IPinlist=IP.split(".")
print(IPinlist)
firstoctet=int(IPinlist[0])
print(firstoctet)
if firstoctet>int(0) and firstoctet<int(127):
    print("This belongs to class A")
elif firstoctet>int(128) and firstoctet<int(191):
    print("This belongs to class B")
else:
    print("failed")

import random
def generateclassBrandomIPs(count=5):
    for i in range(count):
        f_octet=random.randint(128,191)
        s_octet=random.randint(0,255)
        t_octet=random.randint(0,255)
        f_octet=random.randint(0,255)
    print(f"{f_octet}.{s_octet}.{t_octet}.{f_octet}")

generateclassBrandomIPs()
    


a=("1","2")
b=[1,2]
c=[3,4]
print(b+c)

#BGP,OSPF,l3,ACL,SSE, IP sec- in which network or cases do you use BGp and why, explain all you could

Comments

Popular posts from this blog

TCL Interview Programs

Python Interview Programs

-: Networking interview questions :-