Python language implementation gets host name kills process based on port

Source: Internet
Author: User
Recommended reading: Using Python to detect host survival ports and check for surviving hosts

Let's share with you the Python language implementation gets the hostname to kill the process code based on the port.

Ip=os.popen ("Ifconfig eth0|grep ' inet addr ' |awk-f ': ' {print $} ' |awk ' {print $} ') 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)))

below to introduce the Python language implementation according to the PID kill the corresponding process, kill_process.py code 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) # with the equivalent print ' killed PID for the process of%s, the return value is:%s '% (PID, a) except OSError, E:print ' no such process!!! ' if __name__ = = ' __main__ ': Kill (8132) OK, Enjoy it!!!
  • 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.