How to perform a root command with Pexpect?
<p>I'm working on a python program to assist with the apt-get tool. I want to use pexpect to download the chosen package. I believe I'm getting stuck at the child.expect line. It seems to timeout when it comes to that line.</p>

<pre><code>butt = "vlc"
child = pexpect.spawn('sudo apt-get install ' + butt)
child.logfile = sys.stdout
child.expect('[sudo] password for user1: ')
child.sendline('mypassword')
</code></pre>

<p>This is the log file.</p>

<pre class="lang-none prettyprint-override"><code>TIMEOUT: Timeout exceeded.
&lt;pexpect.spawn object at 0xb5ec558c&gt;
version: 3.2
command: /usr/bin/sudo
args: ['/usr/bin/sudo', 'apt-get', 'install', 'vlc']
searcher: &lt;pexpect.searcher_re object at 0xb565710c&gt;
buffer (last 100 chars): '[sudo] password for user1: '
before (last 100 chars): '[sudo] password for user1: '
after: &lt;class 'pexpect.TIMEOUT'&gt;
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 27641
child_fd: 4
closed: False
timeout: 30
delimiter: &lt;class 'pexpect.EOF'&gt;
logfile: &lt;open file '&lt;stdout&gt;', mode 'w' at 0xb74d8078&gt;
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
</code></pre>

<p>UPDATE:</p>

<p>The password gets sent just fine. It also expects the next line as well, but then enters "Y" and does nothing.</p>

<pre><code>child = pexpect.spawn('sudo apt-get install ' + butt)
child.logfile = sys.stdout
child.expect_exact('[sudo] password for user1: ')
child.sendline('mypass')
child.expect_exact('Do you want to continue? [Y/n] ')
child.sendline('Y')
</code></pre>

<p>SOLVED: </p>

<p>I needed to add this line at the end.</p>

<pre><code>child.expect(pexpect.EOF, timeout=None)
</code></pre>

<p>Try <code>child.expect_exact()</code>.</p>

<p>From the docs:</p>

<blockquote>
  <p>The expect() method waits for the child application to return a given string. The string you specify is a regular expression, so you can match complicated patterns.</p>
</blockquote>

<p>It is good practice to use <code>expect()</code> only when the intent is to match a regular expression.</p>

python linux ubuntu pexpect