The Python language obtains the Host Name and kills the process based on the port. The python Host Name

Source: Internet
Author: User

The Python language obtains the Host Name and kills the process based on the port. The python Host Name

Recommended: Use 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 !!!
Articles you may be interested in:
  • Python: Getting Windows or Linux host names and sharing common functions
  • Python implements reverse Host Name Lookup Based on IP addresses
  • How to obtain all IP addresses by host name in python

Related Article

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.