Created
October 28, 2022 10:53
-
-
Save stefa168/60dcb3005e2b265c96faff008d39acdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress; | |
import java.net.URL; | |
import java.util.Scanner; | |
import java.io.IOException; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.StringSelection; | |
import java.awt.datatransfer.Clipboard; | |
public class IndirizzoIP { | |
public static void main(String[] args) { | |
System.out.println("\nUn saluto da Stefano!\nDi seguito sono visualizzati IP nella rete locale e globale.\n"); | |
String extIP = ""; | |
InetAddress i; | |
try { | |
i = InetAddress.getLocalHost(); | |
System.out.println("Nome Computer/IP Locale: " + i); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
URL url = new URL("http://checkip.amazonaws.com/"); | |
Scanner s = new Scanner(url.openStream()); | |
extIP = s.next(); | |
System.out.println("\nIP Esterno: " + extIP); | |
} catch (IOException ex) { | |
// there was some connection problem, or the file did not exist on the server, | |
// or your URL was not in the right format. | |
// think about what to do now, and put it here. | |
ex.printStackTrace(); // for now, simply output it. | |
} | |
System.out.println("\nVersione 2.1.2, compilata il 25-11-17 alle 09:51"); | |
System.out.println("Se vuoi copiare l'IP, scrivi C (+ INVIO), altrimenti premi solo INVIO."); | |
Scanner sc = new Scanner(System.in); | |
String ch = sc.nextLine(); | |
if(ch.length() != 0) | |
if(ch.charAt(0) == 'c' || ch.charAt(0) == 'C') { | |
StringSelection selection = new StringSelection(extIP); | |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
clipboard.setContents(selection, selection); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment