简体   繁体   English

尝试使用python脚本在我的网络上发现iOS设备

[英]Trying to discover iOS devices on my network using python script

Trying to use pybonjour but not sure if it is what I need. 尝试使用pybonjour但不确定它是否是我需要的。 https://code.google.com/p/pybonjour/ https://code.google.com/p/pybonjour/

I want to be able to discover iOS devices that appear on my network automatically, will be running a script later on based on this, but first I want to just discover a iOS devices as soon as it appear/disappears on my wifi network. 我希望能够发现自动出现在我的网络上的iOS设备,稍后将基于此运行脚本,但首先我想在我的wifi网络上出现/消失后立即发现iOS设备。

So the question, how do I do this? 那么问题是,我该怎么做? running on a windows machine with python27 and the pybonjour package installed, the two examples work from the pybonjour page, but what command do I run to discover iOS devices using the scripts included on my network? 在安装了python27和pybonjour软件包的windows机器上运行时,这两个示例在pybonjour页面上运行,但是我使用我的网络中包含的脚本运行什么命令来发现iOS设备? or will this only discovery services running on my pc that i run this script on! 或者这个只运行在我的电脑上运行此脚本的发现服务!

If I am going in the wrong direction please let me know, I can't seem to find the documentation on this package! 如果我走向错误的方向请告诉我,我似乎无法找到关于此包的文档!

python browse_and_resolve.py xxxxxx

Thx Matt. Thx Matt。

Update... 更新中...

This article and the browser was helpful, http://marknelson.us/2011/10/25/dns-service-discovery-on-windows/ in finding the services I needed to search for. 本文和浏览器在查找我需要搜索的服务时很有帮助, http://marknelson.us/2011/10/25/dns-service-discovery-on-windows/

