sipgrep 6.8 KB

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