Need help with TCL code to find IP address from a URL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with TCL code to find IP address from a URL
# 1  
Old 04-22-2013
Need help with TCL code to find IP address from a URL

Need help with a a tcl code. Need to find out the ip address from a URL if it is present to do some activity.

The URLs will be of the form
<domain>?a=12345&d=somestring1(Note: c not present)
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4&d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2

Rules
1) The ip address is present after "c="
2) "c=" may or may not be present
3) If not present the condition should be handled gracefully
4) "c=" may be present anywhere except the end, it will always be "d=" at the end
5) It should only be "c=", something like "ac=" or "bc=" is not valid
6) "c=" will be preceded by either a "?" or "&" only.

Any help with such a code will be helpful.
# 2  
Old 04-23-2013
Hi.

Here is a shell script that drives a tcl script to demonstrate:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate tcl regular expression string extraction.
# For examples on regexp, see:
# http://wiki.tcl.tk/989

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C tclsh

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Demo code for tcl:"
cat tcl1

pl " Results:"
./tcl1 < $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
tclsh 8.4

-----
 Input data file data1:
<domain>?a=12345&d=somestring1(Note: c not present)
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4&d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2

-----
 Demo code for tcl:
#!/usr/bin/env tclsh

# @(#) tcl1     Demonstrate regexp extraction of matched string.

puts stdout ""
if { $argc == 0 } then {
  puts stdout " No command line parameters provided."
} else {
  set i 0
  foreach arg $argv {
    set m1 " $i $arg"
    incr i
    set m2 ""
    if { [ file exists $arg ] } then {
      set m2 [ join [ list "(" [ file type $arg ] ")" ] ]
    }
    puts stdout "$m1 $m2"
  }
}

while { [gets stdin line] >= 0} {
  set ip " "
  regexp {c=([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)} $line -> ip
  if { $ip != " " } {
    puts $ip
  } else {
    puts " Missing ip on $line"
  }
}

exit 0

-----
 Results:

 No command line parameters provided.
 Missing ip on <domain>?a=12345&d=somestring1(Note: c not present)
10.10.10.100
10.1.2.4
10.1.2.4

See wiki noted in script for examples.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading URL using Mechanize and dump all the contents of the URL to a file

Hello, Am very new to perl , please help me here !! I need help in reading a URL from command line using PERL:: Mechanize and needs all the contents from the URL to get into a file. below is the script which i have written so far , #!/usr/bin/perl use LWP::UserAgent; use... (2 Replies)
Discussion started by: scott_cog
2 Replies

2. Shell Programming and Scripting

How to "ls *4.2*" on http URL address ?

Hello. Is there a way to treat internet addresses as hard disk and make ls /url/file_name*.rpmHelp is welcome. (4 Replies)
Discussion started by: jcdole
4 Replies

3. UNIX for Dummies Questions & Answers

Awk: print all URL addresses between iframe tags without repeating an already printed URL

Here is what I have so far: find . -name "*php*" -or -name "*htm*" | xargs grep -i iframe | awk -F'"' '/<iframe*/{gsub(/.\*iframe>/,"\"");print $2}' Here is an example content of a PHP or HTM(HTML) file: <iframe src="http://ADDRESS_1/?click=5BBB08\" width=1 height=1... (18 Replies)
Discussion started by: striker4o
18 Replies

4. Shell Programming and Scripting

How to find invalid URL in a text file using shell script?

How to find and remove invalid URLs in a text file using shell script? (1 Reply)
Discussion started by: vel4ever
1 Replies

5. Shell Programming and Scripting

Bash Script to Find the status of URL

#!/bin/bash timevar=`date +%d-%m-%Y_%H.%M.%S` #-- > Storing Date and Time in a Variable get_contents=`cat urls.txt` #-- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of echo "Datae-time URL Status code Report" >... (2 Replies)
Discussion started by: anishkumarv
2 Replies

6. Programming

Bash Script to Find the status of URL

#!/bin/bash timevar=`date +%F_”%H_%M”` #-- > Storing Date and Time in a Variable get_contents=`cat urls.txt` #-- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of ######### Next Section Does all the processing ######### for i... (0 Replies)
Discussion started by: anishkumarv
0 Replies

7. Emergency UNIX and Linux Support

TCL scripting - searching for a IP address in a string

Hi All, Can anyone please help me with the regular expression/code snippet to search for an IP address in a string with Tcl scripting. Example string "OSPF_NBRUP OSPF neighbor 16.138.181.15 (realm ospf-v2 e1-0/0/0:37.0 area 0.0.0.0) state changed from Full to Down due to KillNbr" In the... (6 Replies)
Discussion started by: Mr. Zer0
6 Replies

8. Programming

Syntax error in tcl/tk code

Hi All, I have written a code in tcl which is supposed to open an GUI in which numbers will be entered & after performing selected operation it wil show a result. #!/usr/local/bin/wish #package require Tk #global opr proc DoOperation {} { global opr set fstno set scdno set result ... (2 Replies)
Discussion started by: milindb
2 Replies

9. Shell Programming and Scripting

find subdomain in url

I want to find out subdomains in the url using shell script. e.g narendra.eukhost.com abc.domainname.co.uk I want to extract narendra & abc from the above urls. Please any one can suggest idea or script ? (2 Replies)
Discussion started by: nrbhole
2 Replies
Login or Register to Ask a Question