Izaya
Izaya

Reputation: 23

Python autoinstall scripts in debian

I'm trying to create a little python script which will be installing packages in Debian. But I searched some time and still can't find any solution to make it work with prompts when the program is needing something from user.

For example auto "yes" and "password" for installing MariaDB.

os.system - stops and wait for the user to give answers and continue the rest of the script, after MariaDB installation is done. But here I want the installation to run automatically.

Is there any function which can handle this?

Upvotes: 1

Views: 66

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31236

pexpect

Example for fdisk that has interaction:

def expectpart():
    TMPLOG = "/tmp/pexpect.log"
    cmd = f'''
sudo fdisk /dev/sdb ;\
echo "alldone" ;
'''
    with open(TMPLOG, "w") as log:
        ch = pexpect.spawn(f"/bin/bash -c \"{cmd}\"", encoding='utf-8', logfile=log)
        ch.expect("Command")
        ch.send("c\r")
        ch.expect("DOS Compat")
        ch.send("n\r")
        ch.expect("Partition type")
        ch.send("p\r")
        ch.expect("Partition number")
        ch.send("1\r")
        ch.expect("First sector")
        ch.send("\r")
        ch.expect("Last sector")
        ch.send("\r")
        ch.expect("Created a new partition")
        ch.send("w\r")
        ch.expect("alldone")
        i = ch.expect([pexpect.EOF], timeout=5)
        ch.close()

Upvotes: 1

Related Questions