首页 > 解决方案 > Debian 中的 Python 自动安装脚本

问题描述

我正在尝试创建一个小 python 脚本,它将在 Debian 中安装软件包。但是我搜索了一段时间,仍然找不到任何解决方案来使它在程序需要用户提供某些东西时与提示一起工作。

例如用于安装 MariaDB 的自动“yes”和“password”。

os.system - 在 MariaDB 安装完成后,停止并等待用户给出答案并继续脚本的其余部分。但在这里我希望安装自动运行。

有什么功能可以处理这个吗?

标签: pythondebian

解决方案


期待

具有交互的 fdisk 示例:

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()

推荐阅读