推荐阅读:使用python检测主机存活端口及检查存活主机
下面给大家分享使用python语言实现获取主机名根据端口杀死进程代码。
ip=os.popen(“ifconfig eth0|grep ‘inet addr’|awk -f ‘:’ ‘{print $2}’|awk ‘{print $1}'”)
ip=ip..read().strip()
pnetstat -anp|grep 8998 |awk ‘{print $7}'”).read().split(‘/’)[0]
os.popen(‘kill -9 {0}’.format(int(pid)))
下面给大家介绍python语言实现根据pid杀死相应进程,kill_process.py代码如下
#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import signal
def kill(pid):
try:
a = os.kill(pid, signal.sigkill)
# a = os.kill(pid, signal.9) # 与上等效
print ‘已杀死pid为%s的进程, 返回值是:%s’ % (pid, a)
except oserror, e:
print ‘没有如此进程!!!’
if __name__ == ‘__main__’:
kill(8132)
ok, enjoy it !!!