PHP ncurses traceroute


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. // define some key constants.
  5. define("ESCAPE_KEY", 27);
  6. define("ENTER_KEY", 13);
  7.  
  8. // get our initial data
  9. $tr_return = traceroute("destiney.com");
  10.  
  11. $ncurses_session = ncurses_init();
  12.  
  13. $main = ncurses_newwin(0, 0, 0, 0); // main window
  14. ncurses_getmaxyx(&$main, $lines, $columns);
  15. ncurses_border(0,0, 0,0, 0,0, 0,0);
  16.  
  17. while(1){
  18.  
  19. // border the main window
  20. ncurses_attron(NCURSES_A_REVERSE);
  21. ncurses_mvaddstr(0, 1, "Traceroute example");
  22. ncurses_attroff(NCURSES_A_REVERSE);
  23.  
  24. // create a lower window which is dynamically sized...
  25. $lower_frame_window = ncurses_newwin(10, $columns-3, $lines-11, 1);
  26. ncurses_wborder($lower_frame_window, 0,0, 0,0, 0,0, 0,0); // border it
  27. $lower_main_window = ncurses_newwin(8, $columns-5, $lines-10, 2);
  28.  
  29. $main_list_window = ncurses_newwin($lines-12, $columns-3, 1, 1);
  30. ncurses_wborder($main_list_window, 0,0, 0,0, 0,0, 0,0); // border it
  31.  
  32. if($currently_selected == ""){
  33. $currently_selected = 0;
  34. }
  35. for($a=0;$a<count($tr_return);$a++){
  36. $out = $tr_return[$a];
  37. if($currently_selected == intval($a)){
  38. ncurses_wattron($main_list_window,NCURSES_A_REVERSE);
  39. ncurses_mvwaddstr ($main_list_window, 1+$a, 1, $out);
  40. ncurses_wattroff($main_list_window,NCURSES_A_REVERSE);
  41. }else{
  42. ncurses_mvwaddstr ($main_list_window, 1+$a, 1, $out);
  43. }
  44. }
  45. if($y == ENTER_KEY){
  46. $newout = explode(" ", $check_me);
  47. $rwhois_return = rwhois(trim($newout[3]));
  48. $a=0;
  49. while(list($key,$val)=each($rwhois_return)){
  50. ncurses_mvwaddstr($lower_main_window, 1+$a, 1, $key . " - " . $val);
  51. $a++;
  52. }
  53. } elseif($y == ESCAPE_KEY){
  54. ncurses_end();
  55. exit;
  56. }
  57. ncurses_move(-1,1); // toss cursor out of sight.
  58. ncurses_wrefresh($lower_frame_window);
  59. ncurses_wrefresh($lower_main_window); // show it
  60. ncurses_wrefresh($main_list_window);
  61.  
  62. // wait for user to press a key
  63. $y = ncurses_getch($lower_main_window);
  64.  
  65. // check for the presence of up-arrow and down-arrow keys
  66. if($y == NCURSES_KEY_UP){
  67. $currently_selected--;
  68. if($currently_selected < 0){ $currently_selected = 0; }
  69. } elseif($y == NCURSES_KEY_DOWN){
  70. $currently_selected++;
  71. if($currently_selected >= count($tr_return)){
  72. $currently_selected = count($tr_return)-1;
  73. }
  74. }
  75. }//end main while
  76.  
  77. // the following are two helper functions for
  78. // this ncurses example.
  79. function traceroute($address){
  80. exec("/usr/sbin/traceroute $address", $trreturn);
  81. return $trreturn;
  82. }//end function
  83.  
  84. // reverse whois
  85. function rwhois($query){
  86. $fp = fsockopen ("rwhois.arin.net", 4321, $errno, $errstr, 30);
  87. if(!$fp){
  88. $ret[] = "$errstr ($errno)\n";
  89. } else {
  90. fputs($fp, "$query
  91.  
  92. ");
  93. $count=1;
  94. while(!feof($fp) && $count < 17){
  95. $back = fgets ($fp,128);
  96. $data = explode(":",$back);
  97. $ret[$data[1]] = $data[2];
  98. $count++;
  99. }//wend
  100. fclose ($fp);
  101. }//fi
  102. return $ret;
  103. }//end function
  104.  
  105. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.