Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl script to modify a username if it matches a couple of conditions

Status
Not open for further replies.
Mar 1, 2010
1
US
I'm trying to compose a tcl script to read a username from our radius server, and modify if it matches a couple of conditions.

First off, here is a typical username that I would want to modify: 0900000023000003780a2202fc0000229f

This is a hex string returned from a dslam. The issue is that the last 8 characters of the string can change on a whim. So I can't use it for authentication data. However, character's 19 through 26 contain the IP address of the device. So 0a2202fc equals the IP address 10.34.2.252. I will have a list of all the devices I will need to compare against.

So here is my plan:

1) Read the username
2) If the length of the username equals 34, send it through an if/else condition
a) Read characters 19~26
b) Comapre them to a(n) external file/array that has 300+ objects - Need help here to choose which would be better
c) If the value looked up in the array and the string from 19~26 matches the 8 digit hex string pulled from the username, truncated the last 8 characters
i.e. 0900000023000003780a2202fc0000229f will be modified to 0900000023000003780a2202fc
d) send the new username through
3) ELSE do nothing and send the username through as presented.

With my elementary knowledge of tcl, I got a start with this.

proc ProcessBASIP {request response environ} {
set username [ $request get User-Name ]

if { [ string length $username ] == 34} {
string equal
string trimright
# SETUP NEW USERNAME
$environ put User-Name $NEW_username
}
else {
$environ put User-Name $username
}

That's as far as I have gotten. I understand some computer languages, but regular expressions and tcl are a bit above me.

Any help you can give me would be appreciated.

Thanks,
Don
 
Read characters 19~26
...and convert them to an address (nnn.nnn.nnn.nnn)
Code:
set str1926 [string range $username 18 25]
set strAddr ""
for i {0 2 4 6} {
   set i2 [expr $i+1]
   set e [string range $str1926 $i $i2]
   append strAddr [scan $e %x]
   append strAddr .
}
set strAddr [string trim $strAddr .]
Tcl indexing is 0-based

Comapre them to a(n) external file/array that has 300+ objects - Need help here to choose which would be better
Depending on the structure of the file, I think I'd read the data into a list. So you would then have a list, say lstDvc each element of which is one of the devices.

If the value looked up in the array and the string from 19~26 matches the 8 digit hex string pulled from the username, truncated the last 8 characters
Code:
set boolMatch 0
if {[lsearch $lstDvc "strAddr"]>-1} {set boolMatch 1}
if boolMatch {set username [string range $username 0 end-8]}



_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top