473,855 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to remotely execute a system command on a remote shell

6 New Member
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(c har *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.
Dec 21 '06 #1
6 16347
sashi
1,754 Recognized Expert Top Contributor
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(c har *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.
Dec 21 '06 #2
pvadhi07
6 New Member
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(c har *servermcnm)
{
char command1[100] = "ssh -X ";
strcat(command1 ,servermcnm);
strcat(command1 ," &");
char command2[100] = "cd ~/P1/P1*";
system(command1 );
system(command2 );
system("./server &");
}
Dec 21 '06 #3
michaelb
534 Recognized Expert Contributor
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)
Expand|Select|Wrap|Line Numbers
  1. system ("cd /home/users/me/subdir/");
  2. system("touch ABCDEF");
  3.  
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?
Dec 21 '06 #4
pvadhi07
6 New Member
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.
Dec 22 '06 #5
pvadhi07
6 New Member
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.
Dec 22 '06 #6
austin1
1 New Member
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(c har *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
Mar 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2138
by: Tom | last post by:
Is it possible to create a button that will gzip a file from a web page ?? The have the page display another button, when the gzip is complete ?? The button should allow us to issues a system command.. ie: power off ! can this be done ? Cheers
4
404
by: NewYorker | last post by:
Hi, I'm looking for code to do the following. Bascially, "System" shell out and execute the command and return the stdout in result. You know a link OR know the trick, please let me know. // result contains the stdout of the command char* result=System("ls -l"); char* result=System("cat myFile.txt");
3
2018
by: Mark Bergman | last post by:
We have inherited a script which runs rsh (remote shell), taking parameters from a variable, and if attempting to run a multi-word command remotely, we get an error! See the example below which shows 3 variations - the middle gives an error: ============ n2:/tmp} cat remscript echo "try rsh for uname (as part of variable)" c=" n1 -l scada 'uname'"
2
1844
by: Ken Allen | last post by:
I have a service that was developed in C# that I wish to debug. The project resides on my desktop system and I wish it to execute on a remote test system (actually on the next desk). When I confifure the project for remote debugging of the project I get the following alert: "Error while trying to run project: Unable to start debugging. Access is denied." This worked fine in the past when the test machine was Windows 2000, but now that...
8
6028
by: Stephen Long | last post by:
I'm looking for information on how i can open a remote command shell <cmd.exe> on a remote pc and perform basic DOS/Shell commands .. TIA Steve
11
2743
by: jobs239 | last post by:
Can I use this line inside C program "system(java -jar <jarfilename>)" to run a java program from C? Or do I have to use some JNI interface.?
3
2647
by: pavi | last post by:
Hi, I am using system command to execute a shell script in a C program. Usage system("sh file"). There are situations where the file may be empty. But execution gets hung if the file is empty. What might be the problem. Regards, Praveen Kumar A.S
9
36016
by: skyy | last post by:
Hi i am trying to use the system() command to run some linux command using perl script... Eg.. Deleting files. system("rm $filename"); However, when my $filename contain " character, the sommand wont work. Any idea how to go around this?
1
1705
by: smy | last post by:
hi, I have a perl-cgi script in which i have to run a exe file.For this i have used a system command.but it's not working.. The same program with the same parameters is running from command line as well as through a perl script. plz help..
0
9903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10767
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7927
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7084
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4567
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4168
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.