我正在尝试获取未知SMB服务器的所有共享设备。尝试通过SMBConnection.connect()函数进行连接时,它拒绝连接至服务器,并且连接丢失或完全没有连接。同时,我在Python中遇到异常,可以从我的桌面环境连接到该服务器。是什么原因造成的。我列出了从Linux到Windows主机不等的SMB服务器。
from smb.SMBConnection import SMBConnection
import socket
ips ['x.x.x.x','x.x.x.x'...]
for ip in ips :
print(ip)
try:
hostname = socket.gethostbyaddr(ip)
except socket.herror as e :
print("[-][-][-]Uknknown host")
continue
smb_conn = SMBConnection(username='Guest',password="",my_name='name',remote_name=hostname)
try:
is_connected = smb_conn.connect(ip,timeout=60)
except :
print("[!] %s is up but refuses smb connection:(( "%ip)
#print("Na not responding shit")
continue
if is_connected:
print("[+] %s connected"%ip)
try:
shares = smb_conn.listShares()
for share in shares :
print(share.name)
print("\n\n\n")
except smb.smb_structs.OperationFailure as e:
print (e.message)
break
更新:执行此脚本时,在服务器启动并运行且可通过Desktop SMB连接访问时,出现TimeoutError。
已解决:在某些情况下,SMB服务器正在侦听端口445,而不是NetBIOS端口139。默认情况下,connect()函数尝试连接到端口139,但可以通过connect(ip,port,timeout)参数对其进行修改。