Advertisement
Guest User

phpcisco

a guest
Jul 6th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. function PGWscripts($pgwCommandString)
  2. {
  3. // ------------------- PARAMETERS
  4.  
  5. $Dontrun = 0 ; // set to 1 to avoid actually performing the script
  6. $Flush = 0 ; // Normal value is 0 for TDT use (flush kills http headers)
  7. $SSH_debug = 1 ; // Yes we want debugs!
  8. $SSH_usleep = 1000000 ; // 0.5 sec
  9. $SSH_IPaddress = "<removed>" ; // The PGW address to connect to via SSH2
  10. // $SSH_username = "removed" ; // The PGW user
  11. // $SSH_password = "removed" ; // The PGW password baa#123
  12. $SSH_username = "removed" ; // The PGW user
  13. $SSH_password = "removed" ; // The PGW password baa#123
  14. // $SSH_username = "mgcusr" ; // The PGW user
  15. // $SSH_password = "cisco" ; // The PGW password baa#123
  16. // --------------------------------------
  17.  
  18. niwaWriteLog("PGWscripts: Process Start command string [" . $pgwCommandString . "]","Diag") ;
  19. $j = 0 ;
  20. $pgwSingleCommand = explode("*",$pgwCommandString) ;
  21. while(isset($pgwSingleCommand[$j]))
  22. {
  23. if($pgwSingleCommand[$j] != NULL || $pgwSingleCommand[$j] != "") niwaWriteLog("PGWscripts: Command [".$j."] is [".$pgwSingleCommand[$j]."]","Diag") ;
  24. $j++ ;
  25. } // end of while
  26. niwaWriteLog("SSH: *** PGW connection, IP Address [" . $SSH_IPaddress . "]","Diag") ;
  27. if($Dontrun) // -------------------------------------------- DEBUG ONLY ----------------------------------
  28. {
  29. niwaWriteLog("PGWscripts: *************** WARNING DEBUG BREAK OUT IS TURNED ON, NO COMMANDS SENT","Diag") ;
  30. return 999 ;
  31. } // -------------------------------------------- DEBUG ONLY ----------------------------------
  32.  
  33. $returncode = 0 ;
  34.  
  35. if (!($resource=@ssh2_connect($SSH_IPaddress))) {
  36. // echo "[FAILED]<br />";
  37. if($SSH_debug) niwaWriteLog("SSH: Failed IP connection","Diag") ;
  38. return(50);
  39. }
  40. //echo "[OK]<br />";
  41. if($SSH_debug) niwaWriteLog("SSH: Connected to IP Address [" . $SSH_IPaddress . "]","Diag") ;
  42.  
  43. // Authentification by login/passwd
  44. //echo "Authentification ";
  45. if($SSH_debug) niwaWriteLog("SSH: Attempt login with username [" . $SSH_username . "]","Diag") ;
  46. if (!@ssh2_auth_password($resource,$SSH_username,$SSH_password)) {
  47. // echo "[FAILED]<br />";
  48. if($SSH_debug) niwaWriteLog("SSH: Failed to login","Diag") ;
  49. return(51);
  50. }
  51. // echo "[OK]<br />";
  52. if($SSH_debug) niwaWriteLog("SSH: Login OK with username [" . $SSH_username . "]","Diag") ;
  53.  
  54. // We need a shell
  55. // echo "Shell stdio ";
  56. if($SSH_debug) niwaWriteLog("SSH: Now require SHELL","Diag") ;
  57. if (!($stdio = @ssh2_shell($resource,"xterm"))) {
  58. // echo "[FAILED]<br />";
  59. if($SSH_debug) niwaWriteLog("SSH: Failed to acquire XTERM SHELL","Diag") ;
  60. return(52);
  61. }
  62. // echo "[OK]<br />";
  63. if($SSH_debug) niwaWriteLog("SSH: SHELL acquired OK","Diag") ;
  64.  
  65. // mml connection
  66. // Be careful to add an '\n' at the end of the command
  67. $command = "mml\n";
  68. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . stripN($command) . "]","Diag") ;
  69. fwrite($stdio,$command);
  70.  
  71. usleep($SSH_usleep);
  72.  
  73. // Then u can fetch the stream to see what happens on stdio
  74. while($line = fgets($stdio)) {
  75. if($Flush) flush();
  76. // echo $line."<br />";
  77. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
  78. }
  79.  
  80. /* ---------------------------------
  81. // $command = "numan-add:fullnumbertrans:svcname=\"2E2\",numtype=\"1\",digstring=\"650000\",translatednum=\"0150000\"\n";
  82. $command = "numan-" . $adddelete . ":fullnumbertrans:svcname=\"" . $svcname . "\",numtype=\"" . $numtype . "\",digstring=\"" . $digstring . "\",translatednum=\"". $translatednum ."\"\n";
  83. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
  84. fwrite($stdio,$command);
  85.  
  86. usleep($SSH_usleep);
  87.  
  88. // Then u can fetch the stream to see what happens on stdio
  89. while($line = fgets($stdio)) {
  90. flush();
  91. // echo $line."<br />";
  92. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . $line . "]","Diag") ;
  93. }
  94. ---------------------------- */
  95. $j = 0 ;
  96. $pgwSingleCommand = explode("*",$pgwCommandString) ;
  97. while(isset($pgwSingleCommand[$j]))
  98. {
  99. if($pgwSingleCommand[$j] != NULL || $pgwSingleCommand[$j] != "")
  100. {
  101. niwaWriteLog("PGWscripts: Command [".$j."] is [".$pgwSingleCommand[$j]."]","Diag") ;
  102. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $pgwSingleCommand[$j] . "]","Diag") ;
  103. fwrite($stdio,$pgwSingleCommand[$j] . "\n");
  104. usleep($SSH_usleep);
  105. // Then u can fetch the stream to see what happens on stdio
  106. while($line = fgets($stdio))
  107. {
  108. if($Flush) flush();
  109. // echo $line."<br />";
  110. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
  111. } // while
  112. } // if
  113. $j++ ;
  114. } // end of while
  115.  
  116. // -------------------------------
  117.  
  118. $command = "quit\n";
  119. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
  120. fwrite($stdio,$command);
  121.  
  122. usleep($SSH_usleep);
  123.  
  124. // Then u can fetch the stream to see what happens on stdio
  125. while($line = fgets($stdio)) {
  126. if($Flush) flush();
  127. // echo $line."<br />";
  128. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
  129. }
  130. // -------------------------------
  131.  
  132. $command = "exit\n";
  133. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
  134. fwrite($stdio,$command);
  135.  
  136. usleep($SSH_usleep);
  137.  
  138. // Then u can fetch the stream to see what happens on stdio
  139. while($line = fgets($stdio)) {
  140. if($Flush) flush();
  141. // echo $line."<br />";
  142. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line). "]","Diag") ;
  143. }
  144.  
  145. // -------------------------------
  146. $command = "logout\n" ;
  147. if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
  148. fwrite($stdio,$command);
  149.  
  150. usleep($SSH_usleep);
  151.  
  152. // Then u can fetch the stream to see what happens on stdio
  153. while($line = fgets($stdio)) {
  154. if($Flush) flush();
  155. // echo $line."<br />";
  156. if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line). "]","Diag") ;
  157. }
  158.  
  159. // -------------------------------
  160.  
  161.  
  162. /* ------------cut----------------
  163. $command = ":wq\n";
  164. niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
  165. fwrite($stdio,$command);
  166.  
  167. usleep($SSH_usleep);
  168.  
  169. $search = "written" ;
  170. $search_preg = "/" . $search . "/i" ;
  171. while($line = fgets($stdio)) {
  172. flush();
  173. $line=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$line);
  174. if (preg_match($search_preg, $line)) niwaWriteLog("SSH: Found command [" . $search . "] OK","Diag") ;
  175. niwaWriteLog("SSH: Got Response [" . $line . "]","Diag") ;
  176. }
  177.  
  178. -------------------- */
  179.  
  180. // It's always cleaner to close all stream
  181. niwaWriteLog("SSH: PGW Code Finished Result [" . $returncode . "]","Diag") ;
  182. usleep($SSH_usleep); // is this needed?
  183. fclose($stdio);
  184. return $returncode ;
  185. } // End of function PGWscripts
  186.  
  187. function stripN($command)
  188. {
  189. $command = str_replace("\r","",$command) ;
  190. $command = str_replace("\n","",$command) ;
  191. return($command) ;
  192. } // stripN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement