Obtain the host name in Python and kill the process by port.

Source: Internet
Author: User
This article mainly introduces the Python language for obtaining information about the host name killing process based on the port. For more information, see: Using python to detect the active port of the host and check the active host.

We will share with you the code for using the python language to obtain the Host Name and kill the process based on the port.

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 )))

The following describes how to kill_process.py uses a pid to kill the corresponding process. The code for kill_process.py is as follows:

#! /Usr/bin/python #-*-coding: UTF-8-*-import osimport sysimport signaldef kill (pid): try: a = OS. kill (pid, signal. SIGKILL) # a = OS. kill (pid, signal.9) # process equivalent to print 'killed pid % s, return value: % s' % (pid, a) failed t OSError, e: print 'no such process !!! 'If _ name _ = '_ main _': kill (8132) OK, Enjoy it !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.