python函数编写脚本
1.输入一个用户名判断是否存在#!/usr/bin/env python3## user## shanhaiimport subprocessprint ("请输入用户名")x = input(">>:")def user_input(x):while True:if not x:continueif x == 'q':breakret = subprocess.ge
·
1.输入一个用户名判断是否存在
#!/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)
2.输出当前系统中所有的普通用户
#!/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)
3.输出目前系统中都监听了哪些端口
#!/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)
4.输入一个端口,判断服务是否开启
#!/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)
5.获取当前系统中非回环接口的 IPv4地址
#!/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)
6.获取当前系统中 CPU 型号, 内核数量
#!/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)
7.获取当前系统内存使用情况:总容量,可用容量
#!/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)
8.获取当前系统的磁盘情况,每块磁盘的设备名称和总容量
#!/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)
9.获取当前系统磁盘的有效分区情况,打印出挂载点和分区容量
#!/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)
10.输出当前系统的 CPU 负载情况 1 ,5 ,15 分钟
#!/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)
更多推荐
已为社区贡献2条内容
所有评论(0)