[轉] How To: ICMP Ping in Java (JDK 1.5 and above)

Programatically using ICMP Ping is a great way to establish that a server is up and running. Previously you couldn’t do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO.


String host = "172.16.0.2"
int timeOut = 3000; // I recommend 3 seconds at least
boolean status = InetAddress.getByName(host).isReachable(timeOut)

status is true if the machine is reachable by ping; false otherwise. Best effort is made to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. However on Windows you can get ICMP Ping without any issues whatsoever.

PS: Linux 版必須該 JVM 使用者有 root 權限發 ICMP

廣告

查看service的port

1.列出service一覽

sudo nmap -sU -sT localhost

2.由name查process id

ps aux | grep <process name>

3.由port查process

sudo netstat -anp | grep <port #>
sudo netstat -tulpn | grep <port #> –>只看tcp listen

 

Linux Network 用 CLI 設定

sudo gedit /etc/wpa_supplicant.conf

network={
key_mgmt=IEEE8021X
eap=TTLS MD5
identity="myloginname"
anonymous_identity="myloginname"
password="mypassword"
phase1="auth=MD5″
phase2="auth=PAP password=mypassword"
eapol_flags=0
}

https://help.ubuntu.com/community/Network802.1xAuthentication

Fedora上的網路設定

基本查詢指令

查詢網路卡資訊:ifconfig -a
查詢防火牆資訊:iptables -L -n
查詢路由:route -n


無線網路

搜尋無線網路:
iwlist ethx scan
ethx 就是網路卡代號
假設我的無限網路是使用eth1:iwlist eth1 scan

開始編寫我的設定檔
vi /etc/sysconfig/network-scripts/ifcfg-eth1

如果是使用dhcp
那ifcfg-eth1裡面就需要有
TYPE=Wireless
DEVICE=eth1
BOOTPROTO=dhcp
ONBOOT=no <–是否一開機就連線
ESSID=xxxx <–無線網路的名稱

如果是使用static
那ifcfg-eth1裡面的選項就變成
TYPE=Wireless
DEVICE=eth1
BOOTPROTO=static
NETMASK=255.255.255.0 <–子網路遮罩
IPADDR=192.168.1.13 <–ip位址
NETWORK=192.168.1.0 <–網域
BROADCAST=192.168.1.255 <–廣播位址
GATEWAY=192.168.22.10 <–預設路由
ONBOOT=no
ESSID=xxxx
KEY=xxxxxxxx<–密碼


有線網路

編寫設定檔
vi /etc/sysconfig/network-scripts/ifcfg-eth0

如果是使用dhcp
ifcfg-eth0裡面的選項就會有
TYPE=Ethernet
DEVICE=eth0
HWADDR=00:40:D0:13:c5:d8 <–網路卡位址
BOOTPROTO=dhcp
NBOOT=no
USERCTL=no <–是否允許使用者(非管理者)控制

如果是使用static
TYPE=Ethernet
DEVICE=eth0
BOOTPROTO=static
NETMASK=255.255.255.0 <–子網路遮罩
IPADDR=192.168.1.13 <—ip位址
NETWORK=192.168.1.0 <–網域
BROADCAST=192.168.1.255 <–廣播位址
GATEWAY=192.168.22.10 <–使用撥接上網的就不要設定gateway
ONBOOT=no
ESSID=xxxx
KEY=xxxxxxxx <–密碼
其實有線和無線網路差不多
只要TYPE寫清楚是wireless或是Ethernet就ok了
另外設定好後

啟動網路
ifup ethx <–看要啟動哪一種網路

關閉網路
ifdown ethx