Jump to content

Need help making output from passthru() function readable


Brent

Recommended Posts

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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[quote author=Vikas Jayna link=topic=102979.msg409634#msg409634 date=1154707994]
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
[/quote]

I still get the same output. $ipconfig is not getting split, \n is only appended to the end of the entire string.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.