0

I am working on an automating an installer script where installer is written as shell script and will be in interactive manner. Code is as shown below:

print "Installing the build using command: \n" + str(cmd)
proc = Popen(cmd,
             stdin=PIPE,
             stdout=PIPE,
             stderr=PIPE,
             shell=True
             )
proc.stdin.flush()
proc.stdin.write("y\n")
proc.stdin.flush()
proc.stdin.write("y\n")
proc.stdin.flush()
proc.stdin.write("root\n")
print "Aftre user name"
proc.stdin.flush()
proc.stdin.write("password\n")
proc.stdin.flush()

(stdout, stderr) = proc.communicate("\n")
print "Build %s is installed successfully" % build
proc.stdin.close()
print stderr
print stdout

While command is executing in manual we will see the output something like:

press y to continue or c to cancel:
.
.
press y to continue or c to cancel:
.
.
Enter Mysql user name: 
Enter password:

But last stdin(used in code for entering password) is not working. The reason may be Enter password command is not written in installer scrip. It is output of mysql -p command.

Due to some reasons we are not storing mysql password. So password is not sent as parameter in the command directly. and Most importantly i can't use 3rd party library.

while working on this i came up with below doubts:

  1. Is there any chance to execute a shell command with shell=False(I am getting File not found exception), Does it really help me in this situattion?
  2. Is there any way to enter password as separate process which is not shell but how can i start a process with out command?
  3. why Enter password is not treated as shell command or Is there anything else i am missing?

Please suggests if any one worked in python and installer scripts.

user2695817
  • 121
  • 1
  • 7
  • You can't execute a shell command with shell=False. But you can execute a program that way. For example, Windows `start` or a Linux command line using a `|` pipe needs the shell. – abarnert Oct 21 '13 at 18:27
  • 2
    Meanwhile, often programs that want your username and password use special APIs like libpam instead of just reading from stdlib. Some *nix programs will have an alternate less-secure way to send the password (e.g., by passing a flag), but not all. L – abarnert Oct 21 '13 at 18:32
  • @BiRico: it's true that a list is better than a string if you use shell=False, and using shell=False when you don't need the shell is better, but most of your explanation is wrong. – abarnert Oct 21 '13 at 19:50
  • 1
    Passwords are usually not read from STDIN, they use terminal APIs to deter automated brute-force hacking attempts. – cdarke Oct 21 '13 at 20:29
  • @abarnert: thanks that great explanation. my bad, i gave everything as shell command. how to execute a shell script by giving shell=False and if i give like that, will i be able to enter password by using subprocess ? – user2695817 Oct 22 '13 at 04:37
  • @user2695817: On many POSIX systems, you can execute a shell script with `shell=False` if it's `+x` and has a proper `#!` line. But I don't think that's what you're asking, is it? What do you actually want to do? Meanwhile, no matter _how_ you ultimately execute the program, if it's not reading the password from stdin, you can't give it the password through `subprocess`. Some tools have an a way to specify a password less securely—or they offer a different alternative that doesn't require passwords (e.g., `ssh` can use a key file). But a tool that doesn't offer any alternative can't be driven. – abarnert Oct 22 '13 at 17:35
  • @user2695817: In the case of `mysql`, passing a `-p` argument allows you to send the password on the command line instead of via the TTY. – abarnert Oct 22 '13 at 17:37
  • thanks. Installer is written in such a way that it should not store mysql password. So it has to be given only in run time. – user2695817 Oct 23 '13 at 05:05
  • any other way than stdin like writing in to temporary file ?? – user2695817 Oct 23 '13 at 05:07
  • @user2695817: It doesn't matter where `mysql` password comes from as long as it is available at the time you call `Popen()`. For a dialog-based interaction, you could use [`pexpect` or `pty`](http://www.noah.org/wiki/Pexpect#Q:_Why_not_just_use_a_pipe_.28popen.28.29.29.3F) modules, [example](http://stackoverflow.com/a/12471855/4279) – jfs Oct 24 '13 at 08:14

0 Answers0