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

Vue implements fuzzy query-Mysql database data

Table of contents 1. Demand 2. Implementation 3. ...

mysql 5.7.11 winx64.zip installation and configuration method graphic tutorial

Install and configure the MySql database system. ...

Mysql method to copy a column of data in one table to a column in another table

mysql copy one table column to another table Some...

Vue recursively implements three-level menu

This article example shares the specific code of ...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

JavaScript implements three common web effects (offset, client, scroll series)

Table of contents 1. Element offset series 2. Ele...

MySQL 5.7.11 zip installation and configuration method graphic tutorial

1. Download the MySQL 5.7.11 zip installation pac...

How to set up scheduled tasks in Linux and Windows

Table of contents Linux 1. Basic use of crontab 2...

WeChat applet implementation anchor positioning function example

Preface In the development of small programs, we ...

XHTML three document type declarations

XHTML defines three document type declarations. T...

Display and hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

25 Vue Tips You Must Know

Table of contents 1. Limit props to type lists 2....

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

JavaScript functional programming basics

Table of contents 1. Introduction 2. What is func...

JS implements random roll call system

Use JS to implement a random roll call system for...