惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 叶小钗
AWS News Blog
AWS News Blog
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
V
V2EX
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Latest news
Latest news
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
T
Threatpost
L
Lohrmann on Cybersecurity
小众软件
小众软件
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
Security Latest
Security Latest
Recent Announcements
Recent Announcements
C
Check Point Blog
B
Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
I
InfoQ

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
Python与系统日常管理
2015-10-17 · via 夜行人

大多时间都在linux下工作,虽说不算系统管理员,也免不了要处理许多系统相关的东西。对linux还算熟悉,偶尔也在公司客串系统管理员的角色。于是就免不了要写许多自动化脚本的东西,来方便系统日常管理了。

bash脚本挺让人恶心的,属于dirty and quick一类。而Python语法清新可人,Python与系统的交互也极其便利,写自动化脚本再合适不过。

在此就分享些自己在写自动化脚本时中意的Python工具:


build-in

  • glob:文件名的匹配,比如
    • glob.glob(‘somedir/*.py’)
  • pathlib python3支持,路径处理的瑞士军刀
  • subprocess
    • 启动一个子进程建议的方式是使用下面的便捷函数:subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
      • subprocess.call([“ls”, “-l”])
    • 对于更高级的使用场景当它们不能满足你的需求时,可以使用底层的Popen接口。
    • subprocess.call([“sed”, “-i”, “-e”, ’s/hello/helloworld/g’, “www.txt”])
    • 单行:subprocess.call([“sed -i -e ’s/hello/helloworld/g’ www.txt”], shell=True)

sh

sh is a full-fledged subprocess replacement for Python 2.6 - 3.4 that allows you to call any program as if it were a function

使用sh可以用 Python 函数的语法去调用 shell 命令,比subprocess舒服多了

有了sh,就可以方便地用python写逻辑,而与系统的交互依然使用shell指令

sh并没有自己去实现一遍系统指令(也不现实),sh的原理很有趣,是通过ModuleType来实现的。有兴趣的同学可以参考源码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from sh import tail

def process_log_line(line):
    if "ERROR" in line:
        send_an_email_to_support(line)

process = tail("-f", "info.log", _out=process_log_line)

# ... do other stuff here ...

process.wait()

path.py

简化文件系统相关操作。与文件系统的相关操作,在python中有些分散,path.py把它们聚合在一个类文件对象里

path.py implements a path objects as first-class entities, allowing common operations on files to be invoked on those path objects directly

1
2
3
4
from path import Path
d = Path('/home/guido/bin')
for f in d.files('*.py'):
    f.chmod(0755)

###Watchdog 监视文件系统改动.事件驱动真是适合用来写监控逻辑啊

Python library and shell utilities to monitor filesystem events

可以在python中使用,也提供shell指令

###pythonpy

the swiss army knife of the command line

让python更方便与shell配合使用,行为更像过滤器

1
ls | py -x '"mv `%s` `%s.txt`" % (x,x)' | sh

###psutil

A cross-platform process and system utilities module for Python

方便用来查看系统信息(CPU, memory, disks, network)

###Fabric

Simple, Pythonic remote execution and deployment.

自动化运维工具,也可用于自动化本地任务,本地机器无非也只是一个普通的host

###ansible 大大有名的自动化运维工具,热度已远超Fabric,当部署环境巨复杂是,ansible playbook就是救星了。当然学习成本也是在不低,edX就是用它来自动化部署的,有700+ task好像


#用户身份与进程权限

顺便提一下用户身份与进程权限,python脚本中与系统交互(操作文件等),必然涉及到权限问题,所以我们需要了解linux的权限机制

linux中,子进程将继承了父进程的所有权限

当Linux加载内核文件以后,就开始运行第一个程序 /sbin/init,它的作用是初始化系统环境,init是第一个运行的程序,它的进程编号(pid)为1。其他所有进程都从它衍生,都是它的子进程。init进程在运行各种开机启动的程序之后,会执行一个login的子进程。我们将用户名和密码传递给login子进程。login在查询了/etc/passwd和/etc/shadow,并确定了其合法性之后,运行(利用exec)一个shell进程,shell进程有效身份被设置成为该用户的身份。

终于轮到我们的python进程登场啦,python进程从shell中启动,python权限为执行它的用户的权限,此后python中调用的shell指令则为python的子进程,权限与python相同

文章作者 种瓜

上次更新 2015-10-17