LinuxQuestions.org
Visit Jeremy's Blog.
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 12-15-2012, 09:48 PM   #1
mwwynne
LQ Newbie
 
Registered: Aug 2012
Distribution: Ubuntu 12.04
Posts: 20

Rep: Reputation: Disabled
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!
 
Old 12-15-2012, 11:11 PM   #2
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,337

Rep: Reputation: 83
Quote:
Originally Posted by mwwynne View Post
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.

Last edited by Xeratul; 12-15-2012 at 11:14 PM.
 
Old 12-16-2012, 09:51 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 12-16-2012 at 01:00 PM.
 
Old 12-16-2012, 10:35 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046
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
 
  


Reply

Tags
linux, python, ubuntu


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Automatically passing an answer to a command's prompt bret Linux - General 3 12-16-2011 09:01 AM
passing parameters from a unix shell prompt into a windows cmd.exe prompt nano2 Programming 1 09-01-2009 11:26 AM
how to configure keyboard in python command prompt varun_shrivastava Linux - General 2 05-31-2007 11:38 PM
Passing a text file to the command line as arguments wimnat Linux - General 2 12-05-2005 08:09 AM
How to run a text file through a command prompt? Fillys6 Linux - Newbie 8 03-24-2005 02:21 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration