Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] Problema con ifconfig e arraylist

    questo è l'output di ifconfig:
    codice:
    lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    	inet6 ::1 prefixlen 128 
    	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    	inet 127.0.0.1 netmask 0xff000000 
    gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
    stf0: flags=0<> mtu 1280
    en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    	ether 34:15:9e:15:8a:38 
    	media: autoselect
    	status: inactive
    en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    	ether 58:b0:35:63:0d:e9 
    	inet6 fe80::5ab0:35ff:fe63:de9%en1 prefixlen 64 scopeid 0x5 
    	inet 10.0.1.5 netmask 0xffffff00 broadcast 10.0.1.255
    	media: autoselect
    	status: active
    da questo output io vorrei estrapolare solo il nome (lo0, gif0,stf0,....) e poi riempirci un arraylist.
    codice:
        public static void discover() throws IOException {
            ArrayList<String> list = new ArrayList<String>();
            Process p = Runtime.getRuntime().exec("ifconfig");
            InputStream input = p.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
    //            System.out.println(line);
                list.add(line);
            }
            for (Object obj : list) {
                System.out.println(list);
            }
            br.close();
            reader.close();
            input.close();
        }
    il problema è che tutto l'ouput viene considerato insieme.
    in sostanza line è tutto l'output del comando.
    come posso fare per spezzarlo prendendo solo determinate parti??

  2. #2
    su suggerimento di un altro utente questo comando da quello che voglio:
    ifconfig -a | grep -Eo "^[a-z]+[0-9]*"

    il problema è che invece nel metodo non restituisce nulla:
    codice:
        public static void discover() throws IOException {
            ArrayList<String> list = new ArrayList<String>();
            Process p = Runtime.getRuntime().exec("ifconfig -a | grep -Eo \"^[a-z]+[0-9]*\"");
            InputStream input = p.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            System.out.println(list);
            br.close();
            reader.close();
            input.close();
        }
    ovviamente ho delle prove e il solo comando ifconfig -a funziona.
    penso quindi sia un problema di pipe.
    c'è un modo per fargli eseguire tutto il comando composto??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.