#!/usr/bin/env python
import time
import paramiko
def send_cmd(conn,command):
conn.send(command + "\n")
time.sleep(1,0)
def get_output(conn):
return conn.recv(65535).decode("utf-8")
def main():
host_dict = {"R1": "show running-config | section vrf_definition",
"R2": "show running-config vrf",}
for hostname, vrf_cmd in host_dict.items();
conn_params = paramiko.SSHClient()
conn_params.set_missing_host_key_policy(paramiko.AutoAddPolicy)
conn_params.connect(
hostname = hostname, port22=22, username="pyuser",password="pypass",look_for_keys=False, allow_agent=False,)
conn = conn_params.invoke_shell()
time_sleep(1,0)
print(f"Logged into {get_output(conn).strip()} successfully")
commands = [vrt_cmd,]
concat_ouput = ""
for command in commands:
send_cmd(conn,command)
#print(get_ouput(conn)),printing the info is less valuable than wr #it
concat_ouput += get_output(conn)
conn.close()
print(f"Writing {hostname} facts to file")
with open(f"{hostname}_facts.txt","w") as handle:
handle.write(concat_output)
if __name__ == "__main__":
mian()
add breakpoint() underneath for command in commands
python get_write.py
type: command->step
Core IAC Concepts
State declaration, Abstraction, Version control. YAML format data
vrf_name: "POLICE"
route_import:
- "65000:1"
- "65000:2"
route-export:
- "65000:1"
In this case, we should only leave route target we need.
Multi-platform Abstraction, we need to keep in mind IOS-XE and IOS-XR templates are different. But the concept are universal.
Infrasture as code
# Define list of hosts, key-value pair yml file
---
host_list:
- name: R1
vrf_cmd: "show running-config | section vrf_def"
platform: ios
- name: R2
vrf_cmd: "show running-config vrf"
platform: iosxr
lets make another python file, I am literally dying right now..
def main():
with open("hosts.yml","r") as handle:
host_root = safe_load(handle)
for host in host_root["host_list"]:
with open(f"vars/{host['name']}_vrfs.yml","r") as handle:
vrfs = safe_load(handle)
#jijan2 programming
j2_env = Environment( loader = FileSystemLoader("."),trim_blocks=True,autoescape= True)
template = j2_env.get_template(
f"templates/paramiko/{host[platform']}_vpn.j2"
new_vrf_config = template.render(data=vrfs)