linux显示python代码行数据,使用 Python 获取 Linux 系统信息的代码

#!/usr/bin/env python

"""

Utility to play around with users and passwords on a Linux system

"""

from __future__ import print_function

import pwd

import argparse

import os

def read_login_defs():

uid_min = None

uid_max = None

if os.path.exists('/etc/login.defs'):

with open('/etc/login.defs') as f:

login_data = f.readlines()

for line in login_data:

if line.startswith('UID_MIN'):

uid_min = int(line.split()[1].strip())

if line.startswith('UID_MAX'):

uid_max = int(line.split()[1].strip())

return uid_min, uid_max

# Get the users from /etc/passwd

def getusers(no_system=False):

uid_min, uid_max = read_login_defs()

if uid_min is None:

uid_min = 1000

if uid_max is None:

uid_max = 60000

users = pwd.getpwall()

for user in users:

if no_system:

if user.pw_uid >= uid_min and user.pw_uid <= uid_max:

print('{0}:{1}'.format(user.pw_name, user.pw_shell))

else:

print('{0}:{1}'.format(user.pw_name, user.pw_shell))

if __name__=='__main__':

parser = argparse.ArgumentParser(description='User/Password Utility')

parser.add_argument('--no-system', action='store_true',dest='no_system',

default = False, help='Specify to omit system users')

args = parser.parse_args()

getusers(args.no_system)

使用--help选项执行上面的程序,你会看到友好的帮助信息:可选项以及它们的作用。

$ ./getusers.py --help

usage: getusers.py [-h] [--no-system]

User/Password Utility

optional arguments:

-h, --help show this help message and exit

--no-system Specify to omit system users

上面程序使用的一个例子,如下所示:

$ ./getusers.py --no-system

gene:/bin/bash

当你传入一个非法的参数,这个程序就会发牢骚(报错)

$ ./getusers.py --param

usage: getusers.py [-h] [--no-system]

getusers.py: error: unrecognized arguments: --param

在上面的程序中,我们简单的理解了如何使用argparse模块。parser = argparse.ArgumentParser(description="User/Password Utility")语句创建了一个带说明程序是做什么的可选描述的ArgumentParser对象,

然后,我们添加参数。我们想要程序能够识别接下来这条语句 add_argument()。parser.add_argument('--no-system', action='store_true', dest='no_system', default = False, help='Specify to omit system users')。第一个方法的参数是当系统调用这个程序,程序使用着将要提供这个参数的名称,接下来的参数acton=store_true表明它是一个布尔选择。那就是说,它真或假影响程序的某些行为。dest为可定制化参数,它的值可以提供给程序使用。假如这个值用户不提供,这个值默认false。最后的参数程序显示的帮助信息。最后,参数被解析通过args=parser.parse_args()方法。一旦解析方法被做,用户选项的值能够被抓取到通过相应的语法参数option_dest,当你配置参数的时候,option_dest是一个你指定的目的变量。getusers(args.no_system)这条语句使用用户提供参数的值将会回调getusers()方法。

下面的程序展示了如何指定非布尔类型的选项。该程序是对第6个程序的重写,附加了一个选项用于指定你感兴趣的网络设备。

#!/usr/bin/env python

from __future__ import print_function

from collections import namedtuple

import argparse

def netdevs(iface=None):

''' RX and TX bytes for each of the network devices '''

with open('/proc/net/dev') as f:

net_dump = f.readlines()

device_data={}

data = namedtuple('data',['rx','tx'])

for line in net_dump[2:]:

line = line.split(':')

if not iface:

if line[0].strip() != 'lo':

device_data[line[0].strip()] = data(float(line[1].split()[0])/(1024.0*1024.0),

float(line[1].split()[8])/(1024.0*1024.0))

else:

if line[0].strip() == iface:

device_data[line[0].strip()] = data(float(line[1].split()[0])/(1024.0*1024.0),

float(line[1].split()[8])/(1024.0*1024.0))

return device_data

if __name__=='__main__':

parser = argparse.ArgumentParser(description='Network Interface Usage Monitor')

parser.add_argument('-i','--interface', dest='iface',

help='Network interface')

args = parser.parse_args()

netdevs = netdevs(iface = args.iface)

for dev in netdevs.keys():

print('{0}: {1} MiB {2} MiB'.format(dev, netdevs[dev].rx, netdevs[dev].tx))

当你不带任何参数执行程序的时候,程序的行为与之前的版本完全一致。然后,你也可以指定感兴趣的网络设备。例如:

$ ./net_devs_2.py

em1: 0.0 MiB 0.0 MiB

wlan0: 146.099492073 MiB 12.9737148285 MiB

virbr1: 0.0 MiB 0.0 MiB

virbr1-nic: 0.0 MiB 0.0 MiB

$ ./net_devs_2.py --help

usage: net_devs_2.py [-h] [-i IFACE]

Network Interface Usage Monitor

optional arguments:

-h, --help show this help message and exit

-i IFACE, --interface IFACE

Network interface

$ ./net_devs_2.py -i wlan0

wlan0: 146.100307465 MiB 12.9777050018 MiB

脚本的系统范围可用性在本文的帮助下,你可能已经可以写一个或多个有用的脚本,就像其它linux命令一样,你想要每天都使用它们。最简单的方式是将脚本设置为可执行的,然后为脚本设置一个BASH别名。你也可以移除.py扩展名,然后将脚本放在诸如/usr/local/sbin这样的标准位置。

其它有用的标准库模组除了本文中已经提到的标准库模组,还有很多其它有用的标准模组:subprocess、ConfigParser、readline和curses。

接下来做什么?

在这个阶段,依靠你自己使用Python的经验,探索Linux内部,你可以参考下面的任一方式。如果你曾经需要写很多shell脚本/命令流水线来探索Linux内部,那么试一下Python。如果你想要一个更简单的方式来编写执行很多任务的实用程序脚本,那么试一下Python。最后,如果你已经使用Python在Linux上别写其它目的的程序,那么试一下用Python探索Linux内部。

php中文网:公益在线php培训,帮助PHP学习者快速成长!

Copyright 2014-2021 https://www.php.cn/ All Rights Reserved | 苏ICP备2020058653号-1e6cebb680dfe320dad7e62bd6442c3a6.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值