Redirect stdin and out to sockets


 
Thread Tools Search this Thread
Top Forums Programming Redirect stdin and out to sockets
# 1  
Old 09-02-2008
Redirect stdin and out to sockets

For windows was pretty simple to redirect the std in a and out of a
child process for "cmd.exe " command prompt terminal to a socket using connected pipes passed to a new process in the STARTUPINFO structure.

Code:
 BOOL b = ::CreatePipe((LPHANDLE)h_stdInRead,(LPHANDLE)hsdtInWriteTmp, &SecAttrib, 0);
    b &= ::CreatePipe((LPHANDLE)hsdtOutReadTmp, (LPHANDLE)h_sdtOutWrite, &SecAttrib, 0);
    b &= ::DuplicateHandle(hProcess, hsdtInWriteTmp, hProcess, h_this2Write, 0, 0, DUPLICATE_SAME_ACCESS);
    b &= ::DuplicateHandle(hProcess, hsdtOutReadTmp, hProcess, h_this2Read, 0, 0, DUPLICATE_SAME_ACCESS);

    hsdtInWriteTmp.Close();
    hsdtOutReadTmp.Close();

    si.dwFlags      = 0;
    si.cb           = sizeof(STARTUPINFO);
    si.dwFlags      = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow  = SW_HIDE;
    si.hStdInput    = h_stdInRead;
    si.hStdOutput   = h_sdtOutWrite;
    si.hStdError    = h_sdtOutWrite;
    si.wShowWindow    = SW_HIDE;
    si.lpDesktop    = 0;   
    si.lpTitle        = "blah" // Version();
   CreateProcess(NULL, "cmd.exe", lps, NULL, 1, CREATE_NEW_CONSOLE, NULL, NULL, psi, ppi) ;
/*
...
...
*/
//handles only 1 connection
 while(1)
{
  if(sock >0)
  {
  if(receive(socket, buffer))
    WriteFile(h_this2Write,..)
  if(ReadFile(h_this2Read))
     send(socket);
  }
  else
  {
     sock = wait_4_connection(); //accept and listen
  }

In above code the context (current directory) of the terminal cmd.exe is not lost between connections, and I can run internal shell commands as start, call, cd, and so on ....

For Linux I've tried to mimic the shell using popen(), and to write and read in the handler returned by popen(), but I cannot run /bash shell commands as cd, cp, mkdir and so on because they are internal bin/bash commands. I would like to have a shell up, and to write and read from it trough it's stdin stdout, available in my app (pipes of file handlers).
Something like:
Code:
//handles only 1 connection
   
  execl_and_get_sddin_and_out("bin/bash"); // keep it up
    ...   
  while(1)
  {
   if(sock >0)
  {
    if(receive(socket, buffer))
      fwrite(shell_stdin,..)
   if(fread(she__stdout))
      send(socket);
  }
  else
  {
     sock = wait_4_connection(); //accept and listen
  }
}


I tried these samples (but they don;t hold the shell up, and the command is sent to a shell, then the shell is closed):

Tying Standard Input and Output to a Socket Connection
http://src.gnu-darwin.org/DarwinSour...h/sshconnect.c

Does anyone has any idea how this can be accomplished.




Thank you.
Gyula.

Last edited by gyula; 09-02-2008 at 12:52 AM..
# 2  
Old 09-02-2008
Sorry, why are you creating a socket connection? Why don't you use pipe the output of the command back to the user?
Quote:
For Linux I've tried to mimic the shell using popen(), and to write and read in the handler returned by popen(), but I cannot run /bash shell commands as cd, cp, mkdir and so on because they are internal bin/bash commands
Hunh? If you use popen, the only limitation is that you cannot both read from and write into the file descriptor. Only if that limitation is a problem do you need sockets. But you say you can't run them because they are "internal commands". But there almost always external versions of these commands available. (For cd, however, it's useless -- you must do this within the context of your program, or prefix all subsequent commands with a "cd".) Regardless, all one needs to do in this case is run bash and provide it the command to parse.

Code:
fd=popen("/bin/bash -c \"cd /tmp; echo *\"","r");

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect string from bash stderr to user stdin

Hi there, I need to execute a command in the bash. The program prints some standard (output and) error and then wants the user to choose one of several options and type the according input. I am trying to solve this issue in a bash script but also running into some circular dependency. How can I... (7 Replies)
Discussion started by: fredestet
7 Replies

2. Shell Programming and Scripting

Redirect String to STDIN

Looking for the proper way to bring a string into the stdin. I have a string that I would like to grep and awk. Each have to be run separately, not piped together. So far, the only way I could figure out how is to echo the string and pipe it: echo 'This is my string' | grep my (3 Replies)
Discussion started by: Panman82
3 Replies

3. UNIX for Dummies Questions & Answers

Redirect stdin stdout to multiple files

Hi, i know how to a) redirect stdout and stderr to one file, b) and write to two files concurrently with same output using tee command Now, i want to do both the above together. I have a script and it should write both stdout and stderr in one file and also write the same content to... (8 Replies)
Discussion started by: ysrini
8 Replies

4. Shell Programming and Scripting

Cannot redirect to STDIN in a shell script

I am unable to use STDIn redirection with < (commands) When I do the following, both approaches work and give the same results: 1. $ printf "aaa\nbbb\n" > file1 $ printf "111\n222\n" > file2 $ cat file1 file2 aaa bbb 111 2222. $ cat <(printf "aaa\nbbb\n") <(printf "111\n222\n") aaa... (8 Replies)
Discussion started by: metaltree
8 Replies

5. UNIX for Advanced & Expert Users

inline redirect stdin

Hi: I have the next script on ksh #!/usr/bin/ksh cd $FUENTES qdesign <<-! \$/opt/cognos/ph843e/bin/qtp <<-! \$/opt/cognos/ph843e/bin/quiz <<-! ! ! ! This script is very simple, i want to nest three process quiz into qtp, and this into qdesign. When I run it , i receive the next... (2 Replies)
Discussion started by: ct2marer
2 Replies

6. Shell Programming and Scripting

inline redirect stdin

Hi: I have the next script on ksh #!/usr/bin/ksh cd $FUENTES qdesign <<-! \$/opt/cognos/ph843e/bin/qtp <<-! \$/opt/cognos/ph843e/bin/quiz <<-! ! ! ! This script is very simple, i want to nest three process quiz into qtp, and this into qdesign. When I run it , i receive the... (5 Replies)
Discussion started by: ct2marer
5 Replies

7. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

8. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

9. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. UNIX for Dummies Questions & Answers

stdin not tty when try to pine or redirect

My supervisor keep getting "stdin not tty" or something like that when he pipe or redirect input into a program. Others don't seem to get this message. Is there some way I can help him to fix or turn this off? Thx in advance (1 Reply)
Discussion started by: Micz
1 Replies
Login or Register to Ask a Question