Jump to content


Photo

Need help making output from passthru() function readable


  • Please log in to reply
8 replies to this topic

#1 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 02:15 PM

This page will only be accessed from the localhost. I am trying to allow a technician to change the ip configuration by simply entering the information into a form. The code works great except that I cannot find a way to display the results in a readable form, it's one continuous line. I know I need to have the \n to designate a new line but I am not able to do that when passing command line results. I've tried using passthru(), system(), and redirecting to a txt file and then using fread(). I would greatly appreciate any suggestions anyone has.

I need to display either the results of the "netsh" command or use the output of a seperate "ipconfig" command.

<?php
$ipaddress = $_POST['ipaddress'];
$subnetmask = $_POST['subnetmask'];
$gateway = $_POST['defaultgateway'];
$DnsServer = $_POST['dnsserver'];
$wait = "Changing Network Settings...";

echo "<br>".$wait."<br><p>";

$filename = 'savesettings.txt';
$fp = fopen($filename, "w");

$line1 = "pushd interface \r\n";
$line2 = "reset all\r\n";
$line3 = "popd \r\n";
$line4 = "pushd interface ipv6 \r\n";
$line5 = "uninstall \r\n";
$line6 = "popd \r\n";
$line7 = "pushd interface ipv6 isatap \r\n";
$line8 = "popd \r\n";
$line9 = "pushd interface ipv6 6to4 \r\n";
$line10 = "reset \r\n";
$line11 = "popd \r\n";
$line12 = "pushd interface portproxy \r\n";
$line13 = "reset \r\n";
$line14 = "popd \r\n";
$line15 = "pushd interface ip \r\n";

$line16 = "set address name=\"Local Area Connection\" source=static addr=$ipaddress mask=$subnetmask \r\n";
$line17 = "set address name=\"Local Area Connection\" source=static gateway=$gateway gwmetric=0 \r\n";
$line18 = "set dns name=\"Local Area Connection\" source=static addr=$DnsServer \r\n";

$write = fputs($fp, $line1);
$write = fputs($fp, $line2);
$write = fputs($fp, $line3);
$write = fputs($fp, $line4);
$write = fputs($fp, $line5);
$write = fputs($fp, $line6);
$write = fputs($fp, $line7);
$write = fputs($fp, $line8);
$write = fputs($fp, $line9);
$write = fputs($fp, $line10);
$write = fputs($fp, $line11);
$write = fputs($fp, $line12);
$write = fputs($fp, $line13);
$write = fputs($fp, $line14);
$write = fputs($fp, $line15);
$write = fputs($fp, $line16);
$write = fputs($fp, $line17);
$write = fputs($fp, $line18);
fclose($fp);

passthru('netsh exec savesettings.txt');

?>

I know this may not be the most efficient way to do this but it is my first php project. I welcome any suggestions for a more efficient way to do it.

Thanks in advance!


#2 tony5429

tony5429
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 04 August 2006 - 02:49 PM

Have you tried using explode() to split the string into an array of strings between the \n's and then using a for loop to display the array of strings with echo "<br>"; between each one?

#3 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 02:58 PM

No I haven't. I might be able to use that later but the problem is that I don't know how to get the \n into the output.

For example, if I do:

passthru('ipconfig /all');

or

system('ipconfig /all', $output);

or

exec('ipconfig /all > output.txt');

All of these methods strip out the new lines and I don't know of a way to split them again.

Thanks.

#4 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 03:36 PM

I tried explode() but I get the same output.

system('ipconfig /all', $output);
$ipconfig = explode(':',$output);
echo $ipconfig[1];
echo $ipconfig[2];
echo $ipconfig[3];

Windows IP Configuration Host Name . . . . . . . . . . . . : Khaos1 Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Unknown IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8139/810x Family Fast Ethernet NIC Physical Address. . . . . . . . . : 00-13-D3-11-09-08 Dhcp Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 102.53.94.254 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 102.53.94.200 DNS Servers . . . . . . . . . . . : 102.53.94.200


#5 Vikas Jayna

Vikas Jayna
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationNoida, Delhi, India

Posted 04 August 2006 - 04:13 PM

You need to do the following:-
1) Explode the string to get the array
2) Then loop on the array
3) Put a newline character in each iteration of the loop.

system('ipconfig /all', $output);
$ipconfig = explode(':',$output);

for($i=0;$i<count($ipconfig);$i++)
{
    echo $ipconfig[$i] . "\n";
}

This should work
Vikas Jayna,
Project Manager - Jeevansathi.com
7 yrs. exp. in LAMP
Certified Mysql DBA
Zend Certified Engineer

#6 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 04:35 PM

You need to do the following:-
1) Explode the string to get the array
2) Then loop on the array
3) Put a newline character in each iteration of the loop.

system('ipconfig /all', $output);
$ipconfig = explode(':',$output);

for($i=0;$i<count($ipconfig);$i++)
{
     echo $ipconfig[$i] . "\n";
}

This should work


I still get the same output. $ipconfig is not getting split, \n is only appended to the end of the entire string.

#7 Vikas Jayna

Vikas Jayna
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationNoida, Delhi, India

Posted 04 August 2006 - 04:39 PM

Are you executing the script through the browser? If yes then replace "\n" with "<br>" in the above code
Vikas Jayna,
Project Manager - Jeevansathi.com
7 yrs. exp. in LAMP
Certified Mysql DBA
Zend Certified Engineer

#8 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 04:47 PM

Yes, I am running the script through the browser. I tried " " but I still get the same results. I replace "\n" with '\n' and verified that it is only appended once to the very end of the string. Neither explode() or split() is splitting the string.

#9 Brent

Brent
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 07:19 PM

Here is the solution if anyone is interested.

The system function automatically prints the output but did not display properly because the browser was trying to format the text. Echoing the preformatted text html tag fixed the problem.

echo '<pre>';
system('ipconfig /all');
echo '</pre>';

Thanks to JayShields on DevNetwork.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users