如何解决自动检测罗德和施瓦茨示波器的 IP 地址
我正在尝试编写一个 Python 代码,该代码将自动检测 Rhode 和 Schwarz 示波器或使用以太网连接连接到我的笔记本电脑的任何测试设备的 IP 地址。
我工作场所的 R&S 示波器的 IP 地址总是以相同的两个数字 169.254.XX 开头,但诀窍是只有当我使用它访问示波器时,示波器的 IP 地址才会出现在 arp -a 报告中我浏览器中的IP地址。之后 IP 显示在 arp 报告中,但在此之前 IP 地址不存在。
所以我写了下面的一段代码,认为它可以工作,但它没有,因为在浏览器的 URL 栏中输入 IP 地址之前没有显示 IP 地址。 我的目标是通过在我的电脑上点击一个按钮来确定示波器的 IP 地址,而不是点击示波器的触摸屏来找出设备的 IP。
import tkinter as tk
import time
import threading
import queue
import subprocess
root = tk.Tk()
root.title("SCPI test")
canvas1 = tk.Canvas(root,width=200,height=140,bg='lightsteelblue2',relief='raised')
canvas1.pack()
q1 = queue.Queue()
q2 = queue.Queue()
def auto_detect_ip(num1,num2,num3,num4):
ping_list = []
ping_ok_list = []
t1 = time.time()
for i in range(num1,-1):
for j in range(num3,num4,-1):
temp = "169.254." + str(i) + "." + str(j)
# temp = "192.168.1.1"
try:
p = subprocess.run(["ping","-n","1",temp],stdout=subprocess.PIPE,timeout=float(timeout_value))
# p = subprocess.run(["ping",timeout=0.006)
return_code = p.returncode
print("return_code :",return_code)
if return_code == 0:
print("ip_address :",temp)
ping_ok_list.append(temp)
break
except subprocess.TimeoutExpired:
pass
time.sleep(0.00001)
else:
continue
break
len_list_ping = len(ping_list)
print("len_list_ping :",len_list_ping)
print("ping_ok_list :",ping_ok_list)
t2 = time.time()
print("time :",t2 - t1)
if num4 == 0:
q1.put(ping_ok_list)
else:
q2.put(ping_ok_list)
def launch_thread():
print("scan running")
thread_1 = threading.Thread(target=auto_detect_ip,args=(254,128,0))
thread_2 = threading.Thread(target=auto_detect_ip,254,128))
thread_1.setDaemon(True)
thread_2.setDaemon(True)
thread_1.start()
thread_2.start()
def found_rs_scope():
launch_thread()
ping_ok_list = q1.get() + q2.get
print("IP_list :",ping_ok_list)
我的解决方案是扫描每个 IP 地址以找到作用域一,但由于在通过浏览器访问作用域之前,IP 没有出现在提示中的 arp -a 中。 所以在扫描过程中没有匹配,也没有找到 IP。 我认为它应该是可行的,但我只是不知道该怎么做。 此外,我希望能够在不需要管理员权限的情况下找到作用域的 IP 地址。
编辑: 我检查了示波器使用的端口,如 bfris 在他的回答中提到的那样,它是 5025。
解决方法
端口扫描器怎么样?在您的情况下,您可能只关心一个端口(我发现了一个侦听 5025 的 R&S Oscope)。但最多 65536 个 IP 地址。如果您能够连接到正确的端口,那么您可能想要做一个 *IDN?查询是否是正确的乐器。
虽然这会很慢。您要扫描的地址太多了。我将超时设置为 0.01 秒,因此扫描所有地址需要 10 多分钟。我什至不知道 0.010 是否是合法的超时。
您可能会考虑要求您的 IT 部门根据 MACID 为您的范围分配一个准静态地址。这可能是最快的解决方案。
import socket
import sys
part1 = 169
part2 = 254
port = 5025
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.setdefaulttimeout(.01)
sock.settimeout(.01)
for part3 in range(1,254+1):
for part4 in range(1,254+1):
IP = f'{part1}.{part2}.{part3}.{part4}'
result = sock.connect_ex((IP,port))
if result == 0:
print(f'found open port {port} at {IP}')
# add additional code here to verify
# it's the right instrument
# also,probably add break to get out of loop
# if you found the right instrument
#
# break
else:
print('nothing at ',IP)
sock.close()
,
我尝试了以下代码但没有成功:
import socket
import tkinter as tk
root = tk.Tk()
root.title("R&S IP finder")
canvas1 = tk.Canvas(root,width=200,height=80,bg='lightsteelblue2',relief='raised')
canvas1.pack()
def ip_finder():
part1 = 169
part2 = 254
port = 5025
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.setdefaulttimeout(.01)
sock.settimeout(.01)
print("starting scan on port {}".format(port))
for part3 in range(1,254 + 1):
for part4 in range(1,254 + 1):
ip_address = f'{part1}.{part2}.{part3}.{part4}'
# print(ip_address)
result = sock.connect_ex((ip_address,port))
if result == 0:
print(ip_address)
break
else:
continue
print("scan finished.")
sock.close()
Launch_prgm = tk.Button(text="Device detect",command=ip_finder,bg='green',fg='white',font=('helvetica',12,'bold'))
canvas1.create_window(100,35,window=Launch_prgm)
root.mainloop()
这是在我的笔记本电脑上运行的代码的输出,它连接到示波器(rhode 和 schwarz),我想在在浏览器中输入 IP 之前找到它以控制范围或在范围界面本身中找到它.
显然条件 if result == 0: 永远不会为真。 我想我需要管理员权限才能做我想做的事。那真是太可惜了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。