Thread: ssh echo

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    16

    ssh echo

    hi,
    Can I use a popen to ssh a command to another box and then use a Unix echo command to print output to my present console..?

    like this
    Code:
    char exe[]="\/usr\/bin\/ssh -v HOSTNAME \";perl -e 'print \" Hello world \"' ";
    Thnk u,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sure, just send "ssh machine echo \"Hello, World\"" to the machine.

    You don't need to do \/ - only \ needs an extra \.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    I tried the simple echo command first but its not working...some how the echo doesnt diosplay on present console.

    Code:
    char exe[]="\/usr\/bin\/ssh -v HOSTNAME  \" ;echo \" Hello world \"  \" ";
    Doesnt work even now....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Describe what happens...

    And you still have unnecessary backslashes in your string - it probably doesn't matter, but it tells me you didn't read my post very well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    Hi like I was saying I tried without the qoutes too....
    Actually this is what I am trying.....
    Code:
    char exe[]="\/usr\/bin\/ssh -v HOSTNAME \";echo printing file ; ls -ltr  ";
    As I showed above....I get the list of files from the other box....
    But I dont get the echo output.....
    any idea what I am missing..../

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure I understand what you are saying: Are you saying you get the result of ls, but not from echo, or are you saying that you get the result of ls on the local machine, and no result from ssh?

    What happens if you just do
    Code:
    ssh hostname echo Hello World
    on a command line?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    At the same time if I run this command from the command line ....
    Code:
    ssh hostname echo Hello World
    It get the reply back as 'Hello world'


    "...Are you saying you get the result of ls, but not from echo..."
    As you said....I get the result of the ls...but not from echo.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what do you get if you do:
    Code:
    char exe[]="/usr/bin/ssh HOSTNAME  echo \"Hello world \" ";
    and pass "exe" to system()?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    Code:
     char cmd[400];
    
    sprintf(cmd,"\/usr\/bin\/ssh -v HOSTNAME \";echo printing file ; ls -ltr  ";);
    char exe_str[]=system( cmd );
     if((ptr = popen(exe_str, "r")) == NULL)
    perror("Couldn't open pipe");
     if(fgets(data, 256, ptr) == NULL)
    {
     }
     pclose(alp);
    
    while (fgets(data, 100, ptr) != NULL)
     {
    printf("%s ",line);
    }
    I get the echo output now.
    But I have a new problem....
    I get the system( cmd) ; output on the console even before the printf command in the fgets statement....

    (please let me know if I was clear)

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by vinuk23 View Post
    I get the system( cmd) ; output on the console even before the printf command in the fgets statement....

    (please let me know if I was clear)
    Not really.

    And why do you still continue to use the ptr is it is NULL. Shouldn't you exit your function?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Really, you do have to CHOOSE whether you want to use popen or system - not both. Also, you should get a warning for "converting integer to string" for the system line in this snippet
    Code:
    char exe_str[]=system( cmd );
     if((ptr = popen(exe_str, "r")) == NULL)
    And since exe_str is very unlikely to point to a valid string and the commend executes "OK" [since it is zero -> NULL, it will probably be a NULL pointer, but whatever system returns, it is VERY unlikely to be a valid string pointer in your application]. So popen() either fails or crashes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ssh daemon question
    By Overworked_PhD in forum Linux Programming
    Replies: 4
    Last Post: 07-07-2009, 11:44 AM
  2. SSH tunnel
    By kastrup_carioca in forum C Programming
    Replies: 10
    Last Post: 01-18-2006, 04:29 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. A breakthrough
    By loopy in forum Linux Programming
    Replies: 4
    Last Post: 11-26-2003, 06:46 PM
  5. C-Shell Scripting.. Help!
    By denizengt in forum Tech Board
    Replies: 3
    Last Post: 10-29-2003, 01:37 PM