Save ifconfig to variables

Asked

Viewed 95 times

3

It would be possible to execute the command ifconfig and save the result in variables (e.g., ip, network mask etc) in Java?

1 answer

0

For anyone with doubts, my code is like this:

@RequestMapping("/conexaoWireless")
public ResponseEntity<ConexaoWirelessVO> informacoesWireless() throws Exception {

    String interfaces = "";

    Process p = Runtime.getRuntime().exec("ifconfig wlp2s0");
    p.waitFor();

    BufferedReader readerIfconfig = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String linha = "";

    while ((linha = readerIfconfig.readLine())!= null) {
        interfaces += linha;
    }

    Process r = Runtime.getRuntime().exec("iwconfig wlp2s0");
    r.waitFor();

    BufferedReader readerIwconfig = new BufferedReader(new InputStreamReader(r.getInputStream()));

    linha = "";

    while ((linha = readerIwconfig.readLine())!= null) {
        interfaces += linha;
    }


    ConexaoWirelessVO vo = new ConexaoWirelessVO();
    vo.setNomeInterface(interfaces.split(" ")[0]);
    vo.setEnderecoIp(interfaces.substring((interfaces.indexOf("inet addr:")+10), (interfaces.indexOf("inet addr:")+26)).split(" ")[0].trim());
    vo.setMascaraRede(interfaces.substring((interfaces.indexOf("Mask:")+5), (interfaces.indexOf("Mask:")+21)).split(" ")[0].trim());
    vo.setEnderecoMac(interfaces.substring((interfaces.indexOf("HWaddr ")+7), (interfaces.indexOf("HWaddr")+25)).split(" ")[0].trim());
    vo.setPacotesRecebidos(interfaces.substring((interfaces.indexOf("RX packets")+11), (interfaces.indexOf("RX packets")+20)).split(" ")[0].trim());
    vo.setPacotesTransferidos(interfaces.substring((interfaces.indexOf("TX packets")+11), (interfaces.indexOf("TX packets")+20)).split(" ")[0].trim());
    vo.setBytesRecebidos(interfaces.substring((interfaces.indexOf("RX bytes")+9), (interfaces.indexOf("RX bytes")+30)).split("\\)")[0].trim()+")");
    vo.setBytesTransferidos(interfaces.substring((interfaces.indexOf("TX bytes")+9), (interfaces.indexOf("TX bytes")+30)).split("\\)")[0].trim()+")");
    vo.setSsid(interfaces.substring((interfaces.indexOf("ESSID:")+6), (interfaces.indexOf("ESSID:")+30)).split(" ")[0].trim());
    vo.setEnderecoApMac(interfaces.substring((interfaces.indexOf("Access Point: ")+14), (interfaces.indexOf("Access Point: ")+40)).split(" ")[0].trim());
    vo.setBitrate(interfaces.substring((interfaces.indexOf("Bit Rate=")+9), (interfaces.indexOf("Bit Rate=")+20)).split(" ")[0].trim()+" Mb/s");
    vo.setForcaSinal(interfaces.substring((interfaces.indexOf("Signal level=")+13), (interfaces.indexOf("Signal level=")+30)).split(" ")[0].trim()+" dBm");
    vo.setTaxaTransferencia(interfaces.substring((interfaces.indexOf("Tx-Power=")+9), (interfaces.indexOf("Tx-Power=")+30)).split(" ")[0].trim()+" dBm");
    vo.setFrequencia(interfaces.substring((interfaces.indexOf("Frequency:")+10), (interfaces.indexOf("Frequency:")+30)).split(" ")[0].trim()+" GHz");
    vo.setQualidadeLink(interfaces.substring((interfaces.indexOf("Link Quality=")+13), (interfaces.indexOf("Link Quality=")+15)).split(" ")[0].trim());

    return new ResponseEntity<ConexaoWirelessVO>(vo , HttpStatus.OK);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.