'Sh' 对象没有属性
'Sh' object has no attribute
我试图列出我网络上的所有 IP 地址,我找到了这段代码,但我 运行 遇到了这个问题。它表明 sh 没有属性。
我试过很多东西,比如导入 pbs 和把 sh 变成 class。
我目前正在使用 windows 10 和 运行 最新的 python 版本。
import pbs
class Sh(object):
def getattr(self, attr):
return pbs.Command(attr)
sh = Sh()
for num in range(10,40):
ip = "192.168.0."+str(num)
try:
sh.ping(ip, "-n 1",_out="/dev/null")
print("PING ",ip , "OK")
except sh.ErrorReturnCode_1:
print("PING ", ip, "FAILED")
我应该看到我相信的 IP 地址列表,但我得到的是:
Traceback (most recent call last):
File "scanner.py", line 11, in <module>
sh.ping(ip, "-n 1",_out="/dev/null")
AttributeError: 'Sh' object has no attribute 'ping'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "scanner.py", line 13, in <module>
except sh.ErrorReturnCode_1:
AttributeError: 'Sh' object has no attribute 'ErrorReturnCode_1'
有帮助吗?
在 Linux 上测试。
(Windows' ping 的文档)
使用模块 sh 应该是
import sh
for num in range(10, 40):
ip = "192.168.0." + str(num)
try:
sh.ping(ip, "-n 1", "-w 1") #, _out="nul") # Windows 10
#sh.ping(ip, "-c 1", "-W 1") #, _out="/dev/null") # Linux
print("PING", ip, "OK")
except sh.ErrorReturnCode_1:
print("PING", ip, "FAILED")
Windows 10 没有设备 /dev/null
(存在于 Linux),但它可能可以使用 nul
来跳过文本。
在 Linux 上即使没有 _out
也不会显示文本,所以也许在 Windows 上不需要 _out
。
Linux 使用 -c 1
只进行一次 ping。 Windows -n 1
或 /n 1
。我还使用 -W 1
在 1 秒后超时 - 所以它不会等待响应太久。 Windows 可能使用 -w 1
或 /w 1
使用模块 pbs
可能你只需将所有 sh
替换为 pbs
import pbs
和
except pbs.ErrorReturnCode_1:
但是我没有这个模块来测试它。
使用标准模块 os
它需要 /dev/null
在 Linux
import os
for num in range(10, 40):
ip = "192.168.0." + str(num)
exit_code = os.system("ping -n 1 -w 1 " + ip + " > nul") # Windows
#exit_code = os.system("ping -c 1 -W 1 " + ip + " > /dev/null") # Linux
if exit_code == 0:
print("PING", ip, "OK")
else:
print("PING", ip, "FAILED")
我试图列出我网络上的所有 IP 地址,我找到了这段代码,但我 运行 遇到了这个问题。它表明 sh 没有属性。
我试过很多东西,比如导入 pbs 和把 sh 变成 class。 我目前正在使用 windows 10 和 运行 最新的 python 版本。
import pbs
class Sh(object):
def getattr(self, attr):
return pbs.Command(attr)
sh = Sh()
for num in range(10,40):
ip = "192.168.0."+str(num)
try:
sh.ping(ip, "-n 1",_out="/dev/null")
print("PING ",ip , "OK")
except sh.ErrorReturnCode_1:
print("PING ", ip, "FAILED")
我应该看到我相信的 IP 地址列表,但我得到的是:
Traceback (most recent call last):
File "scanner.py", line 11, in <module>
sh.ping(ip, "-n 1",_out="/dev/null")
AttributeError: 'Sh' object has no attribute 'ping'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "scanner.py", line 13, in <module>
except sh.ErrorReturnCode_1:
AttributeError: 'Sh' object has no attribute 'ErrorReturnCode_1'
有帮助吗?
在 Linux 上测试。
(Windows' ping 的文档)
使用模块 sh 应该是
import sh
for num in range(10, 40):
ip = "192.168.0." + str(num)
try:
sh.ping(ip, "-n 1", "-w 1") #, _out="nul") # Windows 10
#sh.ping(ip, "-c 1", "-W 1") #, _out="/dev/null") # Linux
print("PING", ip, "OK")
except sh.ErrorReturnCode_1:
print("PING", ip, "FAILED")
Windows 10 没有设备 /dev/null
(存在于 Linux),但它可能可以使用 nul
来跳过文本。
在 Linux 上即使没有 _out
也不会显示文本,所以也许在 Windows 上不需要 _out
。
Linux 使用 -c 1
只进行一次 ping。 Windows -n 1
或 /n 1
。我还使用 -W 1
在 1 秒后超时 - 所以它不会等待响应太久。 Windows 可能使用 -w 1
或 /w 1
使用模块 pbs
可能你只需将所有 sh
替换为 pbs
import pbs
和
except pbs.ErrorReturnCode_1:
但是我没有这个模块来测试它。
使用标准模块 os
它需要 /dev/null
在 Linux
import os
for num in range(10, 40):
ip = "192.168.0." + str(num)
exit_code = os.system("ping -n 1 -w 1 " + ip + " > nul") # Windows
#exit_code = os.system("ping -c 1 -W 1 " + ip + " > /dev/null") # Linux
if exit_code == 0:
print("PING", ip, "OK")
else:
print("PING", ip, "FAILED")