Solved

Running UNIX commands in PHP

Posted on 2004-04-14
5
621 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:manthanein
5 Comments
 
LVL 4

Expert Comment

by:Georgiana Gligor
ID: 10830721
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
 
LVL 2

Expert Comment

by:j_dyer
ID: 10831748
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
 
LVL 7

Author Comment

by:manthanein
ID: 10831885
j_dyer,

      thank you for answering my question.. can you give me an example script to connect to telnet using  proc-open.
0
 
LVL 2

Accepted Solution

by:
j_dyer earned 500 total points
ID: 10832011
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
 
LVL 8

Expert Comment

by:HaydnH
ID: 10832918
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now