Passing text to a command prompt (Python, Ubuntu)
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.
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!
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.
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.
This certainly does not reply your question, but it might give you some ideas.
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.
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 05:17 AM.|