python function scripting

1. Enter a username to determine if it exists

#!/usr/bin/env python3
## user
## shanhai
import subprocess
print ("请输入用户名")
x = input(">>:")
def user_input(x):
    while True:
        if not x:
            continue
        if x == 'q':
            break
        ret = subprocess.getstatusoutput("id " + x)
        code, result = ret
        if code == 0:
            return "此用户存在"
        else:
            return "此用户不存在"
            break
y=user_input(x)
print(y)

Insert picture description here

2. Output all ordinary users in the current system

#!/usr/bin/env python3
## user
## shanhai
import subprocess
def shanhai():
    cmd = 'cat /etc/passwd'
    ret = subprocess.getoutput(cmd)
    user = {
    
    }
    for line in ret.splitlines():
        user_list = line.split(':')[0:4:2]
        if int(user_list[1]) >= 1000:
            user[user_list[0]] = user_list[1]
    return user
ret = shanhai()
for k,v in ret.items():
    print("普通用户:" + k + "\t" + "UID:" + v)

Insert picture description here

3. Output which ports are currently monitored in the system

#!/usr/bin/env python3
## port listen
## shanhai
import subprocess
def shanhai():
    cmd = 'ss -ntal'
    ret = subprocess.getoutput(cmd)
    ports = []
    for line in ret.splitlines():
        if line.startswith('LISTEN'):
            host_port = line.split()[3]
            port = host_port.split(':')[-1]
            ports.append(port)
            ports_list = set(ports)
    return ports_list
ret_port = shanhai()
for i in ret_port:
    print("正在监听的端口有:",i)

Insert picture description here

4. Enter a port to determine whether the service is open

#!/usr/bin/env python3
## server status
## shanhai
import subprocess
def shanhai():
    while True:
        port = input("请输入端口号>>:")
        if not port:
            continue
        if port == 'q':
            break
        ret = subprocess.getstatusoutput("netstat -ntlp | grep " + port)
        code,result = ret
        if code == 0:
            return "服务已启动"
        else:
            return "服务没启动"
ret = shanhai()
print(ret)

Insert picture description here

5. Obtain the IPv4 address of the non-loopback interface in the current system

#!/usr/bin/env python3
## ip
## shanhai
import subprocess
def shanhai():
    ips = {
    
    }
    cmd = 'ip a'
    ret = subprocess.getoutput(cmd)
    for line in ret.splitlines():
        if 'inet ' in line and '127.0.0.1/8' not in line:
            _,ip,*_,name = line.split()
            ips[name] = ip
    return ips
ret = shanhai()
for k,v in ret.items():
    print("名称:" + k + "\t" + "ip:" + v)

Insert picture description here

6. Get the CPU model and the number of cores in the current system

#!/usr/bin/env python3
## cpu_core
## shanhai
import subprocess
def shanhai():
    cpu = "grep 'model name' /proc/cpuinfo | uniq"
    core ="grep 'core id' /proc/cpuinfo | sort -u | wc -l"
    ret_cpu = subprocess.getoutput(cpu)
    ret_core = subprocess.getoutput(core)
    cpu_core = {
    
    }
    core_num = ret_core
    for line in ret_cpu.splitlines():
        cpu_name = ret_cpu.split(": ")[1]
        cpu_core[cpu_name] = core_num
    return cpu_core
ret = shanhai()
for k,v in ret.items():
    print("CPU信息:" + k + "\t" + "内核数:" + v)

Insert picture description here

7. Get the current system memory usage: total capacity, available capacity

#!/usr/bin/env python3
## Mem
## shanhai
import subprocess
def shanhai():
    cmd = 'free -m'
    ret = subprocess.getoutput(cmd)
    mem = {
    
    }
    for line in ret.splitlines():
        if line.startswith('Mem'):
            total = line.split()[1]
            free = line.split()[3]
            mem[total] = free
    return mem
ret = shanhai()
for k,v in ret.items():
    print("总容量:" + k ,"可用容量:" + v)

Insert picture description here

8. Get the current system disk situation, the device name and total capacity of each disk

#!/usr/bin/env python3
## disk
## shanhai
def shanhai():
    import subprocess
    cmd = 'lsblk'
    disks = {
    
    }
    ret = subprocess.getoutput(cmd)
    for line in ret.splitlines():
        if 'disk' in line:
            dev_name = line.split()[0]
            dev_size = line.split()[3]
            disks[dev_name] = dev_size
    return disks
ret = shanhai()
for k,v in ret.items():
    print("磁盘名称:" + k + "\t" + "总容量:" + v)

Insert picture description here

9. Get the effective partition status of the current system disk, print out the mount point and partition capacity

#!/usr/bin/env python3 
## disk_eft
## shanhai
import subprocess
def shanhai():
    cmd = 'df -Th'
    ret = subprocess.getoutput(cmd)
    disk = []
    for i in ret.splitlines():
        disk.append(i)
    return disk
ret = shanhai()
for i in ret:
    print(i)

Insert picture description here

10. Output current system CPU load status 1, 5, 15 minutes

#!/usr/bin/env python3 
## cpu_load
## shanhai
import subprocess
def shanhai():
    cmd = 'uptime'
    ret = subprocess.getoutput(cmd)
    for line in ret.splitlines():
        cpu_load = line.split(":")[-1]
    return cpu_load
ret = shanhai()
print("当前系统CPU每1,5,15分钟负载情况分别为:",ret)

Insert picture description here

Guess you like

Origin blog.csdn.net/AnNan1997/article/details/114873570