Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function PGWscripts($pgwCommandString)
- {
- // ------------------- PARAMETERS
- $Dontrun = 0 ; // set to 1 to avoid actually performing the script
- $Flush = 0 ; // Normal value is 0 for TDT use (flush kills http headers)
- $SSH_debug = 1 ; // Yes we want debugs!
- $SSH_usleep = 1000000 ; // 0.5 sec
- $SSH_IPaddress = "<removed>" ; // The PGW address to connect to via SSH2
- // $SSH_username = "removed" ; // The PGW user
- // $SSH_password = "removed" ; // The PGW password baa#123
- $SSH_username = "removed" ; // The PGW user
- $SSH_password = "removed" ; // The PGW password baa#123
- // $SSH_username = "mgcusr" ; // The PGW user
- // $SSH_password = "cisco" ; // The PGW password baa#123
- // --------------------------------------
- niwaWriteLog("PGWscripts: Process Start command string [" . $pgwCommandString . "]","Diag") ;
- $j = 0 ;
- $pgwSingleCommand = explode("*",$pgwCommandString) ;
- while(isset($pgwSingleCommand[$j]))
- {
- if($pgwSingleCommand[$j] != NULL || $pgwSingleCommand[$j] != "") niwaWriteLog("PGWscripts: Command [".$j."] is [".$pgwSingleCommand[$j]."]","Diag") ;
- $j++ ;
- } // end of while
- niwaWriteLog("SSH: *** PGW connection, IP Address [" . $SSH_IPaddress . "]","Diag") ;
- if($Dontrun) // -------------------------------------------- DEBUG ONLY ----------------------------------
- {
- niwaWriteLog("PGWscripts: *************** WARNING DEBUG BREAK OUT IS TURNED ON, NO COMMANDS SENT","Diag") ;
- return 999 ;
- } // -------------------------------------------- DEBUG ONLY ----------------------------------
- $returncode = 0 ;
- if (!($resource=@ssh2_connect($SSH_IPaddress))) {
- // echo "[FAILED]<br />";
- if($SSH_debug) niwaWriteLog("SSH: Failed IP connection","Diag") ;
- return(50);
- }
- //echo "[OK]<br />";
- if($SSH_debug) niwaWriteLog("SSH: Connected to IP Address [" . $SSH_IPaddress . "]","Diag") ;
- // Authentification by login/passwd
- //echo "Authentification ";
- if($SSH_debug) niwaWriteLog("SSH: Attempt login with username [" . $SSH_username . "]","Diag") ;
- if (!@ssh2_auth_password($resource,$SSH_username,$SSH_password)) {
- // echo "[FAILED]<br />";
- if($SSH_debug) niwaWriteLog("SSH: Failed to login","Diag") ;
- return(51);
- }
- // echo "[OK]<br />";
- if($SSH_debug) niwaWriteLog("SSH: Login OK with username [" . $SSH_username . "]","Diag") ;
- // We need a shell
- // echo "Shell stdio ";
- if($SSH_debug) niwaWriteLog("SSH: Now require SHELL","Diag") ;
- if (!($stdio = @ssh2_shell($resource,"xterm"))) {
- // echo "[FAILED]<br />";
- if($SSH_debug) niwaWriteLog("SSH: Failed to acquire XTERM SHELL","Diag") ;
- return(52);
- }
- // echo "[OK]<br />";
- if($SSH_debug) niwaWriteLog("SSH: SHELL acquired OK","Diag") ;
- // mml connection
- // Be careful to add an '\n' at the end of the command
- $command = "mml\n";
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . stripN($command) . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio)) {
- if($Flush) flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
- }
- /* ---------------------------------
- // $command = "numan-add:fullnumbertrans:svcname=\"2E2\",numtype=\"1\",digstring=\"650000\",translatednum=\"0150000\"\n";
- $command = "numan-" . $adddelete . ":fullnumbertrans:svcname=\"" . $svcname . "\",numtype=\"" . $numtype . "\",digstring=\"" . $digstring . "\",translatednum=\"". $translatednum ."\"\n";
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio)) {
- flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . $line . "]","Diag") ;
- }
- ---------------------------- */
- $j = 0 ;
- $pgwSingleCommand = explode("*",$pgwCommandString) ;
- while(isset($pgwSingleCommand[$j]))
- {
- if($pgwSingleCommand[$j] != NULL || $pgwSingleCommand[$j] != "")
- {
- niwaWriteLog("PGWscripts: Command [".$j."] is [".$pgwSingleCommand[$j]."]","Diag") ;
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $pgwSingleCommand[$j] . "]","Diag") ;
- fwrite($stdio,$pgwSingleCommand[$j] . "\n");
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio))
- {
- if($Flush) flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
- } // while
- } // if
- $j++ ;
- } // end of while
- // -------------------------------
- $command = "quit\n";
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio)) {
- if($Flush) flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line) . "]","Diag") ;
- }
- // -------------------------------
- $command = "exit\n";
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio)) {
- if($Flush) flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line). "]","Diag") ;
- }
- // -------------------------------
- $command = "logout\n" ;
- if($SSH_debug) niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- // Then u can fetch the stream to see what happens on stdio
- while($line = fgets($stdio)) {
- if($Flush) flush();
- // echo $line."<br />";
- if($SSH_debug) niwaWriteLog("SSH: Got Response [" . stripN($line). "]","Diag") ;
- }
- // -------------------------------
- /* ------------cut----------------
- $command = ":wq\n";
- niwaWriteLog("SSH: Sending command [" . $command . "]","Diag") ;
- fwrite($stdio,$command);
- usleep($SSH_usleep);
- $search = "written" ;
- $search_preg = "/" . $search . "/i" ;
- while($line = fgets($stdio)) {
- flush();
- $line=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$line);
- if (preg_match($search_preg, $line)) niwaWriteLog("SSH: Found command [" . $search . "] OK","Diag") ;
- niwaWriteLog("SSH: Got Response [" . $line . "]","Diag") ;
- }
- -------------------- */
- // It's always cleaner to close all stream
- niwaWriteLog("SSH: PGW Code Finished Result [" . $returncode . "]","Diag") ;
- usleep($SSH_usleep); // is this needed?
- fclose($stdio);
- return $returncode ;
- } // End of function PGWscripts
- function stripN($command)
- {
- $command = str_replace("\r","",$command) ;
- $command = str_replace("\n","",$command) ;
- return($command) ;
- } // stripN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement