{"id":426827,"date":"2024-07-22T15:01:29","date_gmt":"2024-07-22T15:01:29","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=426827"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=426827","title":{"rendered":"<span>Netmiko \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c\u0438 Cisco<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440.<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e\u00a0\u0431\u044b\u043b\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e\u00a0netmiko \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c\u0438 Cisco.<\/p>\n<p>\u042f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u0434\u0430\u043b\u044c\u0448\u0435 \u0432\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043e\u0442\u0434\u0435\u043b\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. (DSS digital site support \u043a\u0430\u043a\u00a0\u0438\u0445 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442)<\/p>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0432\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f?<\/p>\n<ul>\n<li>\n<p>DSS \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0438\u043b\u0438\u00a0\u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0442\u0435\u0440\u044b, \u0438\u043b\u0438\u00a0\u043d\u043e\u0432\u044b\u0435 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u043c\u0435\u0440\u044b \u043a\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c \u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>DSS \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u00a0\u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u0434\u043b\u044f\u00a0\u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0440\u0442\u043e\u0432 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 Cisco \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0440\u0442\u043e\u0432 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 Cisco \u0432\u00a0\u0440\u0435\u0436\u0438\u043c\u0435 access \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 User vlan \u0438\u043b\u0438\u00a0Printer vlan.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435, \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0440\u0442\u044b, \u043d\u043e\u00a0\u0443\u00a0DSS \u043d\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0434\u043b\u044f\u00a0\u043a\u0430\u043a\u0438\u0445 vlan \u044d\u0442\u0438 \u043f\u043e\u0440\u0442\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 DSS \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u00a0\u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b.<\/p>\n<p>\u041c\u043e\u0451 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442:<\/p>\n<ol>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0442\u0447\u0451\u0442\u0430 \u043e\u00a0\u0432\u0441\u0435\u0445 \u043f\u043e\u0440\u0442\u0430\u0445 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u043e\u0432 Cisco \u0432\u00a0\u0432\u0438\u0434\u0435 excel \u0444\u0430\u0439\u043b\u0430 \u0438 \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0443 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u0447\u0451\u0442\u0430 \u0432\u00a0\u043e\u0442\u0434\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438. <\/p>\n<p>\u0418\u043c\u0435\u044f \u0442\u0430\u043a\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0432\u0438\u0434\u044f\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u043f\u043e\u0440\u0442\u044b \u0432\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 \u0438 \u0437\u043d\u0430\u044e\u0442 \u0447\u0442\u043e\u00a0\u043f\u043e\u0440\u0442\u044b \u0432\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c vlan. <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u043e \u043d\u0430\u00a0python \u0438 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438\u00a0\u043a\u0430\u0436\u0434\u0443\u044e \u043d\u043e\u0447\u044c \u043f\u043e\u00a0cron, \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0437\u00a0jenkins. \u0412\u00a0jenkins \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043d\u043e\u043f\u043a\u0430 \u00ab\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0447\u0435\u0442\u00bb. <\/p>\n<\/li>\n<li>\n<p>\u0421\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442 DSS \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c Excel \u0444\u0430\u0439\u043b \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 vlan \u043d\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0445 \u043f\u043e\u0440\u0442\u0430\u0445 \u0438 \u043e\u0442\u043e\u0441\u043b\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432 jenkins \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0440\u0430\u0437\u0443 \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 vlan \u043d\u0430 \u043d\u0443\u0436\u043d\u044b\u0445 \u043f\u043e\u0440\u0442\u0430\u0445. \u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d. \u042d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c vlan \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 access \u043f\u043e\u0440\u0442\u0430\u0445. \u041f\u043e\u0440\u0442\u044b trunk \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 jenkins, \u0442\u043e \u044d\u0442\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043f\u043b\u044e\u0441 \u043b\u043e\u0433\u0438, \u043a\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b, \u043a\u043e\u0433\u0434\u0430 \u0438 \u043a\u0430\u043a\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. <\/p>\n<p>\u0427\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e? \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 linux, ansible, python, netmiko, inventory file ansible \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 yaml. <\/p>\n<p>\u0418 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435 \u0441\u0432\u0438\u0447\u0435\u0439 \u0438\u0437 inventory file. <\/p>\n<p>\u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 inventory file ansible:<\/p>\n<pre><code>all:   vars:     ansible_user: admin     ansible_password: admin     ansible_connection: ansible.netcommon.network_cli     ansible_network_os: ios     ansible_become: yes     ansible_become_method: enable     ansible_become_password: cisco     ansible_host_key_auto_add: yes core_switch:   hosts:     core_switch1:       ansible_host: 192.168.38.141     core_switch2:       ansible_host: 192.168.38.142 sw:   hosts:     access_switch3:       ansible_host: 192.168.38.143     access_switch4:       ansible_host: 192.168.38.144     access_switch5:       ansible_host: 192.168.38.145     access_switch6:       ansible_host: 192.168.38.146     access_switch7:       ansible_host: 192.168.38.147<\/code><\/pre>\n<p>\u0412\u043e\u0442 python \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u043e \u0432\u0441\u0435\u043c \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c \u0438\u0437\u00a0\u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 \u00abshow interface status\u00bb \u00abshow cdp neighbor\u00bb<\/p>\n<pre><code class=\"python\">#!\/usr\/bin\/python3  import yaml import argparse from netmiko import ConnectHandler import csv import subprocess  # Function to parse command-line arguments def parse_arguments():     parser = argparse.ArgumentParser(description='Netmiko Script to Connect to Routers and Run Commands')     parser.add_argument('--hosts_file', required=True, help='Path to the Ansible hosts file')     parser.add_argument('--group', required=True, help='Group of routers to connect to from Ansible hosts file')     return parser.parse_args()  def ping_ip(ip_address):   # Use ping command to check if it alive     param = '-c' # for linux os     # Build the command     command = ['ping', param, '1', ip_address]     try:         # Execute the command         subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)         return \"yes\"     except subprocess.CalledProcessError:         return \"no\"   # Main function def main():     # Parse command-line arguments     args = parse_arguments()     # Load the hosts file     with open(args.hosts_file, 'r') as file:         hosts_data = yaml.safe_load(file)     # Extract global variables     global_vars = hosts_data['all']['vars']     # Extract router details for the specified group     if args.group not in hosts_data:         print(f\"Group {args.group} not found in hosts file.\")         return     routers = hosts_data[args.group]['hosts']     comm1='sho int statu | beg Port'     comm2='sho cdp nei | beg Device'      output_filed = args.group + '_inter_des.csv' #      output_filec = args.group + '_inter_cdp.csv' #     STRd = \"Hostname,IP_address,Interface,State,Description,Vlan\"  #      with open(output_filed, \"w\", newline=\"\") as out_filed:         writer = csv.writer(out_filed)         out_filed.write(STRd)         out_filed.write('\\n')     STRc = \"Hostname,IP_address,Interface,New_Description\"  # with ip     with open(output_filec, \"w\", newline=\"\") as out_filec:         writer = csv.writer(out_filec)         out_filec.write(STRc)         out_filec.write('\\n')    # Connect to each router and execute the specified command     for router_name, router_info in routers.items():         if ping_ip(router_info['ansible_host']) == \"no\":   # check if host alive              print( ' offline --------- ', router_name,'  ',router_info['ansible_host'])             continue         else:              print( '  online --------- ', router_name,'  ',router_info['ansible_host'])         # Create Netmiko connection dictionary         netmiko_connection = {             'device_type': 'cisco_ios',             'host': router_info['ansible_host'],             'username': global_vars['ansible_user'],             'password': global_vars['ansible_password'],             'secret': global_vars['ansible_become_password'],         }          # Establish SSH connection         connection = ConnectHandler(**netmiko_connection)         # Enter enable mode         connection.enable()         # Execute the specified command         outputd1 = connection.send_command(comm1)         outputd2 = connection.send_command(comm2)         # Print the output         print(f\"  ------------ Output from {router_name} ({router_info['ansible_host']}):\")         print(f\" \")         lines = outputd1.strip().split('\\n')         lines = lines[1:]         for line in lines:             swi=router_name             ipad= router_info['ansible_host']             por=line[:9].replace(' ', '')         # port             sta =  line[29:41].replace(' ', '')    # interface connected or notconnected             des =  line[10:28].replace(' ', '')    # existing description             vla = line[42:46].replace(' ', '')     # vlan             print(\"switch \",swi,\" port \",por, 'state ',sta,\" Descr \",des,\" vlan \", vla )             STR = swi + \",\" + ipad + \",\" + por +\",\" + sta +\",\" + des + \",\" + vla # +\",\"  # with ip             with open(output_filed, 'a') as f:                 f.write(STR)                 f.write('\\n')         lines1 = outputd2.strip().split('\\n')         lines1 = lines1[1:]  # This correctly removes the first line (header)         filtered_lines =  lines1         try:             first_empty_index = filtered_lines.index('')             # Keep only the lines before the first empty line             filtered_lines = filtered_lines[:first_empty_index]         except ValueError:             # No empty line found, do nothing             pass         lines1 = filtered_lines        # cleaned_text         print(' filtered_lines ', filtered_lines)         for line in lines1:             rlin1 =  line[:16]             dot_position = rlin1.find('.')             rlin2 = rlin1[:dot_position]     # remove domain name from name             rlin =  rlin2 + '|' + line[58:67] + '|' + line[68:]             ndes = rlin.replace(' ', '')   # remove all spaces             por=line[17:33]             por1 = por[0:2]+por[3:33]   # remove 3rd char from port name             por=por1.replace(' ', '')             swi=router_name             ipad= router_info['ansible_host']             print(\"switch \",swi,\" port \",por, \" Descr \", ndes )             STRc = swi + \",\" + ipad + \",\" + por +\",\" + ndes  # with ip             with open(output_filec, 'a') as f:                 f.write(STRc)                 f.write('\\n')         print(f\"  ------------ end\")         connection.disconnect()    # Disconnect from device     output_filem = args.group + '_merg.csv' #     with open(output_filed, mode='r') as file:         reader = csv.DictReader(file)         sw_inter_des_data = list(reader) # Read the sw_inter_cdp.csv file into a list of dictionaries     with open(output_filec, mode='r') as file:         reader = csv.DictReader(file)         sw_inter_cdp_data = list(reader) # Create a lookup dictionary for sw_inter_cdp_data based on Hostname, IP_address, and Interface     cdp_lookup = {         (row['Hostname'], row['IP_address'], row['Interface']): row['New_Description']         for row in sw_inter_cdp_data     } # Add the New_Description to sw_inter_des_data     for row in sw_inter_des_data:         key = (row['Hostname'], row['IP_address'], row['Interface'])         row['New_Description'] = cdp_lookup.get(key, '')      # Write the updated data to a new CSV file     with open(output_filem, mode='w', newline='') as file:         fieldnames = sw_inter_des_data[0].keys()         writer = csv.DictWriter(file, fieldnames=fieldnames)         writer.writeheader()         writer.writerows(sw_inter_des_data)     print(\"New CSV file with added New_Description column has been created as \", args.group , '_merg.csv') # Entry point of the script if __name__ == '__main__':     main() <\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 csv \u0444\u0430\u0439\u043b: <\/p>\n<pre><code class=\"yaml\">Hostname,IP_address,Interface,State,Description,Vlan,New_Description access_switch3,192.168.38.143,Gi0\/0,connected,PORT00,1,R3725|3725|Fas0\/0 access_switch3,192.168.38.143,Gi0\/1,connected,PORT11,1, access_switch3,192.168.38.143,Gi0\/2,connected,002,1, access_switch3,192.168.38.143,Gi0\/3,connected,003,1, access_switch3,192.168.38.143,Gi1\/0,connected,sw2|Gig0\/0,1,sw2||Gig0\/0 access_switch3,192.168.38.143,Gi1\/1,connected,011,20, access_switch3,192.168.38.143,Gi1\/2,connected,12_012345678901123,22, access_switch3,192.168.38.143,Gi1\/3,connected,13_012345678901234,23, access_switch4,192.168.38.144,Gi0\/0,connected,sw1|Gig1\/0,1,sw1||Gig1\/0 access_switch4,192.168.38.144,Gi0\/1,connected,,1, access_switch4,192.168.38.144,Gi0\/2,connected,,1, access_switch4,192.168.38.144,Gi0\/3,connected,,1, access_switch4,192.168.38.144,Gi1\/0,connected,,1, access_switch4,192.168.38.144,Gi1\/1,connected,,1, access_switch4,192.168.38.144,Gi1\/2,connected,,1, access_switch4,192.168.38.144,Gi1\/3,connected,,1, <\/code><\/pre>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c\u0438 mac address, ip address, vendor, lldp neighbor, uptime, downtime \u0438 \u0434\u0440. \u0415\u0441\u043b\u0438 \u0443\u00a0\u0432\u0430\u0441 \u0435\u0441\u0442\u044c Cisco Call Manager \u0438 IP \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u044b \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u043c \u0441\u00a0\u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0430, \u0447\u0442\u043e\u00a0\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u0442 \u043f\u043e\u0438\u0441\u043a \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432.<\/p>\n<p>\u042d\u0442\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0430\u00a0\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0430\u0434\u0438\u0438, \u044f \u043d\u0435\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u043d\u0430\u00a0\u0441\u0442\u0435\u043a\u043e\u0432\u044b\u0445 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445, \u0443\u00a0\u043c\u0435\u043d\u044f \u0438\u0445 \u043d\u0435\u0442 \u043f\u043e\u0434\u00a0\u0440\u0443\u043a\u043e\u0439, \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u00a0\u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 Cisco. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u043e\u0432 Juniper \u0438 Aruba.<\/p>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u043b\u044e\u0431\u044b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/830558\/\"> https:\/\/habr.com\/ru\/articles\/830558\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440.<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e\u00a0\u0431\u044b\u043b\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e\u00a0netmiko \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c\u0438 Cisco.<\/p>\n<p>\u042f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u0434\u0430\u043b\u044c\u0448\u0435 \u0432\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043e\u0442\u0434\u0435\u043b\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. (DSS digital site support \u043a\u0430\u043a\u00a0\u0438\u0445 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442)<\/p>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0432\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f?<\/p>\n<ul>\n<li>\n<p>DSS \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0438\u043b\u0438\u00a0\u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0442\u0435\u0440\u044b, \u0438\u043b\u0438\u00a0\u043d\u043e\u0432\u044b\u0435 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u043c\u0435\u0440\u044b \u043a\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c \u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>DSS \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u00a0\u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u0434\u043b\u044f\u00a0\u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0440\u0442\u043e\u0432 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 Cisco \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0440\u0442\u043e\u0432 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 Cisco \u0432\u00a0\u0440\u0435\u0436\u0438\u043c\u0435 access \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 User vlan \u0438\u043b\u0438\u00a0Printer vlan.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435, \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0440\u0442\u044b, \u043d\u043e\u00a0\u0443\u00a0DSS \u043d\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0434\u043b\u044f\u00a0\u043a\u0430\u043a\u0438\u0445 vlan \u044d\u0442\u0438 \u043f\u043e\u0440\u0442\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 DSS \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u00a0\u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b.<\/p>\n<p>\u041c\u043e\u0451 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442:<\/p>\n<ol>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0442\u0447\u0451\u0442\u0430 \u043e\u00a0\u0432\u0441\u0435\u0445 \u043f\u043e\u0440\u0442\u0430\u0445 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u043e\u0432 Cisco \u0432\u00a0\u0432\u0438\u0434\u0435 excel \u0444\u0430\u0439\u043b\u0430 \u0438 \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0443 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u0447\u0451\u0442\u0430 \u0432\u00a0\u043e\u0442\u0434\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438. <\/p>\n<p>\u0418\u043c\u0435\u044f \u0442\u0430\u043a\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0432\u0438\u0434\u044f\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u043f\u043e\u0440\u0442\u044b \u0432\u00a0\u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u0445 \u0438 \u0437\u043d\u0430\u044e\u0442 \u0447\u0442\u043e\u00a0\u043f\u043e\u0440\u0442\u044b \u0432\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c vlan. <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u043e \u043d\u0430\u00a0python \u0438 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438\u00a0\u043a\u0430\u0436\u0434\u0443\u044e \u043d\u043e\u0447\u044c \u043f\u043e\u00a0cron, \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0437\u00a0jenkins. \u0412\u00a0jenkins \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043d\u043e\u043f\u043a\u0430 \u00ab\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0447\u0435\u0442\u00bb. <\/p>\n<\/li>\n<li>\n<p>\u0421\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442 DSS \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c Excel \u0444\u0430\u0439\u043b \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 vlan \u043d\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0445 \u043f\u043e\u0440\u0442\u0430\u0445 \u0438 \u043e\u0442\u043e\u0441\u043b\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432 jenkins \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0440\u0430\u0437\u0443 \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 vlan \u043d\u0430 \u043d\u0443\u0436\u043d\u044b\u0445 \u043f\u043e\u0440\u0442\u0430\u0445. \u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043e\u0442\u0434\u0435\u043b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d. \u042d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c vlan \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 access \u043f\u043e\u0440\u0442\u0430\u0445. \u041f\u043e\u0440\u0442\u044b trunk \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 jenkins, \u0442\u043e \u044d\u0442\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043f\u043b\u044e\u0441 \u043b\u043e\u0433\u0438, \u043a\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b, \u043a\u043e\u0433\u0434\u0430 \u0438 \u043a\u0430\u043a\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. <\/p>\n<p>\u0427\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e? \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 linux, ansible, python, netmiko, inventory file ansible \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 yaml. <\/p>\n<p>\u0418 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435 \u0441\u0432\u0438\u0447\u0435\u0439 \u0438\u0437 inventory file. <\/p>\n<p>\u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 inventory file ansible:<\/p>\n<pre><code>all:   vars:     ansible_user: admin     ansible_password: admin     ansible_connection: ansible.netcommon.network_cli     ansible_network_os: ios     ansible_become: yes     ansible_become_method: enable     ansible_become_password: cisco     ansible_host_key_auto_add: yes core_switch:   hosts:     core_switch1:       ansible_host: 192.168.38.141     core_switch2:       ansible_host: 192.168.38.142 sw:   hosts:     access_switch3:       ansible_host: 192.168.38.143     access_switch4:       ansible_host: 192.168.38.144     access_switch5:       ansible_host: 192.168.38.145     access_switch6:       ansible_host: 192.168.38.146     access_switch7:       ansible_host: 192.168.38.147<\/code><\/pre>\n<p>\u0412\u043e\u0442 python \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u043e \u0432\u0441\u0435\u043c \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u0430\u043c \u0438\u0437\u00a0\u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 \u00abshow interface status\u00bb \u00abshow cdp neighbor\u00bb<\/p>\n<pre><code class=\"python\">#!\/usr\/bin\/python3  import yaml import argparse from netmiko import ConnectHandler import csv import subprocess  # Function to parse command-line arguments def parse_arguments():     parser = argparse.ArgumentParser(description='Netmiko Script to Connect to Routers and Run Commands')     parser.add_argument('--hosts_file', required=True, help='Path to the Ansible hosts file')     parser.add_argument('--group', required=True, help='Group of routers to connect to from Ansible hosts file')     return parser.parse_args()  def ping_ip(ip_address):   # Use ping command to check if it alive     param = '-c' # for linux os     # Build the command     command = ['ping', param, '1', ip_address]     try:         # Execute the command         subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)         return \"yes\"     except subprocess.CalledProcessError:         return \"no\"   # Main function def main():     # Parse command-line arguments     args = parse_arguments()     # Load the hosts file     with open(args.hosts_file, 'r') as file:         hosts_data = yaml.safe_load(file)     # Extract global variables     global_vars = hosts_data['all']['vars']     # Extract router details for the specified group     if args.group not in hosts_data:         print(f\"Group {args.group} not found in hosts file.\")         return     routers = hosts_data[args.group]['hosts']     comm1='sho int statu | beg Port'     comm2='sho cdp nei | beg Device'      output_filed = args.group + '_inter_des.csv' #      output_filec = args.group + '_inter_cdp.csv' #     STRd = \"Hostname,IP_address,Interface,State,Description,Vlan\"  #      with open(output_filed, \"w\", newline=\"\") as out_filed:         writer = csv.writer(out_filed)         out_filed.write(STRd)         out_filed.write('\\n')     STRc = \"Hostname,IP_address,Interface,New_Description\"  # with ip     with open(output_filec, \"w\", newline=\"\") as out_filec:         writer = csv.writer(out_filec)         out_filec.write(STRc)         out_filec.write('\\n')    # Connect to each router and execute the specified command     for router_name, router_info in routers.items():         if ping_ip(router_info['ansible_host']) == \"no\":   # check if host alive              print( ' offline --------- ', router_name,'  ',router_info['ansible_host'])             continue         else:              print( '  online --------- ', router_name,'  ',router_info['ansible_host'])         # Create Netmiko connection dictionary         netmiko_connection = {             'device_type': 'cisco_ios',             'host': router_info['ansible_host'],             'username': global_vars['ansible_user'],             'password': global_vars['ansible_password'],             'secret': global_vars['ansible_become_password'],         }          # Establish SSH connection         connection = ConnectHandler(**netmiko_connection)         # Enter enable mode         connection.enable()         # Execute the specified command         outputd1 = connection.send_command(comm1)         outputd2 = connection.send_command(comm2)         # Print the output         print(f\"  ------------ Output from {router_name} ({router_info['ansible_host']}):\")         print(f\" \")         lines = outputd1.strip().split('\\n')         lines = lines[1:]         for line in lines:             swi=router_name             ipad= router_info['ansible_host']             por=line[:9].replace(' ', '')         # port             sta =  line[29:41].replace(' ', '')    # interface connected or notconnected             des =  line[10:28].replace(' ', '')    # existing description             vla = line[42:46].replace(' ', '')     # vlan             print(\"switch \",swi,\" port \",por, 'state ',sta,\" Descr \",des,\" vlan \", vla )             STR = swi + \",\" + ipad + \",\" + por +\",\" + sta +\",\" + des + \",\" + vla # +\",\"  # with ip             with open(output_filed, 'a') as f:                 f.write(STR)                 f.write('\\n')         lines1 = outputd2.strip().split('\\n')         lines1 = lines1[1:]  # This correctly removes the first line (header)         filtered_lines =  lines1         try:             first_empty_index = filtered_lines.index('')             # Keep only the lines before the first empty line             filtered_lines = filtered_lines[:first_empty_index]         except ValueError:             # No empty line found, do nothing             pass         lines1 = filtered_lines        # cleaned_text         print(' filtered_lines ', filtered_lines)         for line in lines1:             rlin1 =  line[:16]             dot_position = rlin1.find('.')             rlin2 = rlin1[:dot_position]     # remove domain name from name             rlin =  rlin2 + '|' + line[58:67] + '|' + line[68:]             ndes = rlin.replace(' ', '')   # remove all spaces             por=line[17:33]             por1 = por[0:2]+por[3:33]   # remove 3rd char from port name             por=por1.replace(' ', '')             swi=router_name             ipad= router_info['ansible_host']             print(\"switch \",swi,\" port \",por, \" Descr \", ndes )             STRc = swi + \",\" + ipad + \",\" + por +\",\" + ndes  # with ip             with open(output_filec, 'a') as f:                 f.write(STRc)                 f.write('\\n')         print(f\"  ------------ end\")         connection.disconnect()    # Disconnect from device     output_filem = args.group + '_merg.csv' #     with open(output_filed, mode='r') as file:         reader = csv.DictReader(file)         sw_inter_des_data = list(reader) # Read the sw_inter_cdp.csv file into a list of dictionaries     with open(output_filec, mode='r') as file:         reader = csv.DictReader(file)         sw_inter_cdp_data = list(reader) # Create a lookup dictionary for sw_inter_cdp_data based on Hostname, IP_address, and Interface     cdp_lookup = {         (row['Hostname'], row['IP_address'], row['Interface']): row['New_Description']         for row in sw_inter_cdp_data     } # Add the New_Description to sw_inter_des_data     for row in sw_inter_des_data:         key = (row['Hostname'], row['IP_address'], row['Interface'])         row['New_Description'] = cdp_lookup.get(key, '')      # Write the updated data to a new CSV file     with open(output_filem, mode='w', newline='') as file:         fieldnames = sw_inter_des_data[0].keys()         writer = csv.DictWriter(file, fieldnames=fieldnames)         writer.writeheader()         writer.writerows(sw_inter_des_data)     print(\"New CSV file with added New_Description column has been created as \", args.group , '_merg.csv') # Entry point of the script if __name__ == '__main__':     main() <\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 csv \u0444\u0430\u0439\u043b: <\/p>\n<pre><code class=\"yaml\">Hostname,IP_address,Interface,State,Description,Vlan,New_Description access_switch3,192.168.38.143,Gi0\/0,connected,PORT00,1,R3725|3725|Fas0\/0 access_switch3,192.168.38.143,Gi0\/1,connected,PORT11,1, access_switch3,192.168.38.143,Gi0\/2,connected,002,1, access_switch3,192.168.38.143,Gi0\/3,connected,003,1, access_switch3,192.168.38.143,Gi1\/0,connected,sw2|Gig0\/0,1,sw2||Gig0\/0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-426827","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/426827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426827"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/426827\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}