Solutions to terminal problems encountered while using Python to execute "airodump-ng"

來源:互聯網
上載者:User

標籤:

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     pass6 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         raise12     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, "\n" );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()

 

Solutions to terminal problems encountered while using Python to execute "airodump-ng"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.