0

I'm writing a script to auto-update all my SSH auto-login keys locally. The following script is run if one of my computers doesn't have the initial SSH files yet (and I'm also thinking of sharing it on GitHub after some testing);

ssh-keygen -t rsa

.. and so I get prompts;

  • Enter file in which to save the key (/Users/allendar/.ssh/id_rsa):
  • Enter passphrase (empty for no passphrase):
  • Enter same passphrase again:

The same goes for dynamically adding all the passkeys in the ssh-copy-id command.

I'm mainly only using os libraries now, but I read online I should use something like subprocess Popen.

Is there a way I can catch each prompt I get from that subprocess and maybe match their questions with Regex or literal string matching, and based on that I could send the process a response.

So if I would open a subprocess ssh-keygen -t rsa and it asks Enter same passphrase again: I could maybe catch (read) what it asks and send back a response ComplexPassword.

4

1 回答 1

1

改为使用pexpect

import pexpect

kg = pexpect.spawn('ssh-keygen -t rsa')
kg.expect('Enter same passphrase again:')
于 2013-08-04T19:03:01.160 回答