Jump to content

how to use expect in c++ #!/usr/bin/expect -f to c++ ?


datajumper
 Share

Recommended Posts

hello im kinda new to c++ i probabaly dont even have a good reason on why im wanting to do this

but here goes im trying to learn c++ and the only way i can learn is to build something im interested in

a while back i made this wonderful bash script that installs everything i need on a fresh install of ubuntu

(example #!/usr/bin/expect -f spawn sudo -s expect "password" send "pswd" expect "s" send "apt-get install "whatever ")

get the concept ? ok now i want to do the same thing "kinda" but in c or c++ i

i know a little about the system command which i have confirmed it works and works well

but i dont know how to send multiple commands to the terminal like using send totype commands and passwords like in expect

so i would appriciate any criticism and if you have any knowledge about this or simular i would be very greatful if you could shed

some light on the subject thanx

Link to comment
Share on other sites

The system() function starts a process that you won't have anything to do with at all ever again. All it returns to you is the PID of the newly created program, which means the only thing you can do at this point is send it a signal. You have *ZERO* control.

Since you want to capture the process' output and provide it with input, you need to communicate with this process you're going to start. To do this you first create 2 or 3 pipes (using the pipe() function) - one will be STDIN for the command to be invoked and the other STDOUT (the optional 3rd one for STDERR). Next, using the fork() function, you create a new process for your program. The main process will get the new process' PID as return value for fork(), the newly created process will get 0 which is how you can tell them apart within the code of your program.

On the newly created process' side, you tie the appropriate ends of the pipes to the STDIN, STDOUT and possibly STDERR file descriptors of your process using the dup2() function. Next you start the command that is supposed to be run using the execve() function, which effectively replaces your client program logic with that of the command you're running - but all your setup work with preparing STDIN/STDOUT/STDERR will remain in place.

On the main process' side, you start reading from the STDIN pipe, waiting for the data that you're expecting and when you see it you write to the STDOUT pipe the data that you were supposed to send to the process.

Here's an online example of just this.

Edited by Cooper
Link to comment
Share on other sites

There's still many ways to control processes created w/system(), but if you're going to use pipes or system() you might as well do it in a scripting language as dev time is much faster and net result is the same.

The C++ way to do this, simply said - is to get the apt C++ source code (apt-get source aptitude), locate the relevant functions (in this case one is download_list:: in aptitude/src/download_list.cc), then hardcode the package names you want and call from main(). It's not the only function you need but you can easily find the others in the source.

Link to comment
Share on other sites

The system() function starts a process that you won't have anything to do with at all ever again. All it returns to you is the PID of the newly created program, which means the only thing you can do at this point is send it a signal. You have *ZERO* control.

Since you want to capture the process' output and provide it with input, you need to communicate with this process you're going to start. To do this you first create 2 or 3 pipes (using the pipe() function) - one will be STDIN for the command to be invoked and the other STDOUT (the optional 3rd one for STDERR). Next, using the fork() function, you create a new process for your program. The main process will get the new process' PID as return value for fork(), the newly created process will get 0 which is how you can tell them apart within the code of your program.

On the newly created process' side, you tie the appropriate ends of the pipes to the STDIN, STDOUT and possibly STDERR file descriptors of your process using the dup2() function. Next you start the command that is supposed to be run using the execve() function, which effectively replaces your client program logic with that of the command you're running - but all your setup work with preparing STDIN/STDOUT/STDERR will remain in place.

On the main process' side, you start reading from the STDIN pipe, waiting for the data that you're expecting and when you see it you write to the STDOUT pipe the data that you were supposed to send to the process.

Here's an online example of just this.

thank you so much ill give that a try :: thanx for all your help maybe im on the right path now lol

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...