运行命令行调用Python时出错

2024-04-24 23:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,使用NMAP cmd行invoke扫描网络中的活动用户:我的代码如下:

import socket, subprocess, nmap, time,os, invoke


ipaddr = socket.gethostbyname(socket.gethostname())
print (ipaddr)

ip = ipaddr
proc = subprocess.Popen('ipconfig',stdout=subprocess.PIPE)
while True:
    line = proc.stdout.readline()
    if ip.encode() in line:
        break
mask = proc.stdout.readline().rstrip().split(b':')[-1].replace(b'     ',b'').decode()
print(mask)
network = ip + "/" + mask
print(network)
t0 = time.clock()

print("Using network: " + network)
cmd = 'nmap -n -sP -T4 ' + network + ' 2>&1'
res = invoke(cmd)
lines = res.split('\n')
print(lines)
for i in lines:
    m = find('Host\s+\(?([0-9\.]+)\)?\s+appears to be up.', i)
    if m:
        print(m, "\t", nslookup(m))

t1 = time.clock()
print("Scan complete!")
print("it took %d seconds to scan", (t1-t0))

我得到以下错误:

第21行,in res=调用(cmd) TypeError:“module”对象不可调用

请帮忙:D


Tags: inipcmdtimestdoutresmasknetwork
1条回答
网友
1楼 · 发布于 2024-04-24 23:20:13

问题是您调用的是模块,而不是该模块中的函数。您应该使用run函数来运行shell命令。因此,在文件顶部使用本地导入run,并将第21行更改为run(cmd)

from invoke import run

run(cmd)

另一方面,如果要使用import invoke,则必须将第21行更改为:

invoke.run(cmd)

相关问题 更多 >