Exec() and "keyboard" input
Exec() and "keyboard" input
am 07.10.2007 17:46:22 von BWGames
Hi there,
I have a script that takes two inputs from the keyboard to change some
settings on some equipment. I would usually run this as e.g.
ben@srv001:/$ change-settings
new-setting 1
new-setting2
Settings successfully changed
ben@srv001:/$
I want to automate this from a PHP script (it will be for my use only
with restricted access via .htaccess etc, so security is not a major
issue).
I can do system or exec("change-settings"); ok, but how do I 'enter'
the input from a php-script? The new-settings1 and 2 will be stored
in variables in PHP.
Thanks,
Ben
Re: Exec() and "keyboard" input
am 07.10.2007 23:20:57 von Janwillem Borleffs
> I can do system or exec("change-settings"); ok, but how do I 'enter'
> the input from a php-script? The new-settings1 and 2 will be stored
> in variables in PHP.
>
Two possibilities:
#1:
$buf = '';
$fp = fopen('php://stdin', 'r');
while (!feof($fp)) {
$buf .= fgets($fp, 1024);
}
print "You wrote: $buf";
?>
(when finished, exit with CRTL+Z+)
#2
$buf = '';
$fp = fopen('php://stdin', 'r');
while (trim(($line = fgets($fp, 1024)))) {
$buf .= $line;
}
print "You wrote: $buf";
?>
(when finished, just hot twice)
HTH;
JW
Re: Exec() and "keyboard" input
am 08.10.2007 13:43:19 von colin.mckinnon
On 7 Oct, 22:20, "Janwillem Borleffs" wrote:
> > I can do system or exec("change-settings"); ok, but how do I 'enter'
> > the input from a php-script? The new-settings1 and 2 will be stored
> > in variables in PHP.
>
> Two possibilities:
>
> #1:
>
>
>
> $buf = '';
> $fp = fopen('php://stdin', 'r');
> while (!feof($fp)) {
> $buf .= fgets($fp, 1024);
>
> }
> print "You wrote: $buf";
>
> ?>
>
> (when finished, exit with CRTL+Z+)
>
> #2
>
>
>
> $buf = '';
> $fp = fopen('php://stdin', 'r');
> while (trim(($line = fgets($fp, 1024)))) {
> $buf .= $line;
>
> }
> print "You wrote: $buf";
>
> ?>
>
> (when finished, just hot twice)
>
> HTH;
> JW
This is for getting keyboard input into PHP not sending keyboard input
to the spawned process - a complete solution would use proc_open() to
be able to process stdout and stderr from the spawned process as well
as sending instructions, but if simplicity is required, then just
write directly to stdin:
$fp=popen("/path/to/change-settings", 'w');
if ($fp) {
fputs($fp,"new-setting 1\n");
fputs($fp,"new-setting 2\n");
fclose($fp);
}
Alternatively write the new setting to a file and redirect stdin for
the command to the file:
$outcome=`/path/to/change-settings
C.
Re: Exec() and "keyboard" input
am 08.10.2007 18:50:42 von Brendan Gillatt
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
bwgames@gmail.com wrote:
> Hi there,
>
> I have a script that takes two inputs from the keyboard to change some
> settings on some equipment. I would usually run this as e.g.
>
> ben@srv001:/$ change-settings
> new-setting 1
> new-setting2
> Settings successfully changed
> ben@srv001:/$
>
> I want to automate this from a PHP script (it will be for my use only
> with restricted access via .htaccess etc, so security is not a major
> issue).
>
> I can do system or exec("change-settings"); ok, but how do I 'enter'
> the input from a php-script? The new-settings1 and 2 will be stored
> in variables in PHP.
>
> Thanks,
>
> Ben
>
Depending on your OS you could echo the variables in:
# change-settings < "settings 1", etc
- --
Brendan Gillatt
brendan {at} brendangillatt {dot} co {dot} uk
http://www.brendangillatt.co.uk
PGP Key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xBACD7433
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
iD8DBQFHCl/ikA9dCbrNdDMRAoeRAJ4k/lqGpEAV8A2tjR7WO6C8bJwOrACf Tfrw
T334hZ+q0EgWgk8vaWjOKvM=
=Y1NZ
-----END PGP SIGNATURE-----