Running UNIX commands in PHP

Hello Everybody,

         Is there any way for you to run a batch of UNIX commands in PHP..  for example execute a telnet  command,  and  issuing the  username, password  programmatically, then capture the feedback  if login was successful.  
         In Unix they say to use "Expect"   and in windows Visual Basic applications I used DartTelnet ocx, for this functionality.  Now I want to create a web based application, so is there any way to do it in PHP..  
        Thank you taking time in reading this.

regards,
Michelle
LVL 7
manthaneinAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
j_dyerConnect With a Mentor Commented:
This (untested) code should do the job:

<? php

$host = '192.168.0.1';
$user = 'user';
$pass = 'pass';
$cmd = 'telnet';

$descriptors = array(
  0 => array( "pipe" , "r" ) , //stdin
  1 => array( "pipe" , "w" ) , //stdout
  2 => array( "file" , '/tmp/stderr.txt' , 'a' )
);

$process = proc_open( "$cmd $host" , $descriptors , $pipes );

if( is_resource( $process ) ) {

      while( 1 ) {
            $text = fgets( $pipes[1] , 1024 );
            if( $text == 'login' ) {
                  fputs( $pipes[0] , "$user\n";
            elseif( $text == 'Password' ) {
                  fputs( $pipes[0] , "$pass\n";
                  break; // Password now sent - exit out of login bit
            }
      }

// File handles still open here - do what you want with them but, when
// finished, make sure they're all closed:
      fclose( $pipes[0] );
      fclose( $pipes[1] );
      fclose( $pipes[2] );
      proc_close( $process );

} else {
      print "Something went wrong!\n";
}

?>
0
 
Georgiana GligorCTO / OwnerCommented:
you can run commands with the exec function
take a look here:
http://ro.php.net/manual/en/ref.exec.php

I also recommend you get accustomed to using the online manual at www.php.net, because it contains extremely valuable user comments

Georgiana
0
 
j_dyerCommented:
exec is not going to work, since that will just, in your example, run 'telnet', and then sit there, waiting for the username
to be opened. Unfortunatly, exec will not allow PHP to access the stdin and stdout of the unix commands.

I think that you want to use the proc_open function - http://uk.php.net/proc-open - this allows you to open pipes in your
PHP program, which you then pass to the proc_open command, together with the name of the unix command you want to
execute. The pipes are then used for the programs stdin, stdout and stderr, so your php program can read and write to the
process.

As far as running a batch of Unix commands is concerned; you can either open each command individually in your PHP program,
or write a shell script and execute that from your PHP program.

I suspect my description of this is not as good as it could be...let me know if you need further clarification.
0
 
manthaneinAuthor Commented:
j_dyer,

      thank you for answering my question.. can you give me an example script to connect to telnet using  proc-open.
0
 
HaydnHCommented:
A Quick not on security for above: (No points for this please - it's just a note!)

If you're planning on doing something like the above, for security reasons, grab the username and password from a php include - don't put them straight in to the php file.

Step 1:
---------
Create an include path (if needed) and add the include path to your include_path line of the php.ini, e.g:

include_path  = ./:/usr/local/lib/php:/usr/local/apache/phpIncludes

And restart apache.


Step 2:
---------
Create a php include file as follows in the above path: (named includeFile.php in Step 3)

<?php
  $username='userID';
  $password='passwd';
?>


Step 3:
--------
Add the following instead of the username="name" etc of the php file:

  include("includeFile.php");

And now use $username and $password.



Alternatively if the directory of the site is password protected anyway you could use $PHP_AUTH_USER and $PHP_AUTH_PW to use the userinfo used to access that path of the web server.

Haydn.
0
All Courses

From novice to tech pro — start learning today.