zoukankan      html  css  js  c++  java
  • Solutions to terminal problems encountered while using Python to execute "airodump-ng"

    Python 3.3.5

    Debian 3.12

    One thing I found weird is using the following code is that the input I entered isn't displayed, even after I terminate the script. However I found a way accidently which is to set stdin as DEVNULL.

    1 try:
    2     stdout = subprocess.check_output(["airodump-ng", "mon0"], stdin=subprocess.DEVNULL, timeout = 5)
    3     #stdout = subprocess.check_output(["sleep", "10"], timeout = self.timeout)
    4 except subprocess.TimeoutExpired:
    5     pass
    6 
    7 a = input("test a test")

    In order to figure it out, first I looked into the code of subprocess.check_output, in which I found it handles the TimeoutExpired exception by using kill. As shown below:

     1 with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
     2     try:
     3         output, unused_err = process.communicate(inputdata, timeout=timeout)
     4     except TimeoutExpired:
     5         process.kill()
     6         output, unused_err = process.communicate()
     7         raise TimeoutExpired(process.args, timeout, output=output)
     8     except:
     9         process.kill()
    10         process.wait()
    11         raise
    12     retcode = process.poll()
    13     if retcode:
    14         raise CalledProcessError(retcode, process.args, output=output)

    And the kill method will send SIGKILL to the process, and this signal goes straight to the kernal without notifying this process, which means the process never gets the opportunity to catch the signal and act on it. As for SIGTERM, the application can determine what it wants to do once a SIGTERM is received. While most applications will clean up their resources and stop.

    While executing in airodump-ng, it first modifies the attributes of terminal to disable the display of input using mygetch function. Then it read input from stdin using getchar, as long as there is no input, the thread running mygetch is blocked, which results in the modified attributes of terminal (Do not echo chars of input). 

     1 int mygetch( ) {
     2   struct termios oldt,
     3                  newt;
     4   int            ch;
     5   tcgetattr( STDIN_FILENO, &oldt );
     6   newt = oldt;
     7   newt.c_lflag &= ~( ICANON | ECHO );
     8   tcsetattr( STDIN_FILENO, TCSANOW, &newt );
     9   ch = getchar();
    10   tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    11   return ch;
    12 }

     Therefore if we set stdin as DEVNULL, the getchar function could return immediately, it won't cause any problems.

    Remember the two signals I mentioned above? SIGTERM and SIGKILL, when I walked through the source code of airodump-ng, I found there is a handler for SIGTERM, as shown below:

    1 if( signum == SIGINT || signum == SIGTERM )
    2 {
    3     reset_term();
    4     alarm( 1 );
    5     G.do_exit = 1;
    6     signal( SIGALRM, sighandler );
    7     dprintf( STDOUT_FILENO, "
    " );
    8 }

    reset_term() will restore the modified terminal attributes through tcsetattr() function.

    So there comes another solution, we can send SIGTERM to make the application restore attributes of the terminal.

    1 p = subprocess.Popen(["airodump-ng", "mon0"])
    2 try:
    3     p.wait(timeout = 5)
    4 except subprocess.TimeoutExpired:
    5     p.terminate()
  • 相关阅读:
    C++ 打印 vector
    使用 winsock 实现简单的 Client 和 Server
    Windows 10 Clion 配置 Opencv 4.0.1
    解决编译的时候头文件找不到的问题
    linux内核打印内存函数print_hex_dump使用方法
    ubuntu180
    驱动编译相关
    Real-Time Rendering 4th Chapter 1 Introduction 简介 转载
    do_gettimeofday使用方法
    6、设备树实践操作
  • 原文地址:https://www.cnblogs.com/kanone/p/4381151.html
Copyright © 2011-2022 走看看