Script to quickly list all host names (computer names) in the LAN under Linux

Script to quickly list all host names (computer names) in the LAN under Linux

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:
  • Python socket realizes message transmission between windows and linux in LAN
  • A graphic tutorial on configuring Linux system LAN and extranet access under VMware
  • A unique approach: remote access to LAN under Linux
  • How to change the host name in Linux
  • How to modify the network card name and host name in Linux
  • Python gets Windows or Linux host name general function sharing
  • How to change the Linux host name

<<:  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

Recommend

CSS3 uses the transition property to achieve transition effects

Detailed description of properties The purpose of...

10 skills that make front-end developers worth millions

The skills that front-end developers need to mast...

Tutorial on installing MySQL 5.7.18 using RPM package

system: CentOS 7 RPM packages: mysql-community-cl...

How to Learn Algorithmic Complexity with JavaScript

Table of contents Overview What is Big O notation...

The whole process record of introducing Vant framework into WeChat applet

Preface Sometimes I feel that the native UI of We...

Example analysis to fix problems in historical Linux images

Fix for issues with historical Linux images The E...

MySQL performance optimization index pushdown

Index condition pushdown (ICP) is introduced in M...

Docker uses Supervisor to manage process operations

A Docker container starts a single process when i...

Detailed explanation of the interaction between React Native and IOS

Table of contents Prerequisites RN passes value t...

Analyze Mysql transactions and data consistency processing issues

This article analyzes the consistency processing ...

MySQL 8.0.15 installation graphic tutorial and database basics

MySQL software installation and database basics a...

JavaScript macrotasks and microtasks

Macrotasks and Microtasks JavaScript is a single-...

Detailed explanation of MySQL slow log query

Slow log query function The main function of slow...