LoginSignup
1
0

More than 3 years have passed since last update.

python 内包表記 使えるテンプレート

Last updated at Posted at 2019-07-28

自分のPCのipアドレスの取得
nicのアドレスを取得

ifconfig.py
import netifaces as n
icmp = lambda x: [*map(int, x.split('.'))]
nn=sorted([n.ifaddresses(i)[n.AF_INET][0]['addr'] for i in n.interfaces() if n.AF_INET in n.ifaddresses(i)],key=ipcmp)
print(nn)

ラムダ式は、アドレスをソートするためにstringからintに変換している

result.txt
['127.0.0.1', '192.168.1.10']

よく使う内包表記

自分のいるネットワークの一覧表

無料wifiでやらないでください。

myNet.py
from kamene.all import *
import requests,json
mac=lambda m:(requests.get('http://macvendors.co/api/%s'% m).json()['result'])['company']
ipcmp = lambda x: [*map(int, x[0].split('.'))]
net=sorted([(x[1][ARP].psrc,x[1][ARP].hwsrc) for x in arping('192.168.1.*', timeout=1, verbose=0)[0]],key=ipcmp)
for x in net: print("%-16s %-20s %-20s"%(x[0],x[1],mac(x[1])))
result.txt
192.168.1.1      xx:xx:xx:3f:8a:21    Mitsubishi Electric Corporation
192.168.1.11     xx:xx:bc:2d:e4:77    PEGATRON CORPORATION
192.168.1.90     xx:xx:92:10:3c:f6    SEIKO EPSON CORPORATION
192.168.1.108    xx:xx:eb:97:47:1a    Raspberry Pi Foundation
192.168.1.111    xx:x:xeb:97:47:1a    Raspberry Pi Foundation
192.168.1.119    xx:xx:2d:68:24:3d    Espressif Inc.      
192.168.1.151    xx:xx:77:17:5d:70    HangZhou Gubei Electronics Technology Co.,Ltd
192.168.1.155    xx:xx:8e:a7:d8:3a    Espressif Inc.      
192.168.1.160    xx:xx:42:75:18:71    HangZhou Gubei Electronics Technology Co.,Ltd
192.168.1.161    xx:xx:42:63:0d:93    HangZhou Gubei Electronics Technology Co.,Ltd
192.168.1.199    xx:xx:xx:21:11:fd    SNOM Technology AG  

1
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0