LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-24-2007, 05:37 AM   #1
penyu
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
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
 
Old 10-25-2007, 12:12 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by Wim Sturkenboom; 10-25-2007 at 12:15 AM.
 
Old 10-25-2007, 12:16 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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.
 
Old 10-25-2007, 12:19 AM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Quote:
If I understand you correctly
Thats funny, I guess we understood him a bit differently, eh?
 
Old 10-25-2007, 12:41 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by Wim Sturkenboom; 10-26-2007 at 12:03 AM. Reason: added missing code tag
 
Old 10-25-2007, 12:44 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by crabboy View Post
Thats funny, I guess we understood him a bit differently, eh?
grijns
 
Old 10-25-2007, 02:25 AM   #7
penyu
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
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 View Post
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 View Post
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,

Last edited by penyu; 10-25-2007 at 02:34 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute shell through C program pawarsac Linux - Software 2 07-22-2006 05:23 AM
Client / Server program cannot establish socket connection tliggins Linux - Networking 2 06-19-2006 11:51 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM
creating server socket program to handle multiple client at same time cranium2004 Programming 2 03-14-2005 11:58 AM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 11:11 PM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration