Recently, I have a need to list all host names in the LAN (in the SMB protocol), but the findsmb command always fails to list all the host names. I searched the Internet but there was no ready-made solution, so I wrote a python script. The script will scan all IPs in the LAN ARP table and try to resolve their host names, which can list the relevant information more thoroughly. Note that running this script requires the samba-common-bin and arp-scan packages. If you don't have them, please apt install them first. Usage: Run directly or use python3, then enter the name of the network interface to be scanned (if you don’t know, run ifconfig to check, usually ens33, eth0, etc., which appears in the leftmost column of the command output), then press Enter and wait, it may take several minutes to run. Root permissions are required to run! ! #!/usr/bin/env python3 import os def shellrun(cmd): a = os.popen(cmd) b = a.read() c = b.split('\n') return c def cutarpresult(lst): a = [] b = [] for line in lst[2:]: if line != '': a.append(line) else: break for line in a: b.append(line.split('\t')[0]) return b def commandmaker(ip): return 'nmblookup -A ' + ip def getrst(iplist): rst = [] for ip in iplist: rst.append(shellrun(commandmaker(ip))) return rst def washrst(rst): rtn = [] for line in rst: if line[1].split(' ')[1] != 'reply': rtn.append(line[:-1]) return rtn def main(): interface = input('which interface to use: ') iplist = cutarpresult(shellrun('arp-scan -I ' + interface + ' -l')) for rs in washrst(getrst(iplist)): for line in rs: print(line) if __name__ == '__main__': main() This is the end of this article about a script that quickly lists all host names (computer names) in a LAN under Linux. For more information about how to list all host names in a LAN under Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the usage of NULL and NOT NULL when creating tables in MySQL
>>: Detailed explanation of MySQL Innodb storage structure and storage of Null values
Table of contents 1. Demand 2. Implementation 3. ...
Install and configure the MySql database system. ...
mysql copy one table column to another table Some...
This article example shares the specific code of ...
This article shares with you the specific code of...
Table of contents 1. Element offset series 2. Ele...
1. Download the MySQL 5.7.11 zip installation pac...
Table of contents Linux 1. Basic use of crontab 2...
Preface In the development of small programs, we ...
XHTML defines three document type declarations. T...
Sometimes we need to control whether HTML elements...
Table of contents 1. Limit props to type lists 2....
Selector Grouping Suppose you want both the h2 el...
Table of contents 1. Introduction 2. What is func...
Use JS to implement a random roll call system for...