Solved

Running UNIX commands in PHP

Posted on 2004-04-14
5
623 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 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