Skip to content

Instantly share code, notes, and snippets.

@hynek2001
Last active August 27, 2016 18:21
Show Gist options
  • Save hynek2001/903dd466603c525d25f925c964628ebd to your computer and use it in GitHub Desktop.
Save hynek2001/903dd466603c525d25f925c964628ebd to your computer and use it in GitHub Desktop.
script to filter interfaces
"""
lets assume,
1.
we have file elements.json in same directory as script below
structure of elements.json is :
[{"vendor": "HUAWEI Technology Co.,Ltd", "DisplayName": "AAAAAAAAA", "IP": "1.1.1.1."},]
element is NE40
2. we have jumpserver (ubuntu)
3. we have module paramiko & paramiko-expect installer ( pip install paramiko && pip install paramiko-expect)
4. following credentials are filled properly:
"""
routerusername="solsddsdm"
routeruserpassword="casdadsa"
routerprompt = ".*>$"
jumpserverip="5.5.5.5"
jumpserverprompt="\$ $"
jumpserverusername="sasdasdas"
jumpserverpassword="Atasdasd5"
"""
then we can call script python inter.py -e AAAAAAAAA -f ".*test.*"
and as result we get interfaces, where description will match regexp ".*test.*"
"""
import sys, getopt
import json
import os
import re
import traceback
import paramiko
from paramiko_expect import SSHClientInteraction
def main(ele="KIR-IPR-9306-02",fil="1/0/0"):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(dir_path+os.sep+'elements.json') as json_data:
d = json.load(json_data)
for el in d:
name=el['DisplayName']
if re.match(ele,name)!=None:
print("element {el} filter {fe} {na}".format(el=el['IP'],na=el['DisplayName'],fe=fil))
printInterfaces(el['IP'],fil)
def printInterfaces(ip,fil):
ip = ip
try:
# Create a new SSH client object
client = paramiko.SSHClient()
# Set SSH key parameters to auto accept unknown hosts
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the host
client.connect(hostname=jumpserverip, username=jumpserverusername, password=jumpserverpassword)
# Create a client interaction class which will interact with the host
interact = SSHClientInteraction(client, timeout=20, display=False)
interact.expect(jumpserverprompt, timeout=40)
interact.send("telnet {ipp}".format(ipp=ip))
interact.expect("Username:", timeout=30)
interact.send(routerusername)
interact.expect("Password:.*", timeout=40)
interact.send(routeruserpassword)
interact.expect(routerprompt, timeout=40)
interact.send("screen-length 0 temporary")
interact.expect(routerprompt, timeout=40)
interact.send("dis int desc")
interact.expect(routerprompt, timeout=30)
output = interact.current_output_clean
lines=output.split("\n")
for ll in lines:
rowItems=ll.split(" ")
if rowItems[0]=='' :
continue
interfaceid=""
interfacestatusphy=""
interfacestatusadm = ""
tmptt=[]
for tt in rowItems:
if tt !='':
tmptt.append(tt)
if(len(tmptt)==3):
tmptt.append("")
if(len(tmptt)<4):
continue
desc= tmptt[3].lower()
fil=fil.lower()
#print(tmptt,desc,fil)
if (re.match(fil,desc)!=None):
print(tmptt)
except Exception as es:
traceback.print_exc()
finally:
try:
client.close()
except:
traceback.print_exc()
if __name__ == '__main__':
myopts, args = getopt.getopt(sys.argv[1:], "e:f:")
element=""
filter=""
for o, a in myopts:
if o == '-e':
element = a
elif o == '-f':
filter = a
else:
print("Usage: %s -i input -o output" % sys.argv[0])
#element="KIR-IPR-9306-02"
#filter=".*kir.*"
main(ele=element,fil=filter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment