nadav1372
New Coder
C#:
namespace IPScannerByNR
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn ipAddressColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn statusColumn;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button stopButton;
private System.Windows.Forms.Button resumeButton;
private System.Windows.Forms.TextBox ipAddressTextBox;
private System.Windows.Forms.Button setIpButton;
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.ipAddressColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.statusColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.button1 = new System.Windows.Forms.Button();
this.stopButton = new System.Windows.Forms.Button();
this.resumeButton = new System.Windows.Forms.Button();
this.ipAddressTextBox = new System.Windows.Forms.TextBox();
this.setIpButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.ipAddressColumn,
this.statusColumn});
this.dataGridView1.Location = new System.Drawing.Point(16, 15);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.Size = new System.Drawing.Size(1035, 487);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// ipAddressColumn
//
this.ipAddressColumn.HeaderText = "IP Address";
this.ipAddressColumn.MinimumWidth = 6;
this.ipAddressColumn.Name = "ipAddressColumn";
this.ipAddressColumn.ReadOnly = true;
this.ipAddressColumn.Width = 125;
//
// statusColumn
//
this.statusColumn.HeaderText = "Status";
this.statusColumn.MinimumWidth = 6;
this.statusColumn.Name = "statusColumn";
this.statusColumn.ReadOnly = true;
this.statusColumn.Width = 125;
//
// button1
//
this.button1.Location = new System.Drawing.Point(830, 510);
this.button1.Margin = new System.Windows.Forms.Padding(4);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(100, 28);
this.button1.TabIndex = 1;
this.button1.Text = "Start Ping";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// stopButton
//
this.stopButton.Location = new System.Drawing.Point(951, 510);
this.stopButton.Margin = new System.Windows.Forms.Padding(4);
this.stopButton.Name = "stopButton";
this.stopButton.Size = new System.Drawing.Size(100, 28);
this.stopButton.TabIndex = 2;
this.stopButton.Text = "Stop";
this.stopButton.UseVisualStyleBackColor = true;
this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
this.stopButton.Enabled = false;
//
// resumeButton
//
this.resumeButton.Location = new System.Drawing.Point(951, 510);
this.resumeButton.Margin = new System.Windows.Forms.Padding(4);
this.resumeButton.Name = "resumeButton";
this.resumeButton.Size = new System.Drawing.Size(100, 28);
this.resumeButton.TabIndex = 3;
this.resumeButton.Text = "Resume";
this.resumeButton.UseVisualStyleBackColor = true;
this.resumeButton.Click += new System.EventHandler(this.resumeButton_Click);
this.resumeButton.Visible = false;
//
// ipAddressTextBox
//
this.ipAddressTextBox.Location = new System.Drawing.Point(16, 510);
this.ipAddressTextBox.Margin = new System.Windows.Forms.Padding(4);
this.ipAddressTextBox.Name = "ipAddressTextBox";
this.ipAddressTextBox.Size = new System.Drawing.Size(200, 22);
this.ipAddressTextBox.TabIndex = 4;
//
// setIpButton
//
this.setIpButton.Location = new System.Drawing.Point(224, 510);
this.setIpButton.Margin = new System.Windows.Forms.Padding(4);
this.setIpButton.Name = "setIpButton";
this.setIpButton.Size = new System.Drawing.Size(100, 28);
this.setIpButton.TabIndex = 5;
this.setIpButton.Text = "Set IP";
this.setIpButton.Click += new System.EventHandler(this.setIpButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1067, 554);
this.Controls.Add(this.setIpButton);
this.Controls.Add(this.ipAddressTextBox);
this.Controls.Add(this.resumeButton);
this.Controls.Add(this.stopButton);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1";
this.Text = "IP Scanner";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}
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;
private bool stopRequested = false;
private int lastIP = 2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (isRunning)
{
MessageBox.Show("Operation already in progress.");
return;
}
isRunning = true;
stopRequested = false;
button1.Enabled = false;
stopButton.Enabled = true;
dataGridView1.Rows.Clear();
Task.Run(() => RunPingScript(lastIP)).ContinueWith(t =>
{
this.Invoke((Action)(() =>
{
button1.Enabled = true;
stopButton.Enabled = false;
isRunning = false;
}));
});
}
private void stopButton_Click(object sender, EventArgs e)
{
stopRequested = true;
stopButton.Enabled = false;
resumeButton.Visible = true;
}
private void resumeButton_Click(object sender, EventArgs e)
{
if (isRunning)
{
MessageBox.Show("Operation already in progress.");
return;
}
isRunning = true;
stopRequested = false;
resumeButton.Visible = false;
stopButton.Enabled = true;
Task.Run(() => RunPingScript(lastIP)).ContinueWith(t =>
{
this.Invoke((Action)(() =>
{
button1.Enabled = true;
stopButton.Enabled = false;
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(int startIP)
{
string batchFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pingScript.bat");
for (int i = startIP; i <= 254; i++)
{
if (stopRequested)
{
lastIP = i;
return;
}
string scriptContent = "@Echo off\n" +
"setlocal enabledelayedexpansion\n" +
"set \"status=In Use\"\n" +
"ping -4 -n 1 -w 3000 " + _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 && set \"status=In Use\"\n" +
")\n" +
"echo " + _ipAddress + "." + i + " !status!\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");
}
}
}
}
Last edited by a moderator: