2021年9月6日 星期一

Kotlin - PingTool - To check Internet stability 檢測網路穩定性

 #前言

之前在公司,需要在場外測試網路的穩定度,是否會掉封包之類的等等,於是我就寫了一個
1.在時間內一直Ping
2.時間結束,冷卻幾秒後再繼續Ping
3.每Ping完之後都會將結果寫入txt檔

 #程式流程

輸入

PingAddress(IP Address)

Ping Time(How long)

WaitTime(Cool Down Time)

結果

Ping Loop : 1 
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=30ms TTL=111
Reply from 8.8.8.8: bytes=32 time=92ms TTL=111
Reply from 8.8.8.8: bytes=32 time=50ms TTL=111
Reply from 8.8.8.8: bytes=32 time=38ms TTL=111
>>>>>>>>>>Ping Tools<<<<<<<<<<
PingAddress(8.8.8.8) :
Ping Seconds(120) :
After Ping Wait Time(30) :
------------------Settings------------------
Start Ping -> 8.8.8.8
PingAddress -> 8.8.8.8
Seconds -> 120
WaitTimes -> 30
FileName -> pingResult_09_06_16-22-54.txt
------------------Settings------------------


Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=30ms TTL=111
Reply from 8.8.8.8: bytes=32 time=92ms TTL=111
Reply from 8.8.8.8: bytes=32 time=50ms TTL=111
Reply from 8.8.8.8: bytes=32 time=38ms TTL=111


#程式碼

import sun.misc.Signal
import java.io.BufferedReader
import java.io.File
import java.io.FileWriter
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.*


const val TIMER_DELAY = 1000L
const val TIMER_PERIOD = 1000L
var loop = 1

val runtime = Runtime.getRuntime()
lateinit var process : Process

var file = File("./pingResult_${getDateString()}.txt")

val runnable = Runnable {
val command = "ping $pingAddress -t "
runSystemCommand(command)
}
var pingAddress = "8.8.8.8"
var pingSeconds = 120
var pingWaitTime = 30
var counter = 0
var wait = 0
var thread = Thread(runnable)
val timer = Timer()
val timerTask = object : TimerTask() {
override fun run() {
counter++
if (counter >= pingSeconds) {
if (process.isAlive) {
thread.interrupt()
process.destroyForcibly()
println(">>> $pingSeconds Ping Finish <<<")
}
wait++
}
if (wait > pingWaitTime) {
resetCounters()
thread = Thread(runnable).apply { start() }
}

}


}

fun main(args: Array<String>) {
PingIntit()
}

private fun PingIntit() {
startConsole()
thread.start()
timer.schedule(timerTask, TIMER_DELAY, TIMER_PERIOD)
}
//User Input
private fun startConsole() {
println(">>>>>>>>>>Ping Tools<<<<<<<<<<")
print("PingAddress(8.8.8.8) : ")
var inputPingAddress = readLine().toString()
if (inputPingAddress.isNotEmpty()){
pingAddress = inputPingAddress
}
print("Ping Seconds(120) : ")
var inputSeconds = readLine().toString()
if (inputSeconds.isNotEmpty()){
pingSeconds = inputSeconds.toInt()
}
print("After Ping Wait Time(30) :")
val inputWaitTime = readLine().toString()
if (inputWaitTime.isNotEmpty()){
pingWaitTime = inputWaitTime.toInt()
}
val stringBuffer = StringBuffer()
stringBuffer.apply {
append("------------------Settings------------------ \n")
append("Start Ping -> $pingAddress \n")
append("PingAddress -> $pingAddress \n")
append("Seconds -> $pingSeconds \n")
append("WaitTimes -> $pingWaitTime \n")
append("FileName -> ${file.name} \n")
append("------------------Settings------------------ \n")

}
println(stringBuffer.toString())
val fileWriter = FileWriter(file,true)
fileWriter.write(stringBuffer.toString())
fileWriter.flush()
}
//Run command
fun runSystemCommand(command: String?) {
try {
process = runtime.exec(command)
val inputStream = BufferedReader(
InputStreamReader(process.inputStream)
)
var s: String? = ""
// reading output stream of the command
val fileWriter = FileWriter(file,true)
fileWriter.write("Ping Loop : $loop ")
fileWriter.flush()
while (inputStream.readLine().also { s = it } != null) {
fileWriter.write(s+"\n")
fileWriter.flush()
println(s)
}
loop++
fileWriter.close()

} catch (e: Exception) {
e.printStackTrace()
}
}
//Get time
fun getDateString() : String {
val date = System.currentTimeMillis()
val pattern = "MM_dd_HH-mm-ss"
val simpleDateFormat = SimpleDateFormat(pattern)
return simpleDateFormat.format(Date(date))
}
//Reset
fun resetCounters() {
wait = 0
counter = 0
}

#GitHub



沒有留言:

張貼留言