{"id":16895762,"url":"https://github.com/ytti/ruby-vtp","last_synced_at":"2025-03-20T10:22:42.608Z","repository":{"id":25983432,"uuid":"29425587","full_name":"ytti/ruby-vtp","owner":"ytti","description":"vtp listener for ruby","archived":false,"fork":false,"pushed_at":"2015-12-23T14:27:29.000Z","size":15,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-25T10:44:03.005Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ytti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-18T12:48:49.000Z","updated_at":"2021-08-10T23:28:52.000Z","dependencies_parsed_at":"2022-07-29T05:18:26.210Z","dependency_job_id":null,"html_url":"https://github.com/ytti/ruby-vtp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ytti%2Fruby-vtp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ytti%2Fruby-vtp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ytti%2Fruby-vtp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ytti%2Fruby-vtp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ytti","download_url":"https://codeload.github.com/ytti/ruby-vtp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244591987,"owners_count":20477828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T17:25:57.049Z","updated_at":"2025-03-20T10:22:42.585Z","avatar_url":"https://github.com/ytti.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ruby VTP Listener\n\nIdea is that I could have lab switch where all my edge lab devices connect 2-3\ntimes, as simulated edge custsomers. This switch would have one VLAN per port,\nand then trunk port to linux server.\n\nWhen ever new edge lab device is connected to switch and access VLAN created,\nlinux would automatically create VLAN and network namespace for the VLAN name,\nand of course remove when VLAN is removed\n\nThe moment VLAN is created, the 'virtual host' is created, IP address assigned,\ndefault GW set. So you can immediately start to ping against the 'client' PC.\n\nYou could, of course, also run software at the 'client' PC:\n\n    alias vrf=\"ip netns exec\"\n    vrf vlan_name1 /bin/zsh\n    telnet server 25\n    exit\n    vrf vlan_name2 /bin/zsh\n    iperf -s -u\n\nAnd you're pinging through what ever lab topology is in your lab core connecting those two vlans.\n\n# Library use\n    require 'vtp'\n    require 'open3'\n    class NSPOC\n      VLAN_TRUNK = \"eth1\"\n      MY_IP = \"10\"\n      GW_IP = \"1\"\n      def initialize\n        @ns = {}\n        run\n      end\n\n      def run\n         VTP.capture do |pkt|\n           new = {}\n           pkt.vlan.each do |vlan|\n             next unless vlan.type == 1\n             new[vlan.name] = vlan.id\n           end\n           compare new\n        end\n      end\n\n      def compare new\n        old_ns  = @ns.keys\n        new_ns  = new.keys\n        (old_ns-new_ns).each { |ns| remove ns, @ns[ns] }\n        (new_ns-old_ns).each { |ns| add ns, new[ns] }\n        @ns = new.dup\n      end\n\n      def add name, vlan\n        ip 'netns', 'add', name\n        ip 'link', 'add', 'link', 'eth0', 'netns', name, 'name', VLAN_TRUNK, 'type', 'vlan', 'id', vlan.to_s\n        net = %w(10) + (%w(0) + vlan.to_s.scan(/.{1,2}/))[-2..-1]\n        ip = (net + [MY_IP]).join('.') + '/24'\n        gw = (net + [GW_IP]).join('.')\n        ip 'netns', 'exec', name, 'ip', 'addr',  'add', ip, 'dev', 'eth0'\n        ip 'netns', 'exec', name, 'ip', 'route', 'add', 'default', 'via', gw\n      end\n\n      def remove name, vlan\n        ip 'netns', 'delete', name\n      end\n\n      def ip *args\n        Open3.popen3('ip', *args) do |stdin, stdout, stderr, wait_thr|\n          wait_thr.join\n        end\n      end\n    end\n    NSPOC.new\n\n# CLI use\n    root@kone:~/foo# vtpd --help\n    Usage: vtpd [options]\n        -d, --debug          turn on debugging\n        -f, --file           store to file\n        -i, --interface      specify interface to listen, instead of eth0\n        -n, --domain         if set, injects advertisement request on start\n        -h, --help           Display this help message.\n\n    # vtpd -f /etc/vtp.json     # would run in background and store vtp information in file\n    # vtpd -d                   # would run in foreground and print vtp infromation\n    # vtpd -d --domain 'sux'    # requets VTP database immediately on start from domain 'sux'\n\n# JSON format\n    root@kone:~/foo/ruby-vtp# cat /tmp/vtp.json\n    {\n      \"domain\": \"triotto\",\n      \"revision\": 96,\n      \"vlan\": [\n        {\n          \"id\": 1,\n          \"name\": \"default\"\n        },\n        {\n          \"id\": 9,\n          \"name\": \"vrrp\"\n        },\n      # rest of the vlans\n    }\n\n# TODO\n  1. support vtp1 or vtp3? (vtp1 at least will work by just removing the version check)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fytti%2Fruby-vtp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fytti%2Fruby-vtp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fytti%2Fruby-vtp/lists"}