B4J Question Help Getting UID for Linux OS

Nokia

Active Member
Licensed User
Longtime User
Does anybody have a reliable way to get a good UID for linux os without generating one?

I’ve tried the following code and does not work.

B4X:
#If java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;
import static javax.swing.JOptionPane.showMessageDialog;


    private static String sn = null;
    
    public static final String getLinuxSN() {

        if (sn == null) {
            readDmidecode();
        }
        if (sn == null) {
            readLshal();
        }
        if (sn == null) {
            throw new RuntimeException("Cannot find computer SN");
        }

        return sn;
    }

    private static BufferedReader read(String command) {

        OutputStream os = null;
        InputStream is = null;

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec(command.split(" "));
        } catch (IOException e) {
            showMessageDialog(null, e);
            throw new RuntimeException(e);
        }

        os = process.getOutputStream();
        is = process.getInputStream();

        try {
            os.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return new BufferedReader(new InputStreamReader(is));
    }

    private static void readDmidecode() {

        String line = null;
        String marker = "Serial Number:";
        BufferedReader br = null;

        try {
            br = read("dmidecode -t system");
            while ((line = br.readLine()) != null) {
                if (line.indexOf(marker) != -1) {
                    sn = line.split(marker)[1].trim();
                    break;
                }
            }
        } catch (IOException e) {
            showMessageDialog(null, e);
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private static void readLshal() {

        String line = null;
        String marker = "system.hardware.serial =";
        BufferedReader br = null;

        try {
            br = read("lshal");
            while ((line = br.readLine()) != null) {
                if (line.indexOf(marker) != -1) {
                    showMessageDialog(null, line);
                    sn = line.split(marker)[1].replaceAll("\\(string\\)|(\\')", "").trim();
                    break;
                }
            }
        } catch (IOException e) {
            showMessageDialog(null, e);
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    showMessageDialog(null, e);
                    throw new RuntimeException(e);
                }
            }
        }
    }
    

    /**
     * Method for get Linux Machine MotherBoard Serial Number
     * @return
     */
    public static String GetLinuxMotherBoard_serialNumber() {
        String command = "dmidecode -s baseboard-serial-number";
        String sNum = null;
        try {   
            Process SerNumProcess = Runtime.getRuntime().exec(command);
            BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
            sNum = sNumReader.readLine().trim();
            SerNumProcess.waitFor();
            sNumReader.close();
        }
        catch (Exception ex) {
            showMessageDialog(null, ex.getMessage());
            System.err.println("Linux Motherboard Exp : "+ex.getMessage());
            sNum ="error";
        }
        return sNum;
    }
        

#End If
 

behnam_tr

Active Member
Licensed User
Longtime User
check this library

B4X:
Dim p As Padina_Lib
    
    Log(p.GetMotherboardSN)
    Log(p.UUID_Drive_Serial)
    Log(p.UUID_Drive_Serial2)
    Log(p.UUID_Serial)
    Log(p.UUID_MacId)
 

Attachments

  • jpadina_lib 1.3.zip
    23.1 KB · Views: 175
Upvote 0

madru

Active Member
Licensed User
Longtime User
UID or UUID?

execute in a shell

UUID:
cat /proc/sys/kernel/random/uuid

UID:
id -u user
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User

Thanks for the Padinia_Lib..

I put it into my test and and ran it on Ubuntu Linux system and the only item it returned was the mac id.. is this the mac for network card?

also is this a personal lib you made?
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
Thanks for the Padinia_Lib..

I put it into my test and and ran it on Ubuntu Linux system and the only item it returned was the mac id.. is this the mac for network card?

also is this a personal lib you made?

yes mac id is for network card and is unique id
yes.its a personal

if you want I can share java codes.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
yes mac id is for network card and is unique id
yes.its a personal

if you want I can share java codes.

the only problem with using the mac address, it changes if someone stops using wifi and plugs into nic port or if they don't have wifi on at all.. then no ID shows..
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
UID or UUID?

execute in a shell

UUID:
cat /proc/sys/kernel/random/uuid

UID:
id -u user

I can randomly generate a UUID already, I was wanting something that does not change on the linux computer unless they change hardware or reinstall the OS. something I don't have to use root permissions to get..

I see the UUID of the disk drives in /dev/disks/by-uuid/ the problem here is when I put in a usb drive or any other drive it adds a file.. unless I can singe out the one file.. don't know fi that will work for me or not...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…