nadav1372
New member
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IPScannerByNR
{
public partial class Form1 : Form
{
private string _ipAddress = "1.51.98";
private bool isRunning = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (isRunning)
{
MessageBox.Show("Operation already in progress.");
return;
}
isRunning = true;
button1.Enabled = false;
dataGridView1.Rows.Clear();
Task.Run(() => RunPingScript()).ContinueWith(t =>
{
this.Invoke((Action)(() =>
{
button1.Enabled = true;
isRunning = false;
}));
});
}
private void setIpButton_Click(object sender, EventArgs e)
{
string ipAddressInput = ipAddressTextBox.Text;
System.Net.IPAddress ipAddress;
if (string.IsNullOrEmpty(ipAddressInput) || !System.Net.IPAddress.TryParse(ipAddressInput, out ipAddress))
{
MessageBox.Show("Please Enter a Valid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_ipAddress = ipAddressInput;
dataGridView1.Rows.Clear();
}
private void RunPingScript()
{
string batchFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pingScript.bat");
string scriptContent = "@echo off\n" +
"setlocal enabledelayedexpansion\n" +
"for /L %%i in (2,1,254) do (\n" +
" set \"status=Unknown\"\n" +
" ping -4 -n 1 -f " + _ipAddress + ".%%i > tempPingResult.txt\n" +
" for /f \"tokens=* delims=\" %%a in (tempPingResult.txt) do (\n" +
" echo %%a | find /i \"Request timed out.\" > nul && set \"status=Request Timed out\"\n" +
" echo %%a | find /i \"Destination host unreachable.\" > nul && set \"status=Destination host unreachable\"\n" +
" echo %%a | find /i \"TTL=\" > nul && if \"!status!\" == \"Unknown\" set \"status=In Use\"\n" +
" )\n" +
" if \"" + _ipAddress + ".%%i\" == \"" + _ipAddress + ".1\" (\n" +
" set \"status=In Use\"\n" +
" )\n" +
" echo " + _ipAddress + ".%%i !status!\n" +
")\n" +
"del tempPingResult.txt";
File.WriteAllText(batchFilePath, scriptContent);
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c " + batchFilePath)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = processInfo;
process.OutputDataReceived += (sender, args) =>
{
if (args.Data != null)
{
this.BeginInvoke((Action)(() =>
{
if (!this.IsDisposed && this.IsHandleCreated)
{
UpdateDataGridView(args.Data);
}
}));
}
};
process.ErrorDataReceived += (sender, args) =>
{
if (args.Data != null)
{
Console.Error.WriteLine("Error: " + args.Data);
}
};
try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
Console.Error.WriteLine("Exception occurred: " + ex.Message);
}
}
}
private void UpdateDataGridView(string output)
{
string[] parts = output.Split(new[] { ' ' }, 2);
if (parts.Length > 1)
{
string ip = parts[0];
string status = parts[1].Trim();
if (ip == _ipAddress + ".1" && status == "Unknown")
{
status = "In Use";
}
int rowIndex = dataGridView1.Rows.Add(ip, status);
switch (status)
{
case "Request Timed out":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
break;
case "Destination host unreachable":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Green; // Use a different color if needed
break;
case "In Use":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
break;
default:
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Gray;
break;
}
Console.WriteLine("Added IP: " + ip + " Status: " + status);
}
else
{
Console.WriteLine("Output: " + output);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
string ipAddress = row.Cells["ipAddressColumn"].Value.ToString();
string status = row.Cells["statusColumn"].Value.ToString();
MessageBox.Show("IP Address: " + ipAddress + "\nStatus: " + status + " Cell Clicked");
}
}
}
}
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IPScannerByNR
{
public partial class Form1 : Form
{
private string _ipAddress = "1.51.98";
private bool isRunning = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (isRunning)
{
MessageBox.Show("Operation already in progress.");
return;
}
isRunning = true;
button1.Enabled = false;
dataGridView1.Rows.Clear();
Task.Run(() => RunPingScript()).ContinueWith(t =>
{
this.Invoke((Action)(() =>
{
button1.Enabled = true;
isRunning = false;
}));
});
}
private void setIpButton_Click(object sender, EventArgs e)
{
string ipAddressInput = ipAddressTextBox.Text;
System.Net.IPAddress ipAddress;
if (string.IsNullOrEmpty(ipAddressInput) || !System.Net.IPAddress.TryParse(ipAddressInput, out ipAddress))
{
MessageBox.Show("Please Enter a Valid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_ipAddress = ipAddressInput;
dataGridView1.Rows.Clear();
}
private void RunPingScript()
{
string batchFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pingScript.bat");
string scriptContent = "@echo off\n" +
"setlocal enabledelayedexpansion\n" +
"for /L %%i in (2,1,254) do (\n" +
" set \"status=Unknown\"\n" +
" ping -4 -n 1 -f " + _ipAddress + ".%%i > tempPingResult.txt\n" +
" for /f \"tokens=* delims=\" %%a in (tempPingResult.txt) do (\n" +
" echo %%a | find /i \"Request timed out.\" > nul && set \"status=Request Timed out\"\n" +
" echo %%a | find /i \"Destination host unreachable.\" > nul && set \"status=Destination host unreachable\"\n" +
" echo %%a | find /i \"TTL=\" > nul && if \"!status!\" == \"Unknown\" set \"status=In Use\"\n" +
" )\n" +
" if \"" + _ipAddress + ".%%i\" == \"" + _ipAddress + ".1\" (\n" +
" set \"status=In Use\"\n" +
" )\n" +
" echo " + _ipAddress + ".%%i !status!\n" +
")\n" +
"del tempPingResult.txt";
File.WriteAllText(batchFilePath, scriptContent);
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c " + batchFilePath)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = processInfo;
process.OutputDataReceived += (sender, args) =>
{
if (args.Data != null)
{
this.BeginInvoke((Action)(() =>
{
if (!this.IsDisposed && this.IsHandleCreated)
{
UpdateDataGridView(args.Data);
}
}));
}
};
process.ErrorDataReceived += (sender, args) =>
{
if (args.Data != null)
{
Console.Error.WriteLine("Error: " + args.Data);
}
};
try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
Console.Error.WriteLine("Exception occurred: " + ex.Message);
}
}
}
private void UpdateDataGridView(string output)
{
string[] parts = output.Split(new[] { ' ' }, 2);
if (parts.Length > 1)
{
string ip = parts[0];
string status = parts[1].Trim();
if (ip == _ipAddress + ".1" && status == "Unknown")
{
status = "In Use";
}
int rowIndex = dataGridView1.Rows.Add(ip, status);
switch (status)
{
case "Request Timed out":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
break;
case "Destination host unreachable":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Green; // Use a different color if needed
break;
case "In Use":
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
break;
default:
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Gray;
break;
}
Console.WriteLine("Added IP: " + ip + " Status: " + status);
}
else
{
Console.WriteLine("Output: " + output);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
string ipAddress = row.Cells["ipAddressColumn"].Value.ToString();
string status = row.Cells["statusColumn"].Value.ToString();
MessageBox.Show("IP Address: " + ipAddress + "\nStatus: " + status + " Cell Clicked");
}
}
}
}