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

MySQL 5.7.21 winx64 installation and configuration method graphic tutorial

This article summarizes the notes for installing ...

MySQL SQL Optimization Tutorial: IN and RANGE Queries

First, let's talk about the in() query. It is...

How to build a MySQL high-availability and high-performance cluster

Table of contents What is MySQL NDB Cluster Preli...

Design a data collector with vue

Table of contents Scenario Core Issues Status mon...

Linux system calls for operating files

Table of contents 1. Open the file Parameter Intr...

Tips on MySQL query cache

Table of contents Preface Introduction to QueryCa...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...

The ultimate solution for writing bash scripts with nodejs

Table of contents Preface zx library $`command` c...

202 Free High Quality XHTML Templates (2)

Following the previous article 202 Free High-Qual...

Super simple qps statistics method (recommended)

Statistics of QPS values ​​in the last N seconds ...

MySQL multi-table join introductory tutorial

Connections can be used to query, update, and est...

Install Memcached and PHP Memcached extension under CentOS

Regarding the high-performance distributed memory...

Complete steps to build a Laravel development environment using Docker

Preface In this article, we will use Docker to bu...