example; 例; (this discovered my apple tv's, not at home atm so can't check what the iphone is called! I assume iphone! (这发现了我的苹果电视,不在家里,所以无法检查iphone的名称!我假设是iphone!

python browse_and_resolve.py _appletv._tcp

Also if you have the windows utility dns-sd.exe this will search for all the services available on the network. 此外,如果您有Windows实用程序dns-sd.exe,它将搜索网络上可用的所有服务。 I used this to find what I was looking for. 我用它来找到我要找的东西。

dns-sd -B _services._dns-sd._udp

Update... 更新中...

"Bonjour is used in two ways: - publishing a service - detecting (browsing for) available services". “Bonjour有两种使用方式: - 发布服务 - 检测(浏览)可用服务”。

For what I want to do, I don't think it will work as the ipad/iPhone won't advertise a service unless I'm running a app that advertise one (or jailbreak my iPhone/ipad and then ssh will be open). 对于我想做的事情,我认为它不会起作用,因为ipad / iPhone不会宣传服务,除非我正在运行一个宣传一个的应用程序(或越狱我的iPhone / ipad然后ssh将打开) 。 Any more ideas? 还有什么想法吗?

What you're trying to do (a) probably can't be done, and (b) probably wouldn't be much use if it could. 您尝试做的事情(a)可能无法完成,(b)如果可能的话,可能没什么用处。

The point of Bonjour is to discover services , not devices . Bonjour的目的是发现服务 ,而不是设备 Of course each service is provided by some device, so indirectly you can discover devices with it… but only by discovering a service that they're advertising. 当然,每个服务都是由某些设备提供的,因此间接地您可以通过它发现设备......但只能通过发现他们正在宣传的服务。

As far as I know, (except Apple TVs) don't advertise any services, except while you're running an app that uses Bonjour to find the same app on other machines. 据我所知,(Apple电视除外)不会宣传任何服务,除非您正在运行使用Bonjour在其他计算机上查找相同应用程序的应用程序。 (Except for jailbroken devices, which often advertise SSH, AFP, etc.) (除了越狱设备,通常广告SSH,AFP等)

There are a few ways to, indirectly, get a list of all services being advertised by anyone on the network. 有几种方法间接获得网络上任何人公布的所有服务的列表。 The simplest is probably to use Bonjour Browser for Windows . 最简单的可能是使用适用于Windows的Bonjour Browser (I've never actually used it, but the original Mac tool and the Java port, both of which I have used, both suggest this Windows port for Windows users.) Fire it up and you'll get a list of services, and you can click on each one to get the details. (我从来没有实际使用它,但原来的Mac工具和Java端口,这两个我用过,都表明此Windows端口Windows用户。)火起来,你会得到服务的列表,你可以点击每一个来获取详细信息。

So, you can verify that your iPhone and iPad aren't advertising any services, which will show that there is no way to detect them via Bonjour. 因此,您可以验证您的iPhone和iPad没有宣传任何服务,这将表明无法通过Bonjour检测它们。

Meanwhile, even if you did find a device, what are you planning to do? 同时,即使你没有找到一个设备,你打算做些什么? Presumably you want to communicate with the device in some way, right? 大概你想以某种方式与设备通信,对吧? Whatever service you're trying to communicate with… just browse for that service—and then, if appropriate, filter down to iOS devices. 无论您尝试与哪种服务进行通信...只需浏览该服务 - 然后,如果适用,请过滤到iOS设备。 That's got to be easier than browsing for iOS devices and then filtering down to those that have the service you want. 这比浏览iOS设备更容易,然后过滤到那些拥有您想要的服务的设备。


As for whether there's any way to detect iOS devices… Well, there are at least two possibilities. 至于是否有任何方法可以检测iOS设备......嗯,至少有两种可能性。 I don't know if either of them will work, but… 我不知道他们中的任何一个是否会起作用,但......

First, even if the iOS device isn't advertising anything for you, I assume it's browsing for services you can advertise. 首先,即使iOS设备没有为您做广告,我也认为它正在浏览可以宣传的服务。 How else does it find that there's an Apple TV to AirTunes to, an iTunes on the LAN to sync with, etc.? 怎么还能找到AirTunes的Apple TV,局域网上的iTunes同步等等?

So, use Bonjour Browser to get a list of all services your iTunes-running desktop, Apple TV, etc. are advertising. 因此,使用Bonjour浏览器获取您的iTunes运行桌面,Apple TV等广告的所有服务的列表。 Then turn off all the services on your desktop, use PyBonjour to advertise whichever services seem plausibly relevant (and, if need be, use netcat to put trivial listeners on the ports you advertise). 然后关闭桌面上的所有服务,使用PyBonjour来宣传看似合理相关的服务(如果需要,还可以使用netcat将琐碎的监听器放在你宣传的端口上)。 Then turn on your iPhone, and see if it connects to any of them. 然后打开你的iPhone,看看它是否连接到任何一个。 You may want to leave it running for a while, or switch WiFi off and back on. 您可能希望让它保持运行一段时间,或者关闭并重新打开WiFi。 (I'm guessing that, despite Apple's recommendations, it doesn't browse continuously for most services, but just checks every once in a while and/or every time its network status changes. After all, Apple's recommendations are for foreground interactive apps, not background services.) (我猜测,尽管有Apple的建议,它不会持续浏览大多数服务,但只是偶尔检查一次和/或每次网络状态发生变化。毕竟,Apple的建议是针对前台互动应用,不是后台服务。)

Unfortunately, even if you can find a service that all iOS devices will connect to, you may not be able to distinguish iOS devices from others just by getting connections there. 不幸的是,即使您可以找到所有iOS设备将连接的服务,您也可能无法通过在那里获得连接来区分iOS设备。 For example, I'm pretty sure any Mac or Windows box running iTunes will hit up your fake AirTunes service, and any Mac will hit your AirPrint, and so on. 例如,我很确定运行iTunes的任何Mac或Windows机器都会打破你的假AirTunes服务,任何Mac都会打到你的AirPrint,依此类推。 So, how do you distinguish that from an iPhone hitting it? 那么,你如何将它与iPhone击中呢? You may need to actually serve enough of the protocol to get information out of them. 您可能需要实际提供足够的协议来从中获取信息。 Which will be particularly difficult for Apple's undocumented protocols. 对于Apple的无证协议,这将特别困难。

But hopefully you'll get lucky, and there will be something that all iOS devices, and nothing else, will want to talk to. 但希望你会幸运,并且会有一些东西,所有iOS设备,没有别的,将要与之交谈。 iTunes Sync seems like the obvious possibility. iTunes Sync看起来很明显。

Alternatively, there are a few things they have to broadcast, or they just wouldn't work. 或者,他们必须播放一些内容,否则它们就无法播放。 You can't get on a WiFi network without broadcasts. 没有广播你就无法上网。 And most home WiFi networks use DHCP, which means they have to broadcast DHCP discover (and request), as well. 并且大多数家庭WiFi网络使用DHCP,这意味着他们也必须广播DHCP发现(和请求)。 There may be some kind of heuristic signature you can detect in these messages. 您可以在这些消息中检测到某种启发式签名。 If nothing else, enabling DDNS should cause the device to send its hostname, and you can guess based on that (eg, unless you change the defaults, hostname.lower().endswith('iphone') ). 如果没有别的,启用DDNS会导致设备发送其主机名,您可以根据它进行猜测(例如,除非您更改默认值, hostname.lower().endswith('iphone') )。

The easiest way is probably to set up your desktop as the main access point for your home network. 最简单的方法可能是将桌面设置为家庭网络的主要访问点。 I believe it's as simple as turning on Internet Connection Sharing somewhere in the control panel. 我相信它就像在控制面板中某处打开Internet连接共享一样简单。 (Setting up as a DHCP relay agent is much less overhead than being a full router, but I have no idea how you'd even get started doing that on Windows.) Then you can capture the DHCP broadcasts (or, failing that, the 802.11 broadcasts) as they come in. Wireshark will capture and parse the messages for you easily, so you can watch and see if it looks like this is worth pursuing farther. (设置为DHCP中继代理比完全路由器的开销要少得多,但我不知道你是怎么开始在Windows上开始这样做的。)然后你可以捕获DHCP广播(或者,如果没有,那么, 802.11广播)当他们进来时, Wireshark将为您轻松捕获和解析消息,因此您可以观看并看看它是否值得追求更远。 (See RFC 2131 for details on the format that aren't obvious from Wireshark's cryptic one-liner descriptions.) (有关Wireshark隐藏的单线描述中不明显的格式的详细信息,请参阅RFC 2131。

You can take this even farther and watch the internet connections every host makes once they're connected to the internet. 你可以把它带到更远的地方,观看每个主人在连接到互联网后所建立的互联网连接。 Any device that's periodically checking the App Store, the iOS upgrade server, etc.… Well, unless one of the jailbreak devteam guys lives in your house, that's probably an iPhone, right? 任何定期检查App Store,iOS升级服务器等的设备......好吧,除非其中一个越狱的devteam家伙住在你家里,那可能是iPhone,对吧? The downside is that some of these checks may be very periodic, and detecting an iPhone 6 hours after it connects to your network isn't very exciting. 缺点是这些检查中的一些可能是非常周期性的,并且在连接到网络6小时后检测到iPhone并不是很令人兴奋。

Use python-nmap rather than Bonjour. 使用python-nmap而不是Bonjour。 Or you could use pyzeroconf (Bonjour is an implementation of zeroconf) but it is a little outdated (but should still work). 或者您可以使用pyzeroconf (Bonjour是zeroconf的一个实现),但它有点过时(但应该仍然有效)。

python-nmap is probably easiest, let's suppose you wanted to find all connected devices that have 'iPhone' or 'iPad' in their hostname (just a simplistic concept): python-nmap可能是最简单的,让我们假设您想找到所有在其主机名中都有“iPhone”或“iPad”的连接设备(只是一个简单的概念):

import nmap

...

def notify_me(ip, hostname):
  print("I found an iOS device! IP Address: %s, Hostname: %s" % (ip, hostname))

iOS_device_list = ['iPhone', 'iPad']
iOS_devices_on_net = {}
nm = nmap.PortScanner()

# scan ip range
for i in range(2, 50, 1):
  ip = "192.168.1." + str(i)
  # specify ports to scan
  nm.scan(ip, '62078') # Matt mentioned that it picks up iphone-sync on this port
  hostname = nm[ip].hostname()
  for device in iOS_device_list:
    if device.lower() in hostname.lower():
      iOS_devices_on_net.update({ip:hostname})
      notify_me(ip, hostname)

# show all iOS devices in ip range
print iOS_devices_on_net

The limitation of this approach is that it relies on the individual having not changed their hostname which originally includes their name and device name. 这种方法的局限性在于它依赖于未更改主机名的个人,该主机名最初包括其名称和设备名称。 It also assumes that there is a port listening on the iOS device that will return a hostname (this may not be the case). 它还假设在iOS设备上有一个端口侦听将返回主机名 (可能不是这种情况)。 You can use osscan which is preferred by running it as a command using python-nmap library. 您可以使用osscan作为使用python-nmap库的命令运行它。 This is obviously a much better approach. 这显然是一种更好的方法。 My concept above is just a simple example of how it can be used. 我上面的概念只是一个如何使用它的简单例子。

Using nmap from the command line (I believe python-nmap has nm.commandline() method) is simplest: 从命令行使用nmap(我相信python-nmap有nm.commandline()方法)是最简单的:

nmap -O -v ip

Also try adding --osscan-guess; --fuzzy 另外尝试添加--osscan-guess; --fuzzy --osscan-guess; --fuzzy for best results. --osscan-guess; --fuzzy以获得最佳效果。 Example: 例:

nmap -O -v --osscan-guess ip

Then just search the output for iOS device keywords (see this example ). 然后只搜索iOS设备关键字的输出(请参阅此示例 )。 It's human-readable. 这是人类可读的。 Note that you'll need to be running all of this as an administrator for it to work properly (Windows: runas , other: sudo ). 请注意,您需要以管理员身份运行所有这些才能正常工作(Windows: runas ,其他: sudo )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM