473,545 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exec() and 'keyboard' input

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<enter>
new-setting 1 <enter>
new-setting2 <enter>
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

Oct 7 '07 #1
3 7309
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:

<?php

$buf = '';
$fp = fopen('php://stdin', 'r');
while (!feof($fp)) {
$buf .= fgets($fp, 1024);
}

print "You wrote: $buf";

?>

(when finished, exit with CRTL+Z+<ENTER>)
#2

<?php

$buf = '';
$fp = fopen('php://stdin', 'r');
while (trim(($line = fgets($fp, 1024)))) {
$buf .= $line;
}

print "You wrote: $buf";

?>

(when finished, just hot <ENTERtwice)
HTH;
JW
Oct 7 '07 #2
On 7 Oct, 22:20, "Janwillem Borleffs" <j...@jwscripts .comwrote:
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:

<?php

$buf = '';
$fp = fopen('php://stdin', 'r');
while (!feof($fp)) {
$buf .= fgets($fp, 1024);

}
print "You wrote: $buf";

?>

(when finished, exit with CRTL+Z+<ENTER>)

#2

<?php

$buf = '';
$fp = fopen('php://stdin', 'r');
while (trim(($line = fgets($fp, 1024)))) {
$buf .= $line;

}
print "You wrote: $buf";

?>

(when finished, just hot <ENTERtwice)

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 <file_I_wrote `;
C.

Oct 8 '07 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

bw*****@gmail.c om 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<enter>
new-setting 1 <enter>
new-setting2 <enter>
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?...rch=0xBACD7433
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)

iD8DBQFHCl/ikA9dCbrNdDMRAo eRAJ4k/lqGpEAV8A2tjR7W O6C8bJwOrACfTfr w
T334hZ+q0EgWgk8 vaWjOKvM=
=Y1NZ
-----END PGP SIGNATURE-----
Oct 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1992
by: Ray | last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I enter data into fields of tables, queries and forms of Access 2003, it automatically switches to Chinese keyboard input. Even though I remove the Chinese keyboard input from Regional applet, the system will automatically reinstall Chinese keyboard input when I enter...
23
7684
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
7
10607
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it were a keyboard. I need to be able to identify input from the scanner and keyboard independently. I've looked at DirectX.DirectInput, and using...
0
6634
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles....
2
8743
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as my keyboard. the USB keyboard is detected as HID keyboard device. the program finds the keyboard if it is attached. and I am getting valid handles....
17
7889
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php" requires user input to run; this causes the TCL script calling the PHP script to hose up and die. Is there a way I can do this so that the TCL...
21
7805
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
8
5285
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control has the focus and handle ctrl-c, ctrl-v, or any other shortcuts. I have the same shortcuts working in my app, but have not determined how to find...
7
2631
by: gregory.lielens | last post by:
Hi, I am using a small python file as an input file (defining constants, parameters, input data, ...) for a python application. The input file is simply read by an exec statement in a specific dictionary, and then the application retrieve all the data it need from the dictionary... Everything is working nicely, but I'd like to have...
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5965
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.