File: cdprs.cgi

package info (click to toggle)
cdpr 2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 292 kB
  • sloc: ansic: 1,074; perl: 72; makefile: 23; php: 23
file content (196 lines) | stat: -rwxr-xr-x 4,568 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl
#
# cdprs.cgi: Sample perl cgi to collect cdpr data to a file
#
# Copyright (c) 2003-2008 MonkeyMental.com

# This program will show you which Cisco device your machine is
# connected to based on CDP packets received.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#######################################################


# Subroutine: ReadGetData
#
# On a get request, this is the function that gets the
# information from the browser, and puts it into a 
# string that the cgi script can use.

sub ReadGetData
{
	local(*queryString) = @_ if @_;
	$queryString = $ENV{"QUERY_STRING"};
	return 1;
}

#######################################################


# Subroutine: ReadPostData
#
# On apost request, this is the function that gets the
# information from the browser, and puts in into a
# string that the cgi script can use

sub ReadPostData
{
	local(*queryString) = @_;
	local($contentLength);
	$contentLength = $ENV{"CONTENT_LENGTH"};

	if ($contentLength)
	{
		read(STDIN,$queryString,$contentLength);
	}

	return 1;
}

#######################################################


# Subroutine: DecodeData
#
# Since the information passed from the browser is
# encrypted, it must be decoded before use.  The method
# for encryption is as such:
#  Spaces are turned into plus signs (+)
#
#  Non-alphanumeric characters are turned into their
#  hex equivalent preceded by a percent sign (%)
#  (an apostrophe (') is %27)
#
# This function is called from ParseData

sub DecodeData
{
	local(*convert) = @_ if @_;

	# Replace all +'s with spaces
	$convert =~ s/\+/ /g;

	# Replace all hex values with the proper character
	$convert =~ s/%([0-9A-Fa-f]{2})/pack("c",hex($1))/ge;

	# Return success
	return 1;
}

#######################################################


# Subroutine: ParseData
#
# This function breaks the string recieved from 
# ReadGetData or ReadPostData into values that can
# be used later in the script.  It calls DecodeData
# to decode the encrypted strings.

sub ParseData
{
	local(*queryString) = @_ if @_;
	@Array = split(/&/,$queryString);

	foreach $curString(@Array)
	{
		($key, $value) = split(/=/, $curString);
		&DecodeData(*key);
		&DecodeData(*value);

		# To save off the values passed from the browser into known
		# variables, use the following format.
		#
		# if ($key eq "KeyName" && $value ne "")
		# {
		# 		$KeyName = $value;
		# }
		#
		if ($key eq "switch_ip" && $value ne "")
		{
			$switch_ip = $value;
		}
		if ($key eq "switch_ip6" && $value ne "")
		{
			$switch_ip6 = $value;
		}
		if ($key eq "port" && $value ne "")
		{
			$port = $value;
		}
		if ($key eq "host" && $value ne "")
		{
			$host = $value;
		}
		if ($key eq "loc" && $value ne "")
		{
			$loc = $value;
		}

	}

	return 1;
}

#######################################################
# Defines

$logfile = ">>/tmp/cdprs.csv";

# Main()

# Let the browser know what type of information we are
# going to be sending back to it, common information 
# types are:
#	Content-type: text/html
#	Content-type: text/plain
#
# It is necessary to end the string with two CR/LF's 
# (\n\n)

print "Content-type: text/plain\n\n";


#######################################################

# Find out what type of request we are processing as 
# get and post requests use different methods to obtain
# the data. $requestType will either be "GET" or "POST"

$requestType = $ENV{"REQUEST_METHOD"};

#######################################################

# This is a POST request, so use ReadPostData
# ParseData is a common routine to both GET and POST 
# requests

# Not using a post interface...
if ($requestType eq "POST")
{
	&ReadPostData(*data);
	&ParseData(*data);
}

if ($requestType eq "GET")
{
	&ReadGetData(*data);
	&ParseData(*data);

	open(LOGFILE, $logfile);
	print(LOGFILE "Switch: $switch_ip, Switch6: $switch_ip6, Port: $port, Host: $host, Location: $loc\n");
	close(LOGFILE);

}