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, "\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()
时间: 2024-10-15 22:43:59

Solutions to terminal problems encountered while using Python to execute "airodump-ng"的相关文章

Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)

Acknowledgments I would like to thank Jacob Kjome for reviewing early drafts of this document. His comments helped to clarify several important points. Jake also keeps reminding us on the log4j-dev mailing list that the child-parent delegation model

Problems encountered while deleting resources.

Error The project was not built due to “Problems encountered while deleting resources.”. Fix the problem, then try refreshing this project and rebuilding it since it may be inconsistent. learndiary 删除classes,刷新工程,重新编译.

Javascript > Eclipse > problems encountered during text search

Reproduce: Ctrl + H, Select "File Search", will encounter eclipse kinds of bug/error alert: problems encountered during text search Reason: It's because Ctrl + H by default will search disk files, and when the disk files get un-sync with eclipse

python 执行execute遇到的问题

1.如下方式去查询无法查询出结果,但直接在数据库查询中去查询是能查询到结果的,郁闷中,花了很久的时间才知道原来是双引号导致的 把:name="%s" 中的%s前后的双引号去掉就对了 2.通过自己写的方法去查询数据库的数据时必须要用到cur.fetchall(),不然查询的结果显示不出,很多时候容易忘记 3.我之所以出现1的问题,是因为,如下图,用此种方式写sql语句时,如果字段为非数字的字符串时需加双引号

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

(转) [it-ebooks]电子书列表

[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/Learning Web App Development || Build Quickly with Proven JavaScript Techniques http://

Python著名的lib和开发框架(均为转载)

第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Admin Panels Algorithms and Design Patterns Anti-spam Asset Management A

Python数据结构(二)

5.2. The del statement There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l