 | 
December 21st, 2006, 01:19 AM
| | Newbie | | Join Date: Nov 2006
Posts: 6
| | How to remotely execute a system command on a remote shell
Hi
I am trying to execute a system command in my "C" code running on a Ubuntu Linux machine. The code is supposed to open a secure shell connection to another machine, change directories to a given directory and run an executable on that machine. I have configured the remote machine through ssh keygen to be able to authenticate my account and bypass the password. here is my "C" code:
void create_server(char *servermcnm)
{
char command1[100] = "ssh -X ";
strcat(command1,servermcnm);
strcat(command1," &");
char command2[100] = "cd ~/P1/P1*";
system(command1);
system(command2);
system("./server &");
}
In the above code, servermcnm is the name of the remote machine. The system(command1) call works, because I am able to reach a terminal session on the other machine, but I want the system(command2) line and system("./server &) to execute on the remote machine, but it doesn't work.
| 
December 21st, 2006, 07:09 AM
|  | Expert | | Join Date: Jun 2006 Location: Seremban, Malaysia Age: 32
Posts: 1,624
| | Quote: |
Originally Posted by pvadhi07 Hi
I am trying to execute a system command in my "C" code running on a Ubuntu Linux machine. The code is supposed to open a secure shell connection to another machine, change directories to a given directory and run an executable on that machine. I have configured the remote machine through ssh keygen to be able to authenticate my account and bypass the password. here is my "C" code:
void create_server(char *servermcnm)
{
char command1[100] = "ssh -X ";
strcat(command1,servermcnm);
strcat(command1," &");
char command2[100] = "cd ~/P1/P1*";
system(command1);
system(command2);
system("./server &");
}
In the above code, servermcnm is the name of the remote machine. The system(command1) call works, because I am able to reach a terminal session on the other machine, but I want the system(command2) line and system("./server &) to execute on the remote machine, but it doesn't work. | Hi there,
Have you assigned execute permission to the script? Please check this detail. Good luck & Take care.
| 
December 21st, 2006, 06:14 PM
| | Newbie | | Join Date: Nov 2006
Posts: 6
| |
I have assigned execute permission to the script. The problem is that because I am actually trying to run an executable with my C program embedded code, I am able to run the first command ("namely ssh -X servermcnm"), from my program, but then the subsequent commands to change directory and run the executable aren't running for some reason. The error I receive is something like "can't open a pseudo-terminal session because stdin is not blah blah blah".
Once I reach the other machine by executing the ssh command in my code, the next line in my code needs to be executed on the remote machine's shell, but for whatever reason it fails. I am not sure if this is a configuration related issue or something else. Here's the code:
void create_server(char *servermcnm)
{
char command1[100] = "ssh -X ";
strcat(command1,servermcnm);
strcat(command1," &");
char command2[100] = "cd ~/P1/P1*";
system(command1);
system(command2);
system("./server &");
}
| 
December 21st, 2006, 06:57 PM
| | Moderator | | Join Date: Nov 2006 Location: Boston, USA
Posts: 505
| |
This may not be the whole story, but I think there's at least one problem with your code:
You make one "system" call to change directory.
Then you call system again to do something else.
Are your making an assumption that you're still in that directory?
try this (pseudocode) -
system ("cd /home/users/me/subdir/");
-
system("touch ABCDEF");
-
I expect file ABCDEF will be created wherever you execute your program, not necesarily in /home/users/me/subdir/
What happens if you chain all your commands with ";" and call system just once?
| 
December 22nd, 2006, 12:29 AM
| | Newbie | | Join Date: Nov 2006
Posts: 6
| |
I tried the suggestion made on the previous reply about chaining commands together with ";" and making one system call, but I receive a run time error stating that ";" wasn't recognized.
I am not sure how to get around this problem, but I need to run my "server" program in machine b from calling it with C language code in machine A. I know I need to ssh into machine b, but I don't know how to run the server program in machine b with my C language code right after "ssh"ing into the machine b. Any suggestions about "C" code utilizing solutions would be greatly appreciated. Please help.
Last edited by pvadhi07; December 22nd, 2006 at 12:31 AM.
Reason: Clarification
| 
December 22nd, 2006, 01:06 AM
| | Newbie | | Join Date: Nov 2006
Posts: 6
| |
I was able to run the executable by calling system(command) where command was "ssh -X servername /home/dir1/dir2/executable &". Thanks for michaelb and sashi for attempting to help me.
| 
March 1st, 2007, 11:39 PM
| | Newbie | | Join Date: Mar 2007
Posts: 1
| | Quote: |
Originally Posted by pvadhi07 Hi
I am trying to execute a system command in my "C" code running on a Ubuntu Linux machine. The code is supposed to open a secure shell connection to another machine, change directories to a given directory and run an executable on that machine. I have configured the remote machine through ssh keygen to be able to authenticate my account and bypass the password. here is my "C" code:
void create_server(char *servermcnm)
{
char command1[100] = "ssh -X ";
strcat(command1,servermcnm);
strcat(command1," &");
char command2[100] = "cd ~/P1/P1*";
system(command1);
system(command2);
system("./server &");
}
In the above code, servermcnm is the name of the remote machine. The system(command1) call works, because I am able to reach a terminal session on the other machine, but I want the system(command2) line and system("./server &) to execute on the remote machine, but it doesn't work. | ------------------------------------------
Hi Pvadi,
I am trying to do the same as you are asking here. Meaning execute remote shell , I have been looking and successfully generate the public / private key using ssh-gen , but I do not know how to bypass the password , so it will not prompt me the password.
You had mentioned here that you know how to set up to make it bypass prompting the password. Would you shed some lights for me ?
Thanks a lot,
Austin1
|  | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,248 network members.
|