LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-27-2002, 04:14 PM   #1
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Rep: Reputation: 30
how to put a shell call in a C/C++ program?


Hi everyone!


anybody ever tried to put a shell call from a C/C++ program?
,say, i would like to put a mkdir in it, or a pwd call and get the string back on the program...

Hano
 
Old 04-27-2002, 04:18 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you want the system() function.
 
Old 04-28-2002, 11:20 PM   #3
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
i think u can try reading up and trying some stuff from the unistd.h library

Last edited by gui10; 04-28-2002 at 11:24 PM.
 
Old 05-09-2002, 02:14 PM   #4
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
Ok, but now suppose you want to automate a connection to a host with ssh, do some stuff like read some file or exec something, i would guess that


system(" ssh login@host.org");
system(" ls -l"); // i would guess this do the ls on the remote host

but occurs that the ssh calls doesn't continue until you have ended the connection, and the ls -l command is executed after so the question is:

How can you automate a system call to be executed on the ssh client?

Hano
 
Old 05-09-2002, 04:38 PM   #5
[cacheflow]
Member
 
Registered: Jan 2002
Distribution: Debian 3.0
Posts: 49

Rep: Reputation: 15
Quote:
Originally posted by acid_kewpie
you want the system() function.
maybe but its a very bad idea to use it... its poorly optimized and slow as hell...

Code:
#include <process.h>
#include <stdio.h>

int main(void)
{
   spawnl(P_WAIT, "myprog.exe", "myprog.exe", NULL);
   return 0;
}

Last edited by [cacheflow]; 05-09-2002 at 04:48 PM.
 
Old 05-10-2002, 02:06 PM   #6
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
Yes, if you allow me the objection that process.h and spawnl() doesn't belong to the linux enviroment, but to windoze

Hano
 
Old 05-10-2002, 02:18 PM   #7
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
But more on a more concrete footing, what i'm looking for is a way to call a ssh, somehow return the port / terminal in with it has opened, and then write and read to the port / terminal from a C / C++ program, just like the opening a istream / ofstream ....

There HAVE to be an easy way to do this, Isn't linux supposed to be all about this?

Hano
 
Old 05-10-2002, 03:15 PM   #8
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
I'm checking out a little about CORBA, may be i have to go all that way down.....


Hano
 
Old 05-19-2002, 02:26 PM   #9
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
Be very careful with using ssh in that manner, your program will be prone to security holes. Eg. if a user's PATH environment variable gets changed, or LDLIBRARY_PATH, etc.

ssh allows you to run programs on the remote machine like rsh, eg. "ssh myserver.com ls -al /etc". anything after the server name would be treated as the remote command. If you have public key auth configured even better, then your program would not have to interact with the user, and you would not have to "write" to the ssh port ( I'm not sure thats possible at anyrate ).

Another, even better alternative is to use the OpenSSL library to encrypt your own connection to the remote server. That sounds like it's closer to what you want to do. OpenSSL has a set of well documented API calls to make a simple SSL or TLS connection. After this, you can treat the connection as a normal socket connection. http://openssl.org/
 
  


Reply



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
call a c program in a shell script jagman Programming 4 04-05-2005 04:58 PM
Where to put autostarting shell script? Ollir Mandriva 2 08-11-2004 02:30 PM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 07:31 PM
How to call the arp linux shell command from within a perl program Bassam Programming 1 03-11-2004 02:00 AM
call shell or other program out of my_program lea Programming 3 10-09-2002 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 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
Open Source Consulting | Domain Registration