Skip to content

Commit faf84c9

Browse files
committed
base functionality
0 parents  commit faf84c9

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

PortCheck.ps1

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Script to parallel check TCP ports, Ping tests, and DNS resolution based on a CSV file containing the name, target, test type (TCP/Ping/DNS), and expected result
2+
3+
param (
4+
[string]$CsvFilePath
5+
)
6+
7+
# Check if the CSV file exists
8+
if (-not (Test-Path $CsvFilePath)) {
9+
Write-Host "CSV file not found: $CsvFilePath" -ForegroundColor Red
10+
exit 1
11+
}
12+
13+
# Import the CSV file
14+
$tests = Import-Csv -Path $CsvFilePath -Header Name, Target, TestType, ExpectedResult
15+
16+
# List for parallel jobs
17+
$jobs = @()
18+
19+
# Start the tests in parallel
20+
foreach ($test in $tests) {
21+
# skip first row if it contains headers
22+
if ($test.Name -eq 'Name' -and $test.Target -eq 'Target') {
23+
continue
24+
}
25+
26+
$name = $test.Name
27+
$target = $test.Target
28+
$testType = $test.TestType
29+
$expectedResult = [int]$test.ExpectedResult
30+
31+
# Create a job for each test
32+
$jobs += Start-Job -ScriptBlock {
33+
param ($name, $target, $testType, $expectedResult)
34+
35+
# Function to test a TCP port with a 5-second timeout
36+
function Test-TcpPort {
37+
param (
38+
[string]$target,
39+
[string]$port, # Accept port as string and convert later
40+
[int]$expected
41+
)
42+
43+
try {
44+
# Convert port to integer
45+
$intPort = [int]$port
46+
47+
$tcpClient = New-Object System.Net.Sockets.TcpClient
48+
$tcpClient.ReceiveTimeout = 5000 # 5-second timeout for receive
49+
$tcpClient.SendTimeout = 5000 # 5-second timeout for send
50+
51+
# Asynchronous connect with manual timeout
52+
$asyncResult = $tcpClient.BeginConnect($target, $intPort, $null, $null)
53+
$waitHandle = $asyncResult.AsyncWaitHandle
54+
55+
if ($waitHandle.WaitOne(5000)) {
56+
# 5-second timeout for connection
57+
$tcpClient.EndConnect($asyncResult)
58+
if ($tcpClient.Connected) {
59+
$tcpClient.Close()
60+
if ($expected -eq 1) {
61+
Write-Host "Test successful: $name ($target) on Port $intPort is reachable" -ForegroundColor Green
62+
}
63+
else {
64+
Write-Host "Test failed: $name ($target) on Port $intPort should not be reachable" -ForegroundColor Red
65+
}
66+
}
67+
}
68+
else {
69+
$tcpClient.Close()
70+
throw "Connection timed out"
71+
}
72+
}
73+
catch {
74+
# Connection failed
75+
if ($expected -eq 0) {
76+
Write-Host "Test successful: $name ($target) on Port $intPort is not reachable (expected)" -ForegroundColor Green
77+
}
78+
else {
79+
Write-Host "Test failed: $name ($target) on Port $intPort is not reachable" -ForegroundColor Red
80+
}
81+
}
82+
}
83+
84+
# Function to test Ping
85+
function Test-Ping {
86+
param (
87+
[string]$target,
88+
[int]$expected
89+
)
90+
91+
$ping = Test-Connection -ComputerName $target -Count 1 -Quiet
92+
93+
if ($ping -eq $true) {
94+
if ($expected -eq 1) {
95+
Write-Host "Test successful: $name ($target) is reachable with ping" -ForegroundColor Green
96+
}
97+
else {
98+
Write-Host "Test failed: $name ($target) should not be reachable with ping" -ForegroundColor Red
99+
}
100+
}
101+
else {
102+
if ($expected -eq 0) {
103+
Write-Host "Test successful: $name ($target) is not reachable with ping (expected)" -ForegroundColor Green
104+
}
105+
else {
106+
Write-Host "Test failed: $name ($target) is not reachable with ping" -ForegroundColor Red
107+
}
108+
}
109+
}
110+
111+
# Function to test DNS resolution
112+
function Test-DNS {
113+
param (
114+
[string]$dnsServer,
115+
[string]$hostname,
116+
[int]$expected
117+
)
118+
119+
try {
120+
$dnsResult = Resolve-DnsName -Name $hostname -Server $dnsServer -ErrorAction Stop
121+
if ($dnsResult) {
122+
if ($expected -eq 1) {
123+
Write-Host "Test successful: $hostname resolved by DNS server $dnsServer with IP $($dnsResult.IPAddress)" -ForegroundColor Green
124+
}
125+
else {
126+
Write-Host "Test failed: $hostname should not be resolved by DNS server $dnsServer, but got IP $($dnsResult.IPAddress)" -ForegroundColor Red
127+
}
128+
}
129+
}
130+
catch {
131+
if ($expected -eq 0) {
132+
Write-Host "Test successful: $hostname not resolved by DNS server $dnsServer (expected)" -ForegroundColor Green
133+
}
134+
else {
135+
Write-Host "Test failed: $hostname not resolved by DNS server $dnsServer" -ForegroundColor Red
136+
}
137+
}
138+
}
139+
140+
# Execute the test logic
141+
if ($testType -match '^\d+$') {
142+
# TCP port test if testType is a number
143+
Test-TcpPort -target $target -port $testType -expected $expectedResult
144+
}
145+
elseif ($testType -eq 'Ping') {
146+
# Ping test
147+
Test-Ping -target $target -expected $expectedResult
148+
}
149+
elseif ($testType -eq 'DNS') {
150+
# DNS resolution test
151+
Test-DNS -dnsServer $target -hostname 'google.com' -expected $expectedResult
152+
}
153+
else {
154+
Write-Host "Unknown test type: $testType" -ForegroundColor Yellow
155+
}
156+
} -ArgumentList $name, $target, $testType, $expectedResult
157+
}
158+
159+
# Wait for all jobs to finish
160+
$jobs | ForEach-Object {
161+
Receive-Job -Job $_ -Wait | Out-Null # Suppress True/False output
162+
Remove-Job -Job $_
163+
}
164+
165+
Write-Host "All tests completed." -ForegroundColor Cyan

example.csv

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Name,Target,TestType,ExpectedResult
2+
Cloudflare,1.1.1.1,Ping,1
3+
Cloudflare,1.1.1.1,DNS,1
4+
Google,1.1.1.1,Ping,1
5+
Google,1.1.1.1,DNS,1
6+
Google Website,google.com,443,1
7+
YouTube Website,youtube.com,443,1

readme.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Network Testing Script
2+
3+
This PowerShell script is designed to perform parallel network tests based on a CSV configuration file. It allows network administrators to check the availability of TCP ports, perform ping tests, and resolve DNS names against specified servers. The results are color-coded for quick identification of successes and failures.
4+
5+
## Features
6+
7+
- **TCP Port Testing**: Checks if a TCP port is open on a specified target with a custom timeout.
8+
- **Ping Testing**: Verifies if a target is reachable via ping.
9+
- **DNS Resolution Testing**: Tests if a DNS server can resolve a specified hostname and displays the resolved IP address.
10+
- **Parallel Execution**: Tests are run in parallel to optimize performance and reduce execution time.
11+
- **CSV Configuration**: Tests are configured through a CSV file, making it easy to adjust and extend the test cases.
12+
13+
## Usage
14+
15+
### Script Parameters
16+
17+
- `-CsvFilePath`: Path to the CSV file containing the test configuration.
18+
19+
### CSV File Format
20+
21+
The CSV file should have the following columns:
22+
23+
- `Name`: A descriptive name for the test.
24+
- `Target`: The target IP address or DNS server.
25+
- `TestType`: The type of test to perform (`TCP`, `Ping`, or `DNS`).
26+
- `ExpectedResult`: Expected result of the test (`1` for success, `0` for failure).
27+
28+
Example CSV file:
29+
30+
```csv
31+
Cloudflare,1.1.1.1,Ping,1
32+
Cloudflare,1.1.1.1,DNS,1
33+
Google,1.1.1.1,Ping,1
34+
Google,1.1.1.1,DNS,1
35+
Google Website,google.com,443,1
36+
YouTube Website,youtube.com,443,1
37+
```
38+
39+
## Running the Script
40+
41+
Run the script in PowerShell by providing the path to the CSV file:
42+
43+
```powershell
44+
.\PortCheck.ps1 -CsvFilePath .\example.csv
45+
```
46+
47+
## Output
48+
49+
The script provides color-coded output indicating the success or failure of each test:
50+
51+
- Green: Test successful
52+
- Red: Test failed
53+
- Yellow: Unknown test type
54+
55+
Example output:
56+
57+
```
58+
.\PortCheck.ps1 -CsvFilePath example.csv
59+
60+
Test successful: Cloudflare (1.1.1.1) is reachable with ping
61+
Test successful: google.com resolved by DNS server 1.1.1.1 with IP 2a00:1450:401b:808::200e 142.251.143.78
62+
Test successful: Google (1.1.1.1) is reachable with ping
63+
Test successful: google.com resolved by DNS server 1.1.1.1 with IP 2a00:1450:401b:808::200e 216.58.208.206
64+
Test successful: Google Website (google.com) on Port 443 is reachable
65+
Test successful: YouTube Website (youtube.com) on Port 443 is reachable
66+
```
67+
68+
## Contributing
69+
70+
If you have suggestions or improvements, feel free to submit a pull request or open an issue.

0 commit comments

Comments
 (0)