LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C socket : how Execute shell program from server, and passing the output to client? (http://www.linuxquestions.org/questions/programming-9/c-socket-how-execute-shell-program-from-server-and-passing-the-output-to-client-594142/)

penyu 10-24-2007 05:37 AM

C socket : how Execute shell program from server, and passing the output to client?
 
Dear,
I am a new member of this forum.

I'm learning about Linux C socket programming right now and got problem about command line execution from C program.

I have one server daemon (server.c) and client program (client.c). When server running, client try to execute a command line that describe in argument of client command line.

Is there any method how to make server wait for command line result output, and then send all of result to client? Please let me know.

this is my example scenario :

From server side :
Code:

$ ./server 9999

From client side :
Code:

$./client 192.168.1.1 9999 cat /proc/loadavg
0.91 0.89 0.71 1/166 24375
$

other example from client side :
Code:

$./client 192.168.1.1 9999 snmpwalk -c public 192.168.1.20 1.3.6.1.2.1.2.2.1
IF-MIB::ifIndex.1 = INTEGER: 1
IF-MIB::ifIndex.2 = INTEGER: 2
IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: eth0
IF-MIB::ifType.1 = INTEGER: softwareLoopback(24)
IF-MIB::ifType.2 = INTEGER: ethernetCsmacd(6)
IF-MIB::ifMtu.1 = INTEGER: 16436
IF-MIB::ifMtu.2 = INTEGER: 1500
IF-MIB::ifSpeed.1 = Gauge32: 10000000
IF-MIB::ifSpeed.2 = Gauge32: 100000000
IF-MIB::ifPhysAddress.1 = STRING:
................ many result ..............
IF-MIB::ifOutQLen.1 = Gauge32: 0
IF-MIB::ifOutQLen.2 = Gauge32: 0
IF-MIB::ifSpecific.1 = OID: SNMPv2-SMI::zeroDotZero
IF-MIB::ifSpecific.2 = OID: SNMPv2-SMI::zeroDotZero
$


another example :
Code:

$./client 192.168.1.1 9999 commanderrr
-bash: commanderrr: command not found
$

Thanks for your helps.

Best regards,
Denny Z

Wim Sturkenboom 10-25-2007 12:12 AM

If I understand you correctly, you want the server to wait till the client has processed the command and dumped its output.

If so, I do not really understand the wait part. What do you want to happen after the wait? And what happens now? And how do you want it to work when client and server run on different computers.

Usually servers run forever. A client connects to the server, the server accepts the connection, communication happens, and when the client is done, it disconnects. The server will keep on running, waiting for new connections.

Normally, you will define a commnication protocol. In your case, the client will send a command (or reply) to the server that it's done with what it's doing.

crabboy 10-25-2007 12:16 AM

If I understand you correctly, you want to:

1. enter a 'shell type' command in the client
2. client will send the request to the server via a socket
3. the server will execute the command
4. results sent back over a socket to the client
5. display the results on the client?

Is this correct?

I'm assuming this is some sort of exercise since there are tools that will do this for you already, (rsh). Assuming that you have the sockets down, it sounds like you are seeking the popen command to execute and save the resutls, when then can be sent back to the client.

If this will be used for anything other than an exercise or perhaps a school project (yes it reeks), please think of the security implications here. It really exposes your system.

crabboy 10-25-2007 12:19 AM

Quote:

If I understand you correctly
Thats funny, I guess we understood him a bit differently, eh?

Wim Sturkenboom 10-25-2007 12:41 AM

To elaborate a bit more on the communication protocol. Usually clients instruct servers to do thing and the servers reply.
e.g below is how an ftp server and ftp client communicate (not 100% accurate)

Code:

server waits for connection fro client
client connects and waits for welcome message
server sends welcome message and waits for command
client receives reply
client sends command and waits for reply (e.g ls)
server receives and processes command (e.g. creates a list of files)
server sends reply (e.g list of files) and waits for new command
client receives reply
client sends command and waits for reply (e.g del abc.txt)
server receives and processes command (deletes file abc.txt)
server sends reply (e.g delete pass or fail) and waits for new command
etc
etc
etc
client sends command (e.g. bye), terminates connection and ends
server receives and processes command
server sees that connection is terminated and cleans up it's connection

In your case the client does the work. We can work out a similar protocol based on linux command
Code:

server waits for connection from client
server sends command 'ls' and waits for reply
client receives command and processes it (dump directory listing on screen)
client sends reply that it's done (e.g. 'OK' or 'FAIL')
    if FAIL, client terminates connection
    if OK, client waits for next command
server receives reply
    if reply is FAIL, does something
    if reply is OK, server sends 'cat abc.txt' and waits for reply
    client receives command and processes it (dump file content)
    client sends reply that it's done (e.g. 'OK' or 'FAIL')
        if FAIL, client terminates connection
        if OK, client waits for next command
etc
etc
etc
server sends command 'quit'
client receives command and terminates connection

Hope that this helps you on the way.

Wim Sturkenboom 10-25-2007 12:44 AM

Quote:

Originally Posted by crabboy (Post 2935918)
Thats funny, I guess we understood him a bit differently, eh?

:D grijns :D

penyu 10-25-2007 02:25 AM

Quote:

Originally Posted by crabboy (Post 2935917)
If I understand you correctly, you want to:

1. enter a 'shell type' command in the client
2. client will send the request to the server via a socket
3. the server will execute the command
4. results sent back over a socket to the client
5. display the results on the client?

Is this correct?

Yes, that is what I mean.

Quote:

Originally Posted by crabboy (Post 2935917)
I'm assuming this is some sort of exercise since there are tools that will do this for you already, (rsh). Assuming that you have the sockets down, it sounds like you are seeking the popen command to execute and save the resutls, when then can be sent back to the client.

Your steps explanation are match with my scenario. But I got problem: how to make server pass the execution results to the client. (Step number 4)

Quote:

Originally Posted by crabboy (Post 2935917)
If this will be used for anything other than an exercise or perhaps a school project (yes it reeks), please think of the security implications here. It really exposes your system.

Don't worry, it's just my self-learning exercise.

Thank you for your help.

regards,


All times are GMT -5. The time now is 07:49 AM.