Как узнать Wi-Fi или сотовую связь с помощью Java на Windows 10

Изменено из команды answer команды Ciro Santilli ss внутри определенного источника. Вы можете указать имя исходного файла, или будет выведено текущее. Очень удобен для перехода через источники bison / yacc или другие мета-источники, которые генерируют код С и вставляют директивы #line.

import os.path

class StepSource(gdb.Command):
    def __init__(self):
        super().__init__(
            'ss',
            gdb.COMMAND_BREAKPOINTS,
            gdb.COMPLETE_NONE,
            False
        )
    def invoke(self, argument, from_tty):
        argv = gdb.string_to_argv(argument)
        if argv:
            if len(argv) > 1:
                gdb.write('Usage:\nns [source-name]]\n')
                return
            source = argv[0]
            full_path = False if os.path.basename(source) == source else True
        else:
            source = gdb.selected_frame().find_sal().symtab.fullname()
            full_path = True
        thread = gdb.inferiors()[0].threads()[0]
        while True:
            message = gdb.execute('next', to_string=True)
            if not thread.is_valid():
                break
            try:
                cur_source = gdb.selected_frame().find_sal().symtab.fullname()
                if not full_path:
                    cur_source = os.path.basename(cur_source)
            except:
                break
            else:
                if source == cur_source:
                    break
StepSource()

Известные ошибки

  1. t прерывать отладчик на SIGINT во время работы;
  2. изменил pass на break на исключение, так как не уверен, что он прав.

0
задан JTechnau 18 March 2019 в 16:05
поделиться

1 ответ

Этот код использует java.net.InterfaceAddress для проверки работоспособности интерфейсов. С этого момента тип соединения легко определяется с помощью метода .getDisplayName (). Я изменил код из https://examples.javacodegeeks.com/core-java/net/networkinterface/java-net-networkinterface-example/

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang.StringUtils;

public class networkConnectionTeller {

    public static boolean isNetworkRunningViaCellular() throws SocketException {
        String s = "";
        // NetworkInterface implements a static method that returns all the 
       //interfaces on the PC,
       // which we add on a list in order to iterate over them.
        ArrayList interfaces = 
        Collections.list(NetworkInterface.getNetworkInterfaces());

        s += ("Printing information about the available interfaces...\n");
        for (Object ifaceO : interfaces) {
            NetworkInterface iface = (NetworkInterface) ifaceO;
            // Due to the amount of the interfaces, we will only print info
            // about the interfaces that are actually online.
            if (iface.isUp() && 
           !StringUtils.containsIgnoreCase(iface.getDisplayName(), "loopback")) {  
            //Don`t want to see software loopback interfaces

                // Display name
                s += ("Interface name: " + iface.getDisplayName() + "\n");

                // Interface addresses of the network interface
                s += ("\tInterface addresses: ");
                for (InterfaceAddress addr : iface.getInterfaceAddresses()) {
                    s += ("\t\t" + addr.getAddress().toString() + "\n");
                }

                // MTU (Maximum Transmission Unit)
                s += ("\tMTU: " + iface.getMTU() + "\n");

                // Subinterfaces
                s += ("\tSubinterfaces: " + 
                Collections.list(iface.getSubInterfaces()) + "\n");

                // Check other information regarding the interface
                s += ("\tis loopback: " + iface.isLoopback() + "\n");
                s += ("\tis virtual: " + iface.isVirtual() + "\n");
                s += ("\tis point to point: " + iface.isPointToPoint() + "\n");
                System.out.println(s);

                if (iface.getDisplayName().contains("Broadband")) {
                    return true;
                }
            }
        }
        return false;
    }
}
0
ответ дан JTechnau 18 March 2019 в 16:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: