Sending data to STDIN of a process using echo

Sending data to STDIN of a process using echo

Post by Fred LaFor » Mon, 22 Jul 2002 20:16:11



Hello,

   I wrote a short test program in C, "get_age" , as follows:

#include <stdio.h>
int main()
{
    int age;
    printf("What is your age?\n");
    scanf("%d", &age);
    printf("Your age is %d\n", age);
    return 0;

Quote:}

It compiles without error and runs correctly from the shell.  The
problem comes when I run it...

# ./get_age
# What is your age?
#

...then, on another terminal, try to send input to get_age by using
the echo command and get_age's listing in the /proc directory, like this:

# ps -aux | grep get_age
# root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00 ./get_age
# echo "36\r" > /proc/1880/fd/0

The get_age program immediately shows the new input, "36\r", but the new
input somehow doesn't flush into get_age (I think), and so is left unprocessed
forever...

# ./get_age
# What is your age?
# 36\r
# hangs...........

It just hangs there, never getting to the "Your age is 36." output.
What can I do to my echo statement to get the get_age program to "grab"
the input from STDIN and actually process it?  Do I need
to use some sort of flush command (I can't find one in the bash
shell).  I don't want to modify the get_age program, but would like to know
how to send data to the STDIN of a program that is identified by process
number, and get that program to "accept" or "take in" that data, and
then process it.

Any help would be greatly appreciated.  Thank you very much.

Sincerely,

Fred LaForge

 
 
 

Sending data to STDIN of a process using echo

Post by Fred LaFor » Mon, 22 Jul 2002 20:21:04


Hello,

   I wrote a short test program in C, "get_age" , as follows:

#include <stdio.h>
int main()
{
    int age;
    printf("What is your age?\n");
    scanf("%d", &age);
    printf("Your age is %d\n", age);
    return 0;

Quote:}

It compiles without error and runs correctly from the shell.  The
problem comes when I run it...

# ./get_age
# What is your age?
#

...then, on another terminal, try to send input to get_age by using
the echo command and get_age's listing in the /proc directory, like
this:

# ps -aux | grep get_age
# root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
./get_age
# echo "36\r" > /proc/1880/fd/0

The get_age program immediately shows the new input, "36\r", but the
new
input somehow doesn't flush into get_age (I think), and so is left
unprocessed forever...

# ./get_age
# What is your age?
# 36\r
# hangs...........

It just hangs there, never getting to the "Your age is 36." output.
What can I do to my echo statement to get the get_age program to
"grab"
the input from STDIN and actually process it?  Do I need
to use some sort of flush command (I can't find one in the bash
shell).  I don't want to modify the get_age program, but would like to
know
how to send data to the STDIN of a program that is identified by
process
number, and get that program to "accept" or "take in" that data, and
then process it.

Any help would be greatly appreciated.  Thank you very much.

Sincerely,

Fred LaForge

 
 
 

Sending data to STDIN of a process using echo

Post by Bill Marc » Mon, 22 Jul 2002 22:25:05


On 21 Jul 2002 04:21:04 -0700,

Quote:

>The get_age program immediately shows the new input, "36\r", but the
>new
>input somehow doesn't flush into get_age (I think), and so is left
>unprocessed forever...

># ./get_age
># What is your age?
># 36\r
># hangs...........

echo -e "36\r" ...
 
 
 

Sending data to STDIN of a process using echo

Post by Roland Rodrigu » Mon, 22 Jul 2002 23:59:52



> Hello,

>    I wrote a short test program in C, "get_age" , as follows:

> #include <stdio.h>
> int main()
> {
>     int age;
>     printf("What is your age?\n");
>     scanf("%d", &age);
>     printf("Your age is %d\n", age);
>     return 0;
> }

> It compiles without error and runs correctly from the shell.  The
> problem comes when I run it...

> # ./get_age
> # What is your age?
> #

> ...then, on another terminal, try to send input to get_age by using
> the echo command and get_age's listing in the /proc directory, like
> this:

> # ps -aux | grep get_age
> # root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
> ./get_age
> # echo "36\r" > /proc/1880/fd/0

> The get_age program immediately shows the new input, "36\r", but the
> new
> input somehow doesn't flush into get_age (I think), and so is left
> unprocessed forever...

> # ./get_age
> # What is your age?
> # 36\r
> # hangs...........

> It just hangs there, never getting to the "Your age is 36." output.
> What can I do to my echo statement to get the get_age program to
> "grab"
> the input from STDIN and actually process it?  Do I need
> to use some sort of flush command (I can't find one in the bash
> shell).  I don't want to modify the get_age program, but would like to
> know
> how to send data to the STDIN of a program that is identified by
> process
> number, and get that program to "accept" or "take in" that data, and
> then process it.

