python实现局域网ip地址扫描
2014-03-24 16:01
721 查看
python 遍历局域网ip
从知道python开始,我的视线里就没缺少过他。尤其是现如今开发语言大有傻瓜化的趋势。而作为这一趋势的领导,脚本语言就显得格外亮眼。不管是python还是ruby,perl,都火的不得了。就连java都出了个脚本语言版本,好像是叫Groovy,号称下一代的java。
也难怪,硬件发展使得很多场合的处理性能过剩。脚本语言的缺点正在被逐步缩小。扯得有点远了。
程序很简单:
用到了subprocess模块,下面是用法:
subprocess的目的就是启动一个新的进程并且与之通信。
subprocess模块中只定义了一个类: Popen。可以使用Popen来创建进程,并与进程进行复杂的交互。它的构造函数如下:
subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
参数stdin, stdout, stderr分别表示程序的标准输入、输出、错误句柄。他们可以是PIPE(数据管道),文件描述符或文件对象,也可以设置为None,表示从父进程继承。如果参数shell设为true,程序将通过shell来执行。参数env是字典类型,用于指定子进程的环境变量。如果env
= None,子进程的环境变量将从父进程中继承。
subprocess.PIPE
在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。
subprocess.STDOUT
创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出。
Popen的方法:
Popen.poll()
用于检查子进程是否已经结束。设置并返回returncode属性。
Popen.wait()
等待子进程结束。设置并返回returncode属性。
Popen.communicate(input=None)
与子进程进行交互。向stdin发送数据,或从stdout和stderr中读取数据。可选参数input指定发送到子进程的参数。Communicate()返回一个元组:(stdoutdata, stderrdata)。注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,参数stdin必须被设置为PIPE。同样,如果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。
Popen.send_signal(signal)
向子进程发送信号。
Popen.terminate()
停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess()来结束子进程。
Popen.kill()
杀死子进程。
Popen.stdin,Popen.stdout ,Popen.stderr ,官方文档上这么说:
stdin, stdout and stderr specify
the executed programs’ standard input, standard output and standard error
file handles, respectively. Valid values are PIPE,
an existing file descriptor (a positive integer), an existing file object, and None.
Popen.pid
获取子进程的进程ID。
Popen.returncode
获取进程的返回值。如果进程还没有结束,返回None。
从知道python开始,我的视线里就没缺少过他。尤其是现如今开发语言大有傻瓜化的趋势。而作为这一趋势的领导,脚本语言就显得格外亮眼。不管是python还是ruby,perl,都火的不得了。就连java都出了个脚本语言版本,好像是叫Groovy,号称下一代的java。
也难怪,硬件发展使得很多场合的处理性能过剩。脚本语言的缺点正在被逐步缩小。扯得有点远了。
import subprocess cmd="cmd.exe" begin=101 end=200 while begin<end: p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) p.stdin.write("ping 192.168.1."+str(begin)+"\n"); p.stdin.close() p.wait() print"result is:%s"%p.stdout.read() begin+=1;
程序很简单:
用到了subprocess模块,下面是用法:
subprocess的目的就是启动一个新的进程并且与之通信。
subprocess模块中只定义了一个类: Popen。可以使用Popen来创建进程,并与进程进行复杂的交互。它的构造函数如下:
subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
参数stdin, stdout, stderr分别表示程序的标准输入、输出、错误句柄。他们可以是PIPE(数据管道),文件描述符或文件对象,也可以设置为None,表示从父进程继承。如果参数shell设为true,程序将通过shell来执行。参数env是字典类型,用于指定子进程的环境变量。如果env
= None,子进程的环境变量将从父进程中继承。
subprocess.PIPE
在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。
subprocess.STDOUT
创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出。
Popen的方法:
Popen.poll()
用于检查子进程是否已经结束。设置并返回returncode属性。
Popen.wait()
等待子进程结束。设置并返回returncode属性。
Popen.communicate(input=None)
与子进程进行交互。向stdin发送数据,或从stdout和stderr中读取数据。可选参数input指定发送到子进程的参数。Communicate()返回一个元组:(stdoutdata, stderrdata)。注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,参数stdin必须被设置为PIPE。同样,如果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。
Popen.send_signal(signal)
向子进程发送信号。
Popen.terminate()
停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess()来结束子进程。
Popen.kill()
杀死子进程。
Popen.stdin,Popen.stdout ,Popen.stderr ,官方文档上这么说:
stdin, stdout and stderr specify
the executed programs’ standard input, standard output and standard error
file handles, respectively. Valid values are PIPE,
an existing file descriptor (a positive integer), an existing file object, and None.
Popen.pid
获取子进程的进程ID。
Popen.returncode
获取进程的返回值。如果进程还没有结束,返回None。
相关文章推荐
- python实现局域网ip地址扫描
- Python实现扫描局域网活动ip(扫描在线电脑)
- C#实现局域网IP的扫描,从而打印出IP地址,主机名称和MAC地址
- [PythonCode]扫描局域网的alive ip地址
- [PythonCode]扫描局域网的alive ip地址
- Python实现局域网IP端口扫描
- python多线程实现局域网活跃IP扫描
- Python实现扫描指定目录下的子目录及文件的方法
- Python实现根据IP地址和子网掩码算出网段的方法
- 实现shell多进程局域网扫描
- Python+Socket实现基于UDP协议的局域网广播功能示例
- 【Python】局域网ping扫描在线主机
- Python基于多线程实现ping扫描功能示例
- 多线程技术实现局域网快速扫描
- python实现封装得到virustotal扫描结果
- Python实现扫描指定目录下的子目录及文件的方法
- 如何高效实现扫描局域网IP、主机名、MAC和端口
- 区域填充之扫描线种子法的Python实现
- Netty框架基于UDP实战(一):局域网扫描功能的实现
- 【Python】实现网站常见漏洞扫描+备份扫描+源码分析