As a worker, especially 996, 007’s work, in addition to cooking, the most exciting than the work of occasionally secretly idle, go to the pond fish. \

What kind of fish does the average person catch? Chat, microblog, wechat circle of friends, small games, novels, stock funds and so on.

What’s the biggest obstacle to fishing? The contractors (bosses), of course, expect them to work 24 hours a day. \

But people’s energy is limited, can only concentrate on so many hours a day, other time through fishing to adjust the limited, so as long as we fishing not found by the contractor, it is quite a pleasant thing.

To this end, I wrote a small tool in Python — BOSS is coming — to monitor the BOSS and reduce the probability of being caught.

Train of thought

As we know, every computer, mobile phone and other terminals has a fixed Mac address, and there are several APS in the office area of our company. Everyone’s mobile phone is connected to the NEAREST AP. Therefore, if I know the Mac address of my boss’s mobile phone and scan all THE Mac addresses in the LAN, If the Mac address of the boss’s mobile phone appears, then the boss is probably near me, and it is dangerous to touch the fish. If there is no Mac address of the boss, then the boss may be far away from me, this time it is safer to touch the fish.

All I have to do is get the Mac address of my boss’s phone, and then poll all the Mac addresses in the LAN. Once I find the Mac address of my boss’s phone, I do my job. Once the Mac address of my boss disappears, I can do my job.

implementation

Get the boss’s Mac address

How do I get the boss’s Mac address?

A lot of people hear this may feel lost! You can’t just steal the boss’s phone and look it up in Settings.

There is always a way out, as long as you are willing to use your brain, there are many ways!

Here’s how I did it. Save your LAN Mac address once when your boss is in town and again when he is gone, and then compare it to find the Mac address on his phone. To ensure accuracy, try several times.

Obtain all Mac addresses

The first step is to find the current network segment using the ipconfig/all command:

The second step is to ping IP addresses in network segments one by one using polling commands to establish ARP tables. The command is as follows:

For /L % I IN (1,1,254) DO ping -w 1 -n 1 192.168.1.% I

% I indicates the network segment to be queried.

Step 3 Run the arp command to query all Mac addresses.

arp -a

After running, you should see something like the following: \

MAC Address List

Code implementation

With the idea validated and the preparation done, it’s time to implement the code.

First, we write a method to obtain all the Mac addresses of the LAN according to the above ideas.

def get_macs(): Res = os.popen(" arp-a ") Arps = res.read() print(arps) # Result = arps.split('\n') # set an empty list ips = [] # Macs = [] # for I in range(1, len(result)): # to get the list of the first data line independence idx = result [I] the if (' Internet 'in line) | (" = = line) | (' interface' in the line) : Line_split = line.split(" ") index = 0 for lin line_split: if l! = '': index += 1 if index == 1: ips.append(l) elif index == 2: macs.append(l) return ips, macsCopy the code

Then, write a timed poll.

BossMac = "01-00-5E-0b-14-01" sleep_time = 5 while 1 == 1: time.sleep(sleep_time) ips, macs = get_macs() is_come = 0 for mac in macs: if mac == bossMac: Is_come = 2 # If boss is coming, scan every 5 minutes sleep_time = 300 # alert to the alarm, choice = g.mcSgbox (MSG =" there is a ghost, abort the deal! ") , title="OMG") break if is_come == 0: # If the boss is gone, scan sleep_time = 5 g.msbox (MSG =" everything is ok! ") , title="OMG")Copy the code

My goal here is to poll every 5 minutes if the boss shows up, because when he’s around, you need to focus on your work and not think about it too often. If your boss is gone, poll every 5 seconds, and give frequent warning!

When the boss arrives, the warning popup looks like this:

When the boss disappears, the popover reads: \

conclusion

Of course, this doesn’t work if the boss doesn’t have WiFi on. Or the boss is coming and the phone is slow and doesn’t switch to the AP over here, that’s also a danger. So don’t rely entirely on this gadget. Still, take a look at your surroundings once in a while.

Source: github.com/JustDoPytho…

At the end of the article

Your likes collection is the biggest encouragement to me! Welcome to follow me, share Python dry goods, and exchange Python technology. If you have any comments or technical questions about this article, please leave a comment in the comments section.