> Any help would be greatly appreciated.  Thank you very much.

> Sincerely,

> Fred LaForge

/proc/[pid]/fd/0 (and .../1 and .../2 too) are actually symlinks to the
/dev/ttyN or /dev/pts/N of the terminal or xterm in which your program
runs.  I suggest you use a fifo.
--
roland
 
 
 

Sending data to STDIN of a process using echo

Post by Fred LaFor » Fri, 26 Jul 2002 16:11:43


Hi Bill,
    Thanks for replying.  I tried using the -e option for the echo
command:

    echo -e "36\r" > /proc/1973/fd/0

 and the input 36 shows up on the other terminal running the
get_age program, but it still just hangs there, not processing
the number and moving on to the next printf statement in the program.

--Fred LaForge


> On 21 Jul 2002 04:21:04 -0700,

> >The get_age program immediately shows the new input, "36\r", but the
> >new
> >input somehow doesn't flush into get_age (I think), and so is left
> >unprocessed forever...

> ># ./get_age
> ># What is your age?
> ># 36\r
> ># hangs...........

> echo -e "36\r" ...

 
 
 

Sending data to STDIN of a process using echo

Post by Fred LaFor » Fri, 26 Jul 2002 16:14:58


Thank you for replying Roland,

   I would like to use a fifo but I need to be able to send a message to
an arbitrary program supplied by someone else.  I am now looking into using
Expect to be able to interactively send input to a program.

--Fred LaForge



> > Hello,

> >    I wrote a short test program in C, "get_age" , as follows:

> > #include <stdio.h>
> > int main()
> > {
> >     int age;
> >     printf("What is your age?\n");
> >     scanf("%d", &age);
> >     printf("Your age is %d\n", age);
> >     return 0;
> > }

> > It compiles without error and runs correctly from the shell.  The
> > problem comes when I run it...

> > # ./get_age
> > # What is your age?
> > #

> > ...then, on another terminal, try to send input to get_age by using
> > the echo command and get_age's listing in the /proc directory, like
> > this:

> > # ps -aux | grep get_age
> > # root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
> > ./get_age
> > # echo "36\r" > /proc/1880/fd/0

> > The get_age program immediately shows the new input, "36\r", but the
> > new
> > input somehow doesn't flush into get_age (I think), and so is left
> > unprocessed forever...

> > # ./get_age
> > # What is your age?
> > # 36\r
> > # hangs...........

> > It just hangs there, never getting to the "Your age is 36." output.
> > What can I do to my echo statement to get the get_age program to
> > "grab"
> > the input from STDIN and actually process it?  Do I need
> > to use some sort of flush command (I can't find one in the bash
> > shell).  I don't want to modify the get_age program, but would like to
> > know
> > how to send data to the STDIN of a program that is identified by
> > process
> > number, and get that program to "accept" or "take in" that data, and
> > then process it.

> > Any help would be greatly appreciated.  Thank you very much.

> > Sincerely,

> > Fred LaForge

> /proc/[pid]/fd/0 (and .../1 and .../2 too) are actually symlinks to the
> /dev/ttyN or /dev/pts/N of the terminal or xterm in which your program
> runs.  I suggest you use a fifo.

 
 
 

Sending data to STDIN of a process using echo

Post by Eric Worral » Fri, 26 Jul 2002 18:37:10


Have you considered using sockets? Sockets give a program the capacity
to communicate with any number of arbitrary clients, each client will
have a separate connection. You can use file i/o commands with sockets
or socket commands (send & recv) for better control. Sockets can also be
used for sending small 'connectionless' messages and responses.

Eric Worrall


> Thank you for replying Roland,

>    I would like to use a fifo but I need to be able to send a message to
> an arbitrary program supplied by someone else.  I am now looking into using
> Expect to be able to interactively send input to a program.

> --Fred LaForge



> > > Hello,

> > >    I wrote a short test program in C, "get_age" , as follows:

> > > #include <stdio.h>
> > > int main()
> > > {
> > >     int age;
> > >     printf("What is your age?\n");
> > >     scanf("%d", &age);
> > >     printf("Your age is %d\n", age);
> > >     return 0;
> > > }

> > > It compiles without error and runs correctly from the shell.  The
> > > problem comes when I run it...

> > > # ./get_age
> > > # What is your age?
> > > #

> > > ...then, on another terminal, try to send input to get_age by using
> > > the echo command and get_age's listing in the /proc directory, like
> > > this:

> > > # ps -aux | grep get_age
> > > # root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
> > > ./get_age
> > > # echo "36\r" > /proc/1880/fd/0

> > > The get_age program immediately shows the new input, "36\r", but the
> > > new
> > > input somehow doesn't flush into get_age (I think), and so is left
> > > unprocessed forever...

> > > # ./get_age
> > > # What is your age?
> > > # 36\r
> > > # hangs...........

> > > It just hangs there, never getting to the "Your age is 36." output.
> > > What can I do to my echo statement to get the get_age program to
> > > "grab"
> > > the input from STDIN and actually process it?  Do I need
> > > to use some sort of flush command (I can't find one in the bash
> > > shell).  I don't want to modify the get_age program, but would like to
> > > know
> > > how to send data to the STDIN of a program that is identified by
> > > process
> > > number, and get that program to "accept" or "take in" that data, and
> > > then process it.

> > > Any help would be greatly appreciated.  Thank you very much.

> > > Sincerely,

> > > Fred LaForge

> > /proc/[pid]/fd/0 (and .../1 and .../2 too) are actually symlinks to the
> > /dev/ttyN or /dev/pts/N of the terminal or xterm in which your program
> > runs.  I suggest you use a fifo.

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

Sending data to STDIN of a process using echo

Post by Michael Fran » Sat, 27 Jul 2002 13:26:35


Use the 'pipe' function of your shell:

$ echo "19" | ./get_age

Michael

-------------------------  .~.
Michael Frank              /v\

