Heim  >  Artikel  >  Backend-Entwicklung  >  Python语言实现获取主机名根据端口杀死进程

Python语言实现获取主机名根据端口杀死进程

WBOY
WBOYOriginal
2016-06-10 15:05:221360Durchsuche

推荐阅读:使用python检测主机存活端口及检查存活主机

下面给大家分享使用python语言实现获取主机名根据端口杀死进程代码。

ip=os.popen("ifconfig eth0|grep 'inet addr'|awk -F ':' '{print $2}'|awk '{print $1}'")
ip=ip..read().strip()
pid=os.popen("netstat -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 !!!
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn