LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Passing text to a command prompt (Python, Ubuntu) (http://www.linuxquestions.org/questions/programming-9/passing-text-to-a-command-prompt-python-ubuntu-4175441622/)

mwwynne 12-15-2012 09:48 PM

Passing text to a command prompt (Python, Ubuntu)
 
Hi,

If I run a program that requires user input from the command line, is it possible for me to give the input from a python script?

I essentially just want to automate the process of having to type in command line arguments when running another program.

eg.

If I run ./test and the program asks for a users input, like username and password, or something like that, is it possible to pass that using a python script?

I thought that if I wrote a script that would run "test" and then automatically give it my username and password, etc. when I would normally be prompted with them.

Thanks in advance!

Xeratul 12-15-2012 11:11 PM

Quote:

Originally Posted by mwwynne (Post 4850174)
Hi,

If I run a program that requires user input from the command line, is it possible for me to give the input from a python script?

I essentially just want to automate the process of having to type in command line arguments when running another program.

eg.

If I run ./test and the program asks for a users input, like username and password, or something like that, is it possible to pass that using a python script?

I thought that if I wrote a script that would run "test" and then automatically give it my username and password, etc. when I would normally be prompted with them.

Thanks in advance!


Hi,

This is a question that mostly depends on the script.

Difficult to reply since it might not be close to what you aim.

OK. examples that might be eventualhy helping.

I would go for the C language that can give you all what you desire, but you gotta learn it.


bash ~/script.sh

Code:

#/bin/bash
#script.sh
# this gives you all arguments separated by a space.
echo $@
#$1 is for first one, $2 the second one, ...

For python, it is rather similar. you can pass your arguments.


But if you pipe or redirect, I would go for C. C is everywhere, and is really worth to learn.

I would go for C which gives lot more possibilities if you are patient.
Code:

  #include <stdio.h>
  main()
  {
        int c;
        c = getchar();
        while (c != EOF) {
            putchar(c);
            c = getchar();
        }
  }


This certainly does not reply your question, but it might give you some ideas.

theNbomr 12-16-2012 09:51 AM

I think you are confused about the difference between commandline arguments and standard input. These are different and distinct concepts. Commandline arguments are the parameters that are passed to the program at launch time. Most programming languages have the ability to launch other programs, and in so doing, pass commandline arguments to those programs. Once the program is running, it may expect input on its standard input, a standard stream that Linux programs use to read input. Depending on the programming language it may also be possible to launch a program with the child program's standard input piped to some output stream of the parent process. In such a case, it would be possible for the parent to feed input data to the child process. I'm not a Python expert, but I feel confident that both of these scenarios can be accommodated in Python.
Having said all of this, it is quite common for applications that read passwords to not do so from their standard input streams, for the purpose of defeating what you're trying to do. Those programs tend to open independent connections to the controlling terminal (tty), and read that stream independently. This makes it difficult or impossible for the parent process to influence the data being read as password input, and also allows the program to get the input without echoing it, so it never shows up on the controlling terminal.
The use of pipes to send data to child processes is not uncommon, and extremely useful. Even though it may not serve your immediate need for password entry, it is a concept well worth learning.

--- rod.

TobiSGD 12-16-2012 10:35 AM

If it is user input (not commandline arguments, not stdin) that you want to script then Expect is the way to go, it is specifically designed for that purpose: http://www.nist.gov/el/msid/expect.cfm


All times are GMT -5. The time now is 01:17 PM.