| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # wrapsu.py |
|---|
| 4 | # A *very* simple wrapper for the 'su' command |
|---|
| 5 | # |
|---|
| 6 | # http://mornie.org/code/browser/misc/wrapsu.py |
|---|
| 7 | # |
|---|
| 8 | # (c) Copyright 2008 Daniele Tricoli aka Eriol <eriol@mornie.org> |
|---|
| 9 | # This program is free software: you can redistribute it and/or modify |
|---|
| 10 | # it under the terms of the GNU General Public License as published by |
|---|
| 11 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 12 | # (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | # GNU General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 21 | ## |
|---|
| 22 | # |
|---|
| 23 | # This simple script was born at the end of november 2008 from a discussion |
|---|
| 24 | # with Luca Canali and Davide Prina |
|---|
| 25 | |
|---|
| 26 | import getpass |
|---|
| 27 | import pexpect |
|---|
| 28 | import os |
|---|
| 29 | import sys |
|---|
| 30 | |
|---|
| 31 | SU_PATH = os.path.join('/', 'bin', 'su') |
|---|
| 32 | |
|---|
| 33 | DEFAULT_MESSAGE = 'What is the password? Insert here: ' |
|---|
| 34 | ENV_MESSAGE = os.getenv('WRAPSU_MESSAGE') |
|---|
| 35 | |
|---|
| 36 | # If you set WRAPSU_MESSAGE in your environ it will be used instead |
|---|
| 37 | # of DEFAULT_MESSAGE |
|---|
| 38 | PASSWORD_MESSAGE = ENV_MESSAGE if ENV_MESSAGE else DEFAULT_MESSAGE |
|---|
| 39 | |
|---|
| 40 | def main(args): |
|---|
| 41 | |
|---|
| 42 | args = ' '.join(args) |
|---|
| 43 | command = SU_PATH + ' ' + args |
|---|
| 44 | password = getpass.getpass(PASSWORD_MESSAGE) |
|---|
| 45 | |
|---|
| 46 | child = pexpect.spawn(command) |
|---|
| 47 | child.expect ('Password: ') |
|---|
| 48 | child.sendline(password) |
|---|
| 49 | child.interact() |
|---|
| 50 | |
|---|
| 51 | os._exit(0) |
|---|
| 52 | |
|---|
| 53 | if __name__ == '__main__': |
|---|
| 54 | try: |
|---|
| 55 | main(sys.argv[1:]) |
|---|
| 56 | except TypeError: |
|---|
| 57 | print '' |
|---|
| 58 | os._exit(0) |
|---|
| 59 | except KeyboardInterrupt: |
|---|
| 60 | os._exit(0) |
|---|
| 61 | except Exception: |
|---|
| 62 | os._exit(1) |
|---|