473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Capturing output from a system command

Hi,

I'm new to PHP, im used to coding in asp, so please bare with me. What i would like is to output the result to a text file
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $_ip = $_SERVER['REMOTE_ADDR'];
  3. echo "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>";
  4. echo "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'>&nbsp;<input type='submit' value='Route' class='input_login'></form>";
  5. if($_GET['do'] == 'route')
  6. {
  7. $_domain = $_GET['domain'];
  8.         echo "<pre>";
  9.                    system ("tracert $_domain") ;
  10.           echo "</pre>";
  11. }
  12. echo "<br>";
  13.  
  14.  
  15. $your_data = "This is the data to be stored in the text file.";
  16.  
  17. // Open the file and erase the contents if any
  18. $fp = fopen("hostnamehere.txt", "w");
  19.  
  20. // Write the data to the file
  21. fwrite($fp, $your_data);
  22.  
  23. // Close the file
  24. fclose($fp);
  25.  
  26. ?> 
I have combined 2 files, a tracert file and one that writes to a text file, how do i define the output of the traceroute as a variable so i can pass it to the part that writes to a text file, i have tried a few ways but end up getting errors.

Any help appreciated.

Chris.
Aug 10 '07 #1
4 4631
pbmods
5,821 Expert 4TB
Heya, Chris. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Changed thread title to better describe the problem (did you know that threads whose titles contain phrases such as, 'noob' or 'need help' actually get FEWER responses?).

Check out the exec() function.
Aug 10 '07 #2
jx2
228 100+
i am not sure i dio understand what you asking about
do you want to write to file output from system() ?

[php]
$output = exec ("tracert $_domain",$a) ;

foreach ($a as $k=>$v){
$your_data = $your_data. "$v\n";
}
[/php]


i thought system() return output ...
well...
thanks both of you
now i know :-)

regards
jx2
Aug 11 '07 #3
Atli
5,058 Expert 4TB
Hi.

This is also possible.
Expand|Select|Wrap|Line Numbers
  1. // Start output buffering
  2. ob_start();
  3.  
  4. // Do tracert
  5. system ("tracert $_domain");
  6.  
  7. // Get the output buffer
  8. $buffer = ob_get_contents();
  9.  
  10. // Stop and clean the output buffer
  11. ob_end_clean();
  12.  
  13. // Write to file
  14. $fh = fopen("myfile.txt", "w");
  15. fwrite($fh, $buffer);
  16. fclose($fh);
  17.  
Aug 11 '07 #4
pbmods
5,821 Expert 4TB
Heya, jx2.

system() outputs the result.
exec() returns it. Sort of.

exec() *returns* the last line of the output. If you want to capture *all* of the output, you need to pass a variable (by reference) as the second parameter.

http://php.net/manual/en/function.exec.php
Aug 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jerrygarciuh | last post by:
Hello, I have a client who has decided to output the OO PHP/mySQL site I have made for them onto a cd. I realize that $html = include('somefile.php'); gets 1 for success as its return value....
4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
5
by: Moosebumps | last post by:
I have a large set of Python scripts that interface with command line utilities (primarily Perforce). I am currently capturing ALL the text output in order to get results and such. I am using the...
2
by: Hoegje | last post by:
I am writing a C++ program, which should create a sub- process to start a telnet session to another server. Then it should login to that server (on the telnet login) and execute one or more...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: Doru-Catalin Togea | last post by:
Hi! On solaris, I call a program from python with os.system("some command > outputFile.txt" ) This redirects the output of "some command"'s stdout to "outputFile.txt". However I need to...
6
by: Ed Leafe | last post by:
I've been approached by a local business that has been advised that they need to start capturing and archiving their instant messaging in order to comply with Sarbanes-Oxley. The company is largely...
4
by: amjadcsu | last post by:
Hi I am trying to execute a command using os.system. this command lists the number of nodes alive in a cluster. I would like to capture the output in list/array in python. IS it possible.?/ ...
2
by: jallam | last post by:
Hi, I have a Python script to upload the Invoice data which is in .csv file to OTM (GLOG) system. I'm running this script using the command(given below) using a shell script and capturing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.