平台:linux

描述:利用os.popen()函数调用系统命令nmap进行扫描,并用grep命令对扫描结果关键内容进行提取

代码

#!/usr/bin/env pthon
#--*--coding=utf-8--*-- import os
from terminaltables import AsciiTable gateway = '192.168.1.1' def scan():
scan = os.popen("nmap " + gateway + "/24 -n -sP").read()
f = open('scan.txt','w')
f.write(scan)
f.close()
devices = os.popen(" grep report scan.txt | awk '{print $5}'").read()
devices_mac = os.popen("grep MAC scan.txt | awk '{print $3}'").read() + os.popen("ip addr | grep 'state UP' -A1 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'").read().upper() # get devices mac and localhost mac address
devices_name = os.popen("grep MAC scan.txt | awk '{print $4 ,S$5 $6}'").read() + "\033[1;32m(This device)\033[1;m" table_data = [
['IP Address', 'Mac Address', 'Manufacturer'],
[devices, devices_mac, devices_name]
]
table = AsciiTable(table_data)
print(table.table)
os.popen("rm -f scan.txt") if __name__ == '__main__':
scan()

执行结果

关于terminaltables库的使用: https://robpol86.github.io/terminaltables/

实例代码:https://github.com/robpol86/terminaltables/blob/v3.1.0/example1.py(example2.py,example3.py)

>>> from terminaltables import AsciiTable
>>> table_data = [
   ['Heading1', 'Heading2'],
   ['row1 column1', 'row1 column2'],
   ['row2 column1', 'row2 column2'],
   ['row3 column1', 'row3 column2'],
   ]
>>> table = AsciiTable(table_data)

 

python命令调用函数os.popen的更多相关文章

  1. Python之调用函数

    Python之调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数 abs,它接收一个参数. 可以直接从Python的官方网站查 ...

  2. python strip()函数 os.popen()

    函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字 ...

  3. Python——cmd调用(os.system阻塞处理)(多条命令执行)

    os.system(返回值为0,1,2)方法 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os ...

  4. Python——cmd调用(os.system阻塞处理)

    os.system(返回值为0,1,2) 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os.s ...

  5. Python(调用函数、定义函数)

    函数的返回值: return 值:只能返回一次,只要执行return函数就终止 返回值:没有类型限制,也没有个数限制 没有return:None 返回一个值 返回多个值:元组 先定义,后使用,定义阶段 ...

  6. Python文件操作函数os.open、io.open、内置函数open之间的关系

    Python提供了多种文件操作方式,这里简单介绍os.open.io.open.内置函数open之间的关系: 一.内置函数open和io.open实际上是同一个函数,后者是前者的别名: 二.os.op ...

  7. Python笔记:调用函数,带扩号和和不带括号的区别

    调用函数,如果带括号,那么是调用函数运行后的结果, 调用函数不带括号,调用的是函数本身 例如: def cun (a,b): return a+b print(cun) : 调用函数,打印的是函数 p ...

  8. python中调用函数时,参数顺序与参数赋值问题

    设置类和函数如下:class MM(): def ff(self,url(1),method(2),data=None(3),cookie=None(4)): if method.lower()==& ...

  9. python动态调用函数

    callmap = {ts.get_stock_basics: 'D:/dxw/code/all.csv', ts.get_sz50s: 'D:/dxw/code/50.csv', ts.get_hs ...

随机推荐

  1. C#中IList<T>与List<T>的区别

    首先IList 泛型接口是 ICollection 泛型接口的子代,并且是所有泛型列表的基接口.它仅仅是所有泛型类型的接口,并没有太多方法可以方便实用,如果仅仅是作为集合数据的承载体,确实,IList ...

  2. windows下grunt安装提示不成功

    在电脑按了node.js之后,在cmd中 输入 npm install -g grunt-cli 注意啦 不是 npm install -g grunt-cli 中间多了一个空格就安装不成功了,这个空 ...

  3. 代码阅读软件kscope源码安装指导

    安装 kscope-1.6.2 1. ./configure --without-arts --prefix=/soft/kscope-1.6.2  (I customize the installi ...

  4. WCF入门教程(图文)VS2012

    WCF入门教程(图文)VS2012 上一遍到现在已经有一段时间了,先向关注本文的各位“挨踢”同仁们道歉了.小生自认为一个ITer如果想要做的更好,就需要将自己的所学.所用积极分享出来,接收大家的指导和 ...

  5. 【IOS开发】创建XML文件

    - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@ ...

  6. 自定义表单-jsonform

    项目文件地址:https://github.com/powmedia/backbone-forms#editor-list DEOM:http://jsfiddle.net/evilcelery/dW ...

  7. JVM内存划分

    JVM内存划分吗? 前言: 大家都知道虚拟机,都知道JVM,其实这些都是基于sun公司[oracle公司]的HotSpot虚拟机,当然本篇博文也是以sun公司为基础.还有其他的虚拟机,常见的就有JRo ...

  8. [转]The Best Plugins for Sublime Text

    Source: http://ipestov.com/the-best-plugins-for-sublime-text/ Good day, everyone! I tried to collect ...

  9. 关于C#中readonly

    关于C#中readonly的一点小研究 关于C#中readonly的一点小研究   可能园子里有不少文章已经说明了这个问题了,但是我在这里写这篇博客只是写写自己的一些体会,也权当是整理归纳,高手莫见笑 ...

  10. DSP TMS320C6000基础学习(4)—— cmd文件分析

    DSP中的CMD文件是链接命令文件(Linker Command File),以.cmd为后缀. 在分析cmd文件之前,必需先了解 (1)DSP具体芯片的内存映射(Memory Map) (2)知道点 ...