| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# cdprs.cgi: Sample perl cgi to collect cdpr data to a file |
| 4 |
# |
| 5 |
# Copyright (c) 2003-2008 MonkeyMental.com |
| 6 |
|
| 7 |
# This program will show you which Cisco device your machine is |
| 8 |
# connected to based on CDP packets received. |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or |
| 11 |
# modify it under the terms of the GNU General Public License |
| 12 |
# as published by the Free Software Foundation; either version 2 |
| 13 |
# of the License, or (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program; if not, write to the Free Software |
| 22 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 |
####################################################### |
| 24 |
|
| 25 |
|
| 26 |
# Subroutine: ReadGetData |
| 27 |
# |
| 28 |
# On a get request, this is the function that gets the |
| 29 |
# information from the browser, and puts it into a |
| 30 |
# string that the cgi script can use. |
| 31 |
|
| 32 |
sub ReadGetData |
| 33 |
{ |
| 34 |
local(*queryString) = @_ if @_; |
| 35 |
$queryString = $ENV{"QUERY_STRING"}; |
| 36 |
return 1; |
| 37 |
} |
| 38 |
|
| 39 |
####################################################### |
| 40 |
|
| 41 |
|
| 42 |
# Subroutine: ReadPostData |
| 43 |
# |
| 44 |
# On apost request, this is the function that gets the |
| 45 |
# information from the browser, and puts in into a |
| 46 |
# string that the cgi script can use |
| 47 |
|
| 48 |
sub ReadPostData |
| 49 |
{ |
| 50 |
local(*queryString) = @_; |
| 51 |
local($contentLength); |
| 52 |
$contentLength = $ENV{"CONTENT_LENGTH"}; |
| 53 |
|
| 54 |
if ($contentLength) |
| 55 |
{ |
| 56 |
read(STDIN,$queryString,$contentLength); |
| 57 |
} |
| 58 |
|
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
####################################################### |
| 63 |
|
| 64 |
|
| 65 |
# Subroutine: DecodeData |
| 66 |
# |
| 67 |
# Since the information passed from the browser is |
| 68 |
# encrypted, it must be decoded before use. The method |
| 69 |
# for encryption is as such: |
| 70 |
# Spaces are turned into plus signs (+) |
| 71 |
# |
| 72 |
# Non-alphanumeric characters are turned into their |
| 73 |
# hex equivalent preceded by a percent sign (%) |
| 74 |
# (an apostrophe (') is %27) |
| 75 |
# |
| 76 |
# This function is called from ParseData |
| 77 |
|
| 78 |
sub DecodeData |
| 79 |
{ |
| 80 |
local(*convert) = @_ if @_; |
| 81 |
|
| 82 |
# Replace all +'s with spaces |
| 83 |
$convert =~ s/\+/ /g; |
| 84 |
|
| 85 |
# Replace all hex values with the proper character |
| 86 |
$convert =~ s/%([0-9A-Fa-f]{2})/pack("c",hex($1))/ge; |
| 87 |
|
| 88 |
# Return success |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
####################################################### |
| 93 |
|
| 94 |
|
| 95 |
# Subroutine: ParseData |
| 96 |
# |
| 97 |
# This function breaks the string recieved from |
| 98 |
# ReadGetData or ReadPostData into values that can |
| 99 |
# be used later in the script. It calls DecodeData |
| 100 |
# to decode the encrypted strings. |
| 101 |
|
| 102 |
sub ParseData |
| 103 |
{ |
| 104 |
local(*queryString) = @_ if @_; |
| 105 |
@Array = split(/&/,$queryString); |
| 106 |
|
| 107 |
foreach $curString(@Array) |
| 108 |
{ |
| 109 |
($key, $value) = split(/=/, $curString); |
| 110 |
&DecodeData(*key); |
| 111 |
&DecodeData(*value); |
| 112 |
|
| 113 |
# To save off the values passed from the browser into known |
| 114 |
# variables, use the following format. |
| 115 |
# |
| 116 |
# if ($key eq "KeyName" && $value ne "") |
| 117 |
# { |
| 118 |
# $KeyName = $value; |
| 119 |
# } |
| 120 |
# |
| 121 |
if ($key eq "switch_ip" && $value ne "") |
| 122 |
{ |
| 123 |
$switch_ip = $value; |
| 124 |
} |
| 125 |
if ($key eq "switch_ip6" && $value ne "") |
| 126 |
{ |
| 127 |
$switch_ip6 = $value; |
| 128 |
} |
| 129 |
if ($key eq "port" && $value ne "") |
| 130 |
{ |
| 131 |
$port = $value; |
| 132 |
} |
| 133 |
if ($key eq "host" && $value ne "") |
| 134 |
{ |
| 135 |
$host = $value; |
| 136 |
} |
| 137 |
if ($key eq "loc" && $value ne "") |
| 138 |
{ |
| 139 |
$loc = $value; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
####################################################### |
| 148 |
# Defines |
| 149 |
|
| 150 |
$logfile = ">>/tmp/cdprs.csv"; |
| 151 |
|
| 152 |
# Main() |
| 153 |
|
| 154 |
# Let the browser know what type of information we are |
| 155 |
# going to be sending back to it, common information |
| 156 |
# types are: |
| 157 |
# Content-type: text/html |
| 158 |
# Content-type: text/plain |
| 159 |
# |
| 160 |
# It is necessary to end the string with two CR/LF's |
| 161 |
# (\n\n) |
| 162 |
|
| 163 |
print "Content-type: text/plain\n\n"; |
| 164 |
|
| 165 |
|
| 166 |
####################################################### |
| 167 |
|
| 168 |
# Find out what type of request we are processing as |
| 169 |
# get and post requests use different methods to obtain |
| 170 |
# the data. $requestType will either be "GET" or "POST" |
| 171 |
|
| 172 |
$requestType = $ENV{"REQUEST_METHOD"}; |
| 173 |
|
| 174 |
####################################################### |
| 175 |
|
| 176 |
# This is a POST request, so use ReadPostData |
| 177 |
# ParseData is a common routine to both GET and POST |
| 178 |
# requests |
| 179 |
|
| 180 |
# Not using a post interface... |
| 181 |
if ($requestType eq "POST") |
| 182 |
{ |
| 183 |
&ReadPostData(*data); |
| 184 |
&ParseData(*data); |
| 185 |
} |
| 186 |
|
| 187 |
if ($requestType eq "GET") |
| 188 |
{ |
| 189 |
&ReadGetData(*data); |
| 190 |
&ParseData(*data); |
| 191 |
|
| 192 |
open(LOGFILE, $logfile); |
| 193 |
print(LOGFILE "Switch: $switch_ip, Switch6: $switch_ip6, Port: $port, Host: $host, Location: $loc\n"); |
| 194 |
close(LOGFILE); |
| 195 |
|
| 196 |
} |