sipgrep 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/perl
  2. # sipgrep version 0.2. Skin for ngrep. (C) 2005-2006 Alexandr Dubovikov <[email protected]>
  3. use Term::ANSIColor;
  4. use Getopt::Std;
  5. #colors: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, ON_WHITE
  6. #
  7. #type: BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK, REVERSE, CONCEALED,
  8. $COLORS{'method'}='bold red';
  9. $COLORS{'response'} ='bold yellow';
  10. $COLORS{'callid'} = 'bold magenta';
  11. $COLORS{'fromtag'} = 'bold blue';
  12. $COLORS{'totag'} = 'bold green';
  13. $COLORS{'viabranch'} = 'bold cyan';
  14. $limit=2000;
  15. $ngrep="/usr/local/bin/ngrep"; #path to NGREP
  16. $ngrep_flags="-l"; # Flag for Ngrep
  17. $colorsmethods="INVITE|REGISTER|BYE|ACK|CANCEL|OPTIONS|REFER|NOTIFY|MESSAGE|INFO|PRACK|UPDATE";
  18. %options=();
  19. getopts("f:t:l:ahVp:Tcn",\%options);
  20. $version=<<END;
  21. Sipgrep version 0.2
  22. Created by Alexandr Dubovikov <shurik\@start4.info>
  23. END
  24. $usage=<<END;
  25. Usage: sipgrep <-h> <-f number> <-t number> <-a> <-l file> <-V> <-p> <-T> <-n|-c>
  26. -h Displays this help message.
  27. -f ARG Search ARG in From field.
  28. -t ARG Search ARG in To field.
  29. -a Search the ARG from '-f' and '-t' parameters in To and From fields.
  30. -l ARG Debug file name.
  31. -V Displays the current version.
  32. -p Port for ngrep.
  33. -T Parameter for ngrep. Indicating the delta between packet matches.
  34. -c Allow colors in debug file.
  35. -n Not allow colors in STDOUT.
  36. Example: sipgrep -f 0123456 -t 0654321 -l debug.sip
  37. or
  38. sipgrep -f 0123456 -a -l debug.sip
  39. END
  40. #version
  41. if(defined $options{V})
  42. {
  43. print $version; exit;
  44. }
  45. #usage
  46. if((!defined $options{f} && !defined $options{t}) || defined $options{h})
  47. {
  48. print $usage; exit;
  49. }
  50. #TimeStamp
  51. $ngrep_flags .= sprintf(" %s", (defined $options{T}) ? "-T" : "-t" );
  52. #Port
  53. $ngrep_flags .= sprintf(" port %d", (defined $options{p}) ? $options{p} : "5060" );
  54. #our system variables
  55. $anumber=$options{f};
  56. $bnumber=$options{t};
  57. $all=$options{a};
  58. $filedebug=$options{l};
  59. $nocolors=$options{n};
  60. $debugfilecolors=$options{c};
  61. #remove old debug file.
  62. unlink $filedebug if(defined $filedebug);
  63. #open PIPE
  64. open(PIPE,"$ngrep $ngrep_flags |") or die "Can't run '$ngrep' programm: $!\n";
  65. select(PIPE); $| = 1; # make unbuffered
  66. select(STDOUT); $| = 1; # make unbuffered
  67. while(<PIPE>)
  68. {
  69. chomp($_);
  70. s/ //ig;
  71. s/ // if(/^ /);
  72. if(/\.\. (.*)$/)
  73. {
  74. $tmp.=$_;
  75. if(create_newline($tmp)==1)
  76. {
  77. undef $firstvia;
  78. system_out("----------------begin of packet -----------------------------\n");
  79. foreach $key (@tmparray)
  80. {
  81. system_out($key."\n");
  82. }
  83. system_out("------------------end of packet -----------------------------\n");
  84. }
  85. }
  86. elsif(/^#/) { undef $tmp;}
  87. elsif(/^U /) { $tmp=$_."....";}
  88. else { $tmp.=$_;}
  89. }
  90. close(PIPE);
  91. sub create_newline
  92. {
  93. my $tmpstring = shift;
  94. exit if($index > $limit);
  95. undef @tmparray;
  96. @tmparray=split(/\.\./,$tmpstring);
  97. $print_out=1;
  98. undef $searchcallid;
  99. foreach $key(@tmparray)
  100. {
  101. if(defined $anumber || defined $bnumber)
  102. {
  103. $print_out=0;
  104. getmatch($key); #if(!$callid)
  105. $tmpcallid=getcallid($key);
  106. if($searchcallid==1)
  107. {
  108. $GCALLID{$tmpcallid}=1;
  109. $print_out=1;
  110. }
  111. }
  112. }
  113. return $print_out;
  114. }
  115. sub getmatch
  116. {
  117. my $tmps = shift;
  118. #From: "Martin Mustermann" <sip:[email protected]>;tag=2bdf62455c76484b9e1163154d2758cd;epid=46aa53832b
  119. if($tmps=~/^From:/i && ((defined $anumber && $tmps=~/$anumber/ig) || (defined $all && defined $bnumber && $tmps=~/$bnumber/ig)))
  120. {
  121. $searchcallid=1;
  122. }
  123. elsif($tmps=~/^To:/i && ((defined $bnumber && $tmps=~/$bnumber/ig) || (defined $all && defined $anumber && $tmps=~/$anumber/ig)))
  124. {
  125. $searchcallid=1;
  126. }
  127. if($tmps=~/^Call-ID:/ig)
  128. {
  129. (undef,$tmpcallid)=split(/: /,$tmps,2);
  130. $print_out=1 if($GCALLID{$tmpcallid}==1);
  131. }
  132. }
  133. sub getcallid
  134. {
  135. my $tmps = shift;
  136. (undef,$tmpcallid)=split(/: /,$tmps,2) if($tmps=~/^Call-ID:/ig);
  137. return $tmpcallid;
  138. }
  139. sub system_out
  140. {
  141. my $out = shift;
  142. my $tmpmain, $tmpstr;
  143. #Method:
  144. if($out =~/^($colorsmethods) /ig)
  145. {
  146. ($tmpmain,$tmpstr)=split(/ /,$out,2);
  147. print_out($tmpmain, $COLORS{'method'});
  148. print_out(" ".$tmpstr);
  149. }
  150. #Response:
  151. elsif($out =~/^SIP\/2\.0 [1-6][0-9][0-9] /ig)
  152. {
  153. ($tmpstr, $tmpmain)=split(/ /,$out,2);
  154. print_out($tmpstr." ");
  155. print_out($tmpmain, $COLORS{'response'});
  156. }
  157. #Callid
  158. elsif($out =~/^(Call-ID):/ig)
  159. {
  160. ($tmpstr, $tmpmain)=split(/: /,$out,2);
  161. print_out($tmpstr.": ");
  162. print_out($tmpmain, $COLORS{'callid'});
  163. }
  164. #From/To: tag
  165. elsif($out =~/^(From|f|To|t): /ig && $out=~/;tag=/ig)
  166. {
  167. ($tmpstr, $tmpmain)=split(/;tag=/,$out,2);
  168. print_out($tmpstr.";tag=");
  169. ($tmpmain, $tmpstr)=split(/;/,$tmpmain,2);
  170. print_out($tmpmain, $out =~/^(From|f): / ? $COLORS{'fromtag'} : $COLORS{'totag'});
  171. print_out(";".$tmpstr) if(defined $tmpstr);
  172. }
  173. #Via: branch
  174. elsif($out =~/^(Via|v): /ig && $out=~/;branch=/ig && !defined $firstvia)
  175. {
  176. ($tmpstr, $tmpmain)=split(/;branch=/,$out,2);
  177. print_out($tmpstr.";branch=");
  178. ($tmpmain, $tmpstr)=split(/;/,$tmpmain,2);
  179. print_out($tmpmain, $COLORS{'viabranch'});
  180. print_out(";".$tmpstr) if(defined $tmpstr);
  181. $firstvia = 1;
  182. }
  183. else { print_out($out); }
  184. }
  185. sub print_out
  186. {
  187. my $ltext = shift;
  188. my $lcolor = shift;
  189. $lcolor='reset' if(!defined $lcolor || defined $nocolors);
  190. print color $lcolor;
  191. print $ltext;
  192. if(defined $filedebug)
  193. {
  194. open(DBG, ">>$filedebug");
  195. $lcolor = 'reset' if(!(defined $debugfilecolors));
  196. print DBG color $lcolor;
  197. print DBG $ltext;
  198. close(DBG);
  199. }
  200. }