Call sudo in Python and autofill password

How to complete with Python without tampering with system settings. Python3.5(3.4)~

To enter the password from other than the terminal with sudo, you need to pass it from the standard input with the -S option. A newline character is also required.

Pass "password + newline character" as a byte string to the argument input of subprocess.run () (subprocess.check_output for Python3.4) (argument input was added from Python3.4).

An example of mounting multiple ISO files in sequence and unmounting them.

import os
import subprocess
import getpass
import tempfile


#Creating a temporary mount point
mp = tempfile.mkdtemp()

#Enter password
passwd = (getpass.getpass() + '\n').encode()

while True:

    path = input('Input file path (".quit" to quit) : ')

    if path == '.quit':
        os.rmdir(mp)
        raise SystemExit

    subprocess.run(('sudo', '-S',
                    'mount', '-t', 'iso9660', '-o', 'loop', path, mp),
                   input=passwd, check=True)

    """Gonyo Gonyo"""

    subprocess.run(('sudo', '-S', 'umount', mp), input=passwd, check=True)

http://docs.python.jp/3/library/subprocess.html

Recommended Posts

Call sudo in Python and autofill password
Stack and Queue in Python
Easy password box in Python
Unittest and CI in Python
Dynamically call methods in Python
Install CaboCha in Ubuntu environment and call it with Python.
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Password generation in texto with python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find and check inverse matrix in Python
Linear Independence and Basis: Linear Algebra in Python <6>
Pandas
Reverse Hiragana and Katakana in Python2.7
Differences in multithreading between Python and Jython
Call APIGateWay with APIKey in python requests
I created a password tool in Python.
Module import and exception handling in python
How to use is and == in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Organize python modules and packages in a mess
Geocoding in python
SendKeys in Python