#!/usr/bin/env python3## user## shanhaiimport subprocess
print("请输入用户名")
x =input(">>:")defuser_input(x):whileTrue:ifnot x:continueif x =='q':break
ret = subprocess.getstatusoutput("id "+ x)
code, result = ret
if code ==0:return"此用户存在"else:return"此用户不存在"break
y=user_input(x)print(y)
2. Output all ordinary users in the current system
#!/usr/bin/env python3## user## shanhaiimport subprocess
defshanhai():
cmd ='cat /etc/passwd'
ret = subprocess.getoutput(cmd)
user ={
}for line in ret.splitlines():
user_list = line.split(':')[0:4:2]ifint(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)
3. Output which ports are currently monitored in the system
#!/usr/bin/env python3## port listen## shanhaiimport subprocess
defshanhai():
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)
4. Enter a port to determine whether the service is open
#!/usr/bin/env python3## server status## shanhaiimport subprocess
defshanhai():whileTrue:
port =input("请输入端口号>>:")ifnot port:continueif port =='q':break
ret = subprocess.getstatusoutput("netstat -ntlp | grep "+ port)
code,result = ret
if code ==0:return"服务已启动"else:return"服务没启动"
ret = shanhai()print(ret)
5. Obtain the IPv4 address of the non-loopback interface in the current system
#!/usr/bin/env python3## ip## shanhaiimport subprocess
defshanhai():
ips ={
}
cmd ='ip a'
ret = subprocess.getoutput(cmd)for line in ret.splitlines():if'inet 'in line and'127.0.0.1/8'notin line:
_,ip,*_,name = line.split()
ips[name]= ip
return ips
ret = shanhai()for k,v in ret.items():print("名称:"+ k +"\t"+"ip:"+ v)
6. Get the CPU model and the number of cores in the current system
#!/usr/bin/env python3## cpu_core## shanhaiimport subprocess
defshanhai():
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)
7. Get the current system memory usage: total capacity, available capacity
#!/usr/bin/env python3## Mem## shanhaiimport subprocess
defshanhai():
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)
8. Get the current system disk situation, the device name and total capacity of each disk
#!/usr/bin/env python3## disk## shanhaidefshanhai():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)
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## shanhaiimport subprocess
defshanhai():
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)
10. Output current system CPU load status 1, 5, 15 minutes
#!/usr/bin/env python3 ## cpu_load## shanhaiimport subprocess
defshanhai():
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)