python - Debian 中的 Python 自动安装脚本
问题描述
我正在尝试创建一个小 python 脚本,它将在 Debian 中安装软件包。但是我搜索了一段时间,仍然找不到任何解决方案来使它在程序需要用户提供某些东西时与提示一起工作。
例如用于安装 MariaDB 的自动“yes”和“password”。
os.system - 在 MariaDB 安装完成后,停止并等待用户给出答案并继续脚本的其余部分。但在这里我希望安装自动运行。
有什么功能可以处理这个吗?
解决方案
具有交互的 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()
推荐阅读
- swift - 如何为现有的 xcode 项目创建 Package.swift
- state - 是否可以同时在一个状态图中运行多个状态?
- r - R install.packages 返回“错误:锁定目录失败”
- python - 如何在不更改熊猫中其他列的情况下将特定列更改为行?
- android - 连接服务协议时出错:没有可用的 Flutter 视图
- php - 如何将值从 html 传递到 php 中的 xpath 表达式?
- javascript - 如何创建javascript倒计时?
- c++ - 我们可以在 TypeScript 中创建 std::type_index 吗?
- c# - 在asp核心中使用筛子进行分页时如何发送单个请求而不是两个请求?
- arrays - 退出循环后我的计数器被重置