{"id":351,"date":"2018-11-26T10:00:46","date_gmt":"2018-11-26T09:00:46","guid":{"rendered":"https:\/\/blog.van-daag.nl\/?p=351"},"modified":"2018-11-26T10:00:46","modified_gmt":"2018-11-26T09:00:46","slug":"domoticz-cisco-port-on-of-switch","status":"publish","type":"post","link":"https:\/\/van-daag.nl\/?p=351","title":{"rendered":"Domoticz Cisco Port on \/ of switch"},"content":{"rendered":"<p>Script is created by Johan ven Boomgaard.<\/p>\n<pre class=\"lang:sh decode:true \" title=\"Set-Cisco.sh\">#!\/bin\/bash\r\n# Set-cisco-port.sh\r\n# ==============\r\n# |Author: JvdB|\r\n# ==============\r\n# \r\n# Find,disable,enable port using SNMP at Cisco switch#\r\n# \r\n# Usage: cd &lt;directory&gt; .\/Set-cisco-port.sh &lt;switch ip-address&gt; &lt;description&gt; &lt;action&gt;\r\n# Example: cd &lt;directory&gt; .\/Set-cisco-port.sh 192.168.2.250 Test-pc e \r\n#\r\n# Where:\r\n# - 1st parameter is switch IP address\r\n# - 2nd is Description of NIC\/Device\r\n# - 3rd is operation(e - enable port when find description at port, d -disable port when find description at port)\r\n#\r\n# =======================================\r\n# Start script with setting the variables\r\n# =======================================\r\n#\r\nswitch_ip=$1;\r\ndesc=$(echo $2 | sed 's\/:\/\/g');\r\ncommunity=\"private\";\r\noperation=$3;\r\n#\r\n2970_get_port(){\r\n# Check for description at port\r\nfor i in `snmpwalk -On -v2c -c $community@1 192.168.2.250 .1.3.6.1.4.1.9.9.46.1.3.1.1.2  | sed 's\/.1.3.6.1.4.1.9.9.46.1.3.1.1.2.1.\/\/g'  | awk '{print $1}'`; do\r\n        find_mac=`snmpwalk -On -v2c -c  $community@$i 192.168.2.250 .1.3.6.1.2.1.17.4.3.1.1 | sed s\/' '\/\/g  | grep -i $desc | sed 's\/^.*Hex-STRING\\:\/\/g'| awk '{print $1}'`;\r\n        if [[ $find_mac != \"\" ]]; then\r\n                point1=$(snmpwalk -On -v2c -c $community@$i $switch_ip .1.3.6.1.2.1.17.4.3.1.1 | sed s\/' '\/\/g  | grep -i $desc  | sed 's\/.1.3.6.1.2.1.17.4.3.1.1.\/\/g' | sed 's\/=.*\/\/g' );\r\n                port_numb=`snmpwalk -v2c -c $community@$i $switch_ip .1.3.6.1.2.1.17.4.3.1.2  | grep -i $point1 | sed 's\/^.*INTEGER\\: \/\/g'`;\r\n                echo \"Description $desc was found at port number #\"$port_numb;\r\n        fi\r\ndone;\r\n}\r\ndisable_port(){\r\n        echo \"Going to disable port by Description\";\r\n\t# Find by port description\r\n        port_to_disable=`snmpwalk -v2c -On -c $community $switch_ip .1.3.6.1.2.1.31.1.1.1.18 | grep -i $desc | sed 's\/.1.3.6.1.2.1.31.1.1.1.18.\/\/g' | awk '{print $1}'`;\r\n        # If port was not found\r\n        if [[ $port_to_disable == \"\" ]]; then\r\n                echo \"Port wasn't found by port description. Exiting ...\";\r\n                2970_get_port;\r\n                $port_to_disable=$port_numb;\r\n                echo $ $port_to_disable;\r\n                exit;\r\n        fi;\r\n        # Disable port\r\n        snmpset -v2c -c $community $switch_ip .1.3.6.1.2.1.2.2.1.7.$port_to_disable i 2;\r\n        # Save running config of Cisco switch to startup\r\n        save_2970_cfg;\r\n}\r\nenable_port(){\r\n        echo \"Going to enable port by Description\";\r\n        # Find by port description\r\n        port_to_enable=`snmpwalk -v2c -On -c $community $switch_ip .1.3.6.1.2.1.31.1.1.1.18 | grep -i $desc | sed 's\/.1.3.6.1.2.1.31.1.1.1.18.\/\/g' | awk '{print $1}'`;\r\n        # If port was not found\r\n        if [[ $port_to_enable == \"\" ]]; then\r\n                echo \"Port wasn't found by port description. Exiting ...\";\r\n                2970_get_port;\r\n                $port_to_enable=$port_numb;\r\n                echo $ $port_to_enable;\r\n                exit;\r\n        fi;\r\n        # Enable port\r\n        snmpset -v2c -c $community $switch_ip .1.3.6.1.2.1.2.2.1.7.$port_to_enable i 1;\r\n        # Save running config of Cisco switch to startup\r\n        save_2970_cfg;\r\n}\r\nsave_2970_cfg(){\r\n        echo \"Saving Cisco 2970 switch configuration\";\r\n         snmpset -t60 -v2c -c $community $switch_ip 1.3.6.1.4.1.9.2.1.54.0 i 1\r\n}\r\nmain(){\r\n        if [[ $operation == \"f\" ]]; then\r\n                echo \"Find port operation\";\r\n                2970_get_port;\r\n        elif [[ $operation == \"d\" ]]; then\r\n                echo \"Disable port operation\";\r\n                disable_port;\r\n        elif [[ $operation == \"e\" ]]; then\r\n                echo \"Enable port operation\";\r\n                enable_port;\r\n        else\r\n                echo \"Wrong arguments given\";\r\n        fi;\r\n}\r\nmain;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Script is created by Johan ven Boomgaard. #!\/bin\/bash # Set-cisco-port.sh # ============== # |Author: JvdB| # ============== # # Find,disable,enable port using SNMP at Cisco switch# # # Usage: cd &lt;directory&gt; .\/Set-cisco-port.sh &lt;switch ip-address&gt; &lt;description&gt; &lt;action&gt; # Example: cd &lt;directory&gt; .\/Set-cisco-port.sh 192.168.2.250 Test-pc e # # Where: # &#8211; 1st parameter is switch IP address [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[35,13],"tags":[],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-domoticz","category-raspberry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351"}],"version-history":[{"count":2,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":373,"href":"https:\/\/van-daag.nl\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions\/373"}],"wp:attachment":[{"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/van-daag.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}