------------------------ /(   )\
                          ^`~'^


Quote:> Hello,

>    I wrote a short test program in C, "get_age" , as follows:

> #include <stdio.h>
> int main()
> {
>     int age;
>     printf("What is your age?\n");
>     scanf("%d", &age);
>     printf("Your age is %d\n", age);
>     return 0;
> }

> It compiles without error and runs correctly from the shell.  The
> problem comes when I run it...

> # ./get_age
> # What is your age?
> #

> ...then, on another terminal, try to send input to get_age by using
> the echo command and get_age's listing in the /proc directory, like
> this:

> # ps -aux | grep get_age
> # root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
> ./get_age
> # echo "36\r" > /proc/1880/fd/0

> The get_age program immediately shows the new input, "36\r", but the
> new
> input somehow doesn't flush into get_age (I think), and so is left
> unprocessed forever...

> # ./get_age
> # What is your age?
> # 36\r
> # hangs...........

> It just hangs there, never getting to the "Your age is 36." output.
> What can I do to my echo statement to get the get_age program to
> "grab"
> the input from STDIN and actually process it?  Do I need
> to use some sort of flush command (I can't find one in the bash
> shell).  I don't want to modify the get_age program, but would like to
> know
> how to send data to the STDIN of a program that is identified by
> process
> number, and get that program to "accept" or "take in" that data, and
> then process it.

> Any help would be greatly appreciated.  Thank you very much.

> Sincerely,

> Fred LaForge

 
 
 

Sending data to STDIN of a process using echo

Post by Fred LaFor » Sun, 28 Jul 2002 14:40:10


Hello Eric,

   If I use sockets don't I have to write code for them in the
program that I wish to send a message to?  This is a problem because
I am trying to set up a free, online compiler where a student could
enter the source code for simple applications like "get_age", click
a button to submit this code to the server, have the c code compiled
on the server and get the success or
error results back (I've got that part running),and then run their
program (on the server) and see the results in a text area in their
browser.  They would be able to send back responses like "36" if their
program asked for their age,etc., then wait for the response from
their
program such as "You are 36; what is your height:" and then enter
their height and send that to the server for a response from their
program, and so on.
   Wouldn't the problem with sockets be that the student would have to
add socket code to their get_age type program, so that it could be
compiled into it?

Sincerely,

Bill Northway


> Have you considered using sockets? Sockets give a program the capacity
> to communicate with any number of arbitrary clients, each client will
> have a separate connection. You can use file i/o commands with sockets
> or socket commands (send & recv) for better control. Sockets can also be
> used for sending small 'connectionless' messages and responses.

> Eric Worrall


> > Thank you for replying Roland,

> >    I would like to use a fifo but I need to be able to send a message to
> > an arbitrary program supplied by someone else.  I am now looking into using
> > Expect to be able to interactively send input to a program.

> > --Fred LaForge



> > > > Hello,

> > > >    I wrote a short test program in C, "get_age" , as follows:

> > > > #include <stdio.h>
> > > > int main()
> > > > {
> > > >     int age;
> > > >     printf("What is your age?\n");
> > > >     scanf("%d", &age);
> > > >     printf("Your age is %d\n", age);
> > > >     return 0;
> > > > }

> > > > It compiles without error and runs correctly from the shell.  The
> > > > problem comes when I run it...

> > > > # ./get_age
> > > > # What is your age?
> > > > #

> > > > ...then, on another terminal, try to send input to get_age by using
> > > > the echo command and get_age's listing in the /proc directory, like
> > > > this:

> > > > # ps -aux | grep get_age
> > > > # root      1880  0.1  0.3  1264  380 pts/2    S    01:33   0:00
> > > > ./get_age
> > > > # echo "36\r" > /proc/1880/fd/0

> > > > The get_age program immediately shows the new input, "36\r", but the
> > > > new
> > > > input somehow doesn't flush into get_age (I think), and so is left
> > > > unprocessed forever...

> > > > # ./get_age
> > > > # What is your age?
> > > > # 36\r
> > > > # hangs...........

> > > > It just hangs there, never getting to the "Your age is 36." output.
> > > > What can I do to my echo statement to get the get_age program to
> > > > "grab"
> > > > the input from STDIN and actually process it?  Do I need
> > > > to use some sort of flush command (I can't find one in the bash
> > > > shell).  I don't want to modify the get_age program, but would like to
> > > > know
> > > > how to send data to the STDIN of a program that is identified by
> > > > process
> > > > number, and get that program to "accept" or "take in" that data, and
> > > > then process it.

> > > > Any help would be greatly appreciated.  Thank you very much.

> > > > Sincerely,

> > > > Fred LaForge

> > > /proc/[pid]/fd/0 (and .../1 and .../2 too) are actually symlinks to the
> > > /dev/ttyN or /dev/pts/N of the terminal or xterm in which your program
> > > runs.  I suggest you use a fifo.

 
 
 

Sending data to STDIN of a process using echo

Post by Eric Worral » Sun, 28 Jul 2002 18:57:34


Thankyou for the information.

How do you plan to deliver the service? Will the students be in a
classroom or will the service be delivered via the web / internet
browsers?

If you decide to use sockets, the interface the students use to
communicate with the server would need to use sockets but the student's
code would not require sockets to function.

Sockets in Linux behave very similarly to files. Telnet uses sockets to
connect to a remote machine but once connected any program you run on
the remote machine is not aware it is communicating with you via sockets
- as far as the remote code is concerned it is receiving input from
stdin and writing to stdout and stderr.

There are a lot of ways of delivering your proposed service. Telnet and
ftp (or the secure equivalents) provide all the functionality required
to upload files and execute compile and run commands on a remote
machine.

Alternatively, you could provide the service via a the web. Consider the
following example:

script: test.cgi
-----
#!/usr/bin/perl
$| = 1; # set unbuffered
open STDERR, ">&STDOUT"; # redirect error messages to normal output
print "Content-type: text/plain\n\n"; # notify the browser of the data
type
print "ls -l\n"; # explain what is about to be run
exec "/bin/ls", "-l"; # execute the ls command
-----

If you ran the script using a browser (e.g.
http://www.myserver.com/cgi-bin/test.cgi), the browser would display
something like the following:
ls -l
total 1
rwxr-xr-x ... ls

It would be more difficult to provide an interactive service via http
(i.e. program asks a question, student provides a response) but not
impossible.

I hope this helps. Let me know more about your requirements and I shall
help you further.

Eric Worrall


> Hello Eric,

>    If I use sockets don't I have to write code for them in the
> program that I wish to send a message to?  This is a problem because
> I am trying to set up a free, online compiler where a student could
> enter the source code for simple applications like "get_age", click
> a button to submit this code to the server, have the c code compiled
> on the server and get the success or
> error results back (I've got that part running),and then run their
> program (on the server) and see the results in a text area in their
> browser.  They would be able to send back responses like "36" if their
> program asked for their age,etc., then wait for the response from
> their
> program such as "You are 36; what is your height:" and then enter
> their height and send that to the server for a response from their
> program, and so on.
>    Wouldn't the problem with sockets be that the student would have to
> add socket code to their get_age type program, so that it could be
> compiled into it?

> Sincerely,

> Bill Northway


> > Have you considered using sockets? Sockets give a program the capacity
> > to communicate with any number of arbitrary clients, each client will
> > have a separate connection. You can use file i/o commands with sockets
> > or socket commands (send & recv) for better control. Sockets can also be
> > used for sending small 'connectionless' messages and responses.

> > Eric Worrall

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

Sending data to STDIN of a process using echo

Post by jonat.. » Tue, 30 Jul 2002 10:11:02


what if a student submit an endless-loop program or a resource consuming
program?
 
 
 

Sending data to STDIN of a process using echo

Post by Eric Worral » Wed, 31 Jul 2002 00:03:01


help ulimit
man 3 ulimit
man edquota

You could also write a script which killed any process which took too
many resources.

You need to also be careful about hacking. Many forms of attack require
access to a normal user account. If a hacker managed to get a student
account they could attempt to attack your machine using compiled
programs. Many types of attack use compiled code on the target machine.

Eric Worrall


> what if a student submit an endless-loop program or a resource consuming
> program?

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

Sending data to STDIN of a process using echo

Post by Fred LaFor » Fri, 02 Aug 2002 06:20:11


I plan to monitor cpu usage for each app run by the user, and kill
it after a certain number of cpu cycles have been used.
In general security will be handled by having a user set up a user account
with severaly limited powers on the server.  But before dealing with the
security I need to solve the problem of dealing with sending back and forth the
output and user response of an interactive program directly to the user
browser, without using a telnet program etc.
--Fred

> what if a student submit an endless-loop program or a resource consuming
> program?

 
 
 

Sending data to STDIN of a process using echo

Post by Eric Worral » Fri, 02 Aug 2002 08:42:28



> I plan to monitor cpu usage for each app run by the user, and kill
> it after a certain number of cpu cycles have been used.
> In general security will be handled by having a user set up a user account
> with severaly limited powers on the server.  But before dealing with the
> security I need to solve the problem of dealing with sending back and forth the
> output and user response of an interactive program directly to the user
> browser, without using a telnet program etc.
> --Fred


> > what if a student submit an endless-loop program or a resource consuming
> > program?

Student -> browser -> Server CGI script -> INET socket -> monitor
process -> shell

The shell has stdin, stdout and stderr redirected to the monitor
process.

The monitor process stores output from the shell and passes the output
to the Server CGI script via the INET socket when requested. The monitor
process also passes the student's input to the shell.

The server CGI script is invoked by http requests from the browser. The
CGI script passes student input to the monitor process and waits for
responses from the shell (via the monitor process). You need to decide
the rules governing the Server CGI script (e.g. wait until the shell has
not produced output for 5 seconds then respond to the browser).

The browser accepts input from the student and displays responses from
the shell. The browser should also include buttons for interrupting and
killing rogue processes on the shell.

The browser -> Server CGI script would need a mechanism for identifying
the monitor process or shell with which to communicate. A simple
solution would be to store the INET socket address in a cookie, but this
would be very insecure, your students would be able to hijack other
student shells by changing their socket address cookie. You could
experiment with encrypted tokens to make it difficult to hijack another
student's session by guessing the value of their cookie.

Hope this helps.

Eric Worrall

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

1. Sending input to process using echo

Hi,

I wrote this short test program in C, "get_age" :

#include <stdio.h>
int main()
{
    int age;
    printf("What is your age?\n");
    scanf("%d", &age);
    printf("Your age is %d\n", age);
    return 0;

It compiles without error and runs correctly from the shell.  The
problem comes when I run it...

# ./get_age
# What is your age?
#

...then, on another Xterm, try to send input to get_age by using
 the echo command and getage's ps number:

# echo "36\r" > /proc/1880/fd/0

The get_age program immediately shows the new input, "36\r", but the
new input doesn't flush into get_age (I think), and so is left
unprocessed forever...

# ./get_age
# What is your age?
# 36\r
# hangs...........

When I then type in "45" and press enter on the original Xterm where
get_age is running, the get_age program there prints out, "Your age is
45" correctly, ignoring the 36 sent to it by echo.  Why doesn't
get_age process the number 36 that was originally sent to it by echo?

How do I (or can I?) send input to the STDIN of a running program like
get_age, and have that program process it?  I would like to use the
shell to do this; I don't want to modify the code of the "get_age"
program.  Am I using echo incorrectly?  Should I put some other sort
of control character(s) after "36" beside the "\r"? Are there other
methods using the shell that I can write to the STDIN of get_age (and
have it process my input) without physically typing the input in on
the Xterm where I started get_age?

 Any help would be greatly appreciated.
--Bill Northway

2. Setting Directory/File permissions???

3. passing stdin to a forked process, after writing some data to the process ..

4. gcc on Freeware CD, compile error

5. Sending XML data to Apache web server and capturing this sent data from the Apache server

6. FreeBSD 4.8 jail

7. Sending binary data using send(2)

8. WANTED - log files

9. daemon process can send message to stdin ?

10. Using separate window child process for stdin/stdout

11. Using File descriptor 0 (stdin) in child process

12. Can a parent process place input as stdin of child process?

13. tcp send bigger data faster then small data