Python 基本输出

Python 基本输出

系统信息
#!/usr/bin/evn python3
#输入一个用户名判断是否存在
#编写日期:2021/3/11  
print("输入用户判断其是否存在")
n = input("请输入一个用户:")
import subprocess
ret = subprocess.getstatusoutput("id " + n)
code, result = ret
if code== 0:
    print("用户存在")
else:
    print("用户不存在")

 
#输出目前系统中都监听了哪些端口
cmd1 = 'ss -ntal'
ret1 = subprocess.getoutput(cmd1)
ports = []
for line in ret1.splitlines():
    if line.startswith('LISTEN'):
        host_port = line.split()[3]
        port = host_port.split(':')[-1]
        for i in port:
            if port in ports:
                continue
            ports.append(port)
print(ports)
 

#输入一个端口,判断服务是否开启
cmd2 = 'ss -ntal'
ret2 = subprocess.getoutput(cmd2)
ports = []
for line in ret2.splitlines():
    if line.startswith('LISTEN'):
        host_port = line.split()[3]
        port = host_port.split(':')[-1]
        for i in port:
            if port in ports:
                continue

            ports.append(port)
while True:
    n = input('请输入一个端口:')
    if not n:
        continue
    if n == 'q':
        break
    n == int(n)
    if n in ports:
        print('该服务已开启')
    else:
        print('该服务未开启')
exit('退出程序......')

#输出当前系统中所有的普通用户
ret3 = subprocess.getoutput('cat /etc/passwd')
for line in ret3.splitlines():
    user = line.split(':')[0]
    uid = line.split(':')[2]
    if int(uid) > 999:
        print(user)
                   
#获取当前系统中非回环接口的 IPv4地址
ret4 = subprocess.getoutput('ifconfig')
for line in ret4.splitlines():
    if 'inet' in line and 'broadcast' in line:
        ip = line.split()[1]
        print("当前系统中非回环接口的IPv4地址有 " + ip)


#获取当前系统中 CPU 型号, 内核数量 
cpu_name =subprocess.getoutput("grep 'name' /proc/cpuinfo | uniq -c")
cpu_number = cpu_name.split()[0]
cpu = cpu_name.split(": ")[1]
print("CPU版本是 " + cpu + 
"\n内核数量是" + cpu_number + "个")


#获取当前系统内存使用情况:总容量,可用容量
import subprocess
cmd3 = 'free -h'
ret5 = subprocess.getoutput(cmd3)
for line in ret5.splitlines():
    if 'Mem' in line:
        _,total,_,free,*_ = line.split()
        print ("系统内存总容量为 " + total + "\n系统内存剩余容量为 " + free)


#获取当前系统的磁盘情况,每块磁盘的设备名称和总容量
cmd4 = 'lsblk'
ret6 = subprocess.getoutput(cmd4)
for line in ret6.splitlines():
    if 'disk' in line:
        name,_,_,size,*_ = line.split()
        print ("磁盘" + name + "的总容量为: " + size)


#获取当前系统磁盘的有效分区情况,打印出挂载点和分区容量
cmd5 = 'lsblk'
ret7 = subprocess.getoutput(cmd5)
for line in ret7.splitlines():
    if 'part' in line and '/' in line:
        _,_,_,size,_,_,mount = line.split()
        print ("该分区容量为:" + size + "  " +"挂载点为:" +  mount)
    elif 'lvm' in line and '[SWAP]' not in line:
        _,_,_,size,_,_,mount = line.split()
        print ("该分区容量为:" + size + "  " +"挂载点为:" +  mount)



#输出当前系统的 CPU 负载情况 1 ,5 ,15 分钟
cmd6 = ('uptime')
ret8 = subprocess.getoutput(cmd6)
a = ret8.split(':')[-1]
one = a.split(',')[0]
five = a.split(',')[1]
fifteen = a.split(',')[2]
print ("当前CPU平均负载:" +
 "\n一分钟:" + one +
"\n五分钟:" + five +
"\n十五分钟:" + fifteen )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维-阿鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值