- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[C#] Hướng dẫn viết ứng dụng check live SSH Server
Xin chào các bạn, bài viết hôm nay, mình sẽ hướng dẫn các bạn viết ứng dụng check live SSH Server (kiểm tra tình trạng con SSH còn sử dụng được hay không). Trong bài viết, mình sử dụng thư viện SSH.NET C#.
Vậy SSH là gì?
SSH hay còn được gọi là Secure Shell, là một giao thức điều khiển từ xa cho phép người dùng kiểm soát và chỉnh sửa server từ xa qua Internet. Dịch vụ được tạo ra nhằm thay thế cho trình Telnet vốn không có mã hóa và sử dụng kỹ thuật cryptographic để đảm bảo tất cả giao tiếp gửi tới và gửi từ server từ xa diễn ra trong tình trạng mã hóa. Nó cung cấp thuật toán để chứng thực người dùng từ xa, chuyển input từ client tới host, và relay kết quả trả về tới khách hàng.
Bạn nào thường chơi MMO (Make Money Online), thường thì hay sử dụng SSH để chạy kiếm traffic, hoặc FakeIP để kết nối server.
Ví dụ: Giờ mình muốn viết một ứng dụng lấy dữ liệu từ website, nhưng website này chặn IP đến từ Việt Nam, giờ mình làm sao để lấy được dữ liệu từ website đó.
Mình sẽ sử dụng một con server SSH để fake ip kết nối tới website đó.
Dưới đây là giao diện phần mềm kiểm tra SSH.
Trong bài viết này mình sử dụng thư viện SSH.NET C#, các bạn có thể download thư viện này về từ Nuget, hoặc có thể download source của mình bên dưới để tham khảo.
Dưới đây, là cấu trúc file text chứa thông tin SSH.
- Đầu tiên chúng ta cần tạo một class MySSH.cs với nội dung như sau:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Renci.SshNet;
using System.Net;
using System.IO;
namespace CheckLiveSSH
{
class MySSH
{
SshClient client;
ForwardedPortLocal fwPort;
public async Task checkSSH(string ip, string user, string pwd, string host, string timeout)
{
return await Task.Run(() =>
{
try
{
//uint port = (uint)new Random().Next(1080, 9999);
uint port = 1080;
ConnectionInfo ConnNfo = new ConnectionInfo(ip, 22, user, new AuthenticationMethod[]{
new PasswordAuthenticationMethod(user, pwd) });
fwPort = new ForwardedPortLocal("127.0.0.1", port, host, 80);
client = new SshClient(ConnNfo);
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(int.Parse(timeout));
client.Connect();
if (client.IsConnected)
{
client.AddForwardedPort(fwPort);
fwPort.Start();
if (fwPort.IsStarted)
{
closeAll();
return "live";
}
}
}
catch (Exception)
{
}
return "Die";
});
}
private void closeAll()
{
client.RemoveForwardedPort(fwPort);
fwPort.Dispose();
client.Disconnect();
client.Dispose();
}
}
}
- Tiếp theo, viết sự kiện cho winform import dữ liệu từ file text (*.txt) và kiểm tra dữ liệu từng con SSH
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CheckLiveSSH
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
DataTable table;
public DataTable Convert(string File, string TableName, string delimiter)
{
table = new DataTable();
table.Columns.Add("ipaddress", typeof(string));
table.Columns.Add("username", typeof(string));
table.Columns.Add("password", typeof(string));
table.Columns.Add("location", typeof(string));
table.Columns.Add("pos", typeof(string));
table.Columns.Add("city", typeof(string));
table.Columns.Add("status", typeof(string));
StreamReader s = new StreamReader(File);
string AllData = s.ReadToEnd();
string[] stringSeparators = new string[] { "
" };
string[] rows = AllData.Split(stringSeparators, StringSplitOptions.None);
foreach (string r in rows)
{
string[] items = r.Split(delimiter.ToCharArray());
table.Rows.Add(items);
}
return table;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text files (*.txt) | *.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
string url = ofd.FileName;
txt_url.Text = url;
DataTable dt = Convert(url, "test", "|");
gv_data.DataSource = dt;
}
}
int somay = 0;
private int _soMayChon;
private int _mayChamXong;
private int somayChamXong
{
set
{
lock (gv_data)
{
_mayChamXong = value;
if (_mayChamXong >= _soMayChon)
{
//progressBarControl1.BeginInvoke(new Action(() =>
//{
// Double percent = 100;
// progressBarControl1.EditValue = percent;
//}));
}
else
{
//progressBarControl1.BeginInvoke(new Action(() =>
//{
// Double percent = Convert.ToDouble(_mayChamXong) / Convert.ToDouble(_soMayChon) * 100.0;
// progressBarControl1.EditValue = percent;
// Load_DuLieu();
//}));
}
}
}
get
{
lock (gv_data)
{
return _mayChamXong;
}
}
}
private void TinhTrangKetNoi(int rowHandle, string msg)
{
gv_data.BeginInvoke((Action)(() =>
{
gv_data[6, rowHandle].Value = msg;
}));
}
private void btn_start_Click(object sender, EventArgs e)
{
_soMayChon = table.Rows.Count;
somayChamXong = 0;
//int i = 0;
var ipList = new List<KeyValuePair<int, string>>();
for (var r = 0; r <= table.Rows.Count - 1; r++)
{
somay++;
ipList.Add(new KeyValuePair<int, string>(r, table.Rows[r]["ipaddress"].ToString()));
}
if (somay > 0)
{
foreach(var ipInfo in ipList)
{
Task.Run(async () =>
{
TinhTrangKetNoi(ipInfo.Key,"Đang kiểm tra...");
MySSH ssh = new MySSH();
int r = ipInfo.Key;
string username = table.Rows[r]["username"].ToString();
string password = table.Rows[r]["password"].ToString();
string result = await ssh.checkSSH(ipInfo.Value, username, password, "google.com", "15");
TinhTrangKetNoi(ipInfo.Key, result);
});
};
}
}
}
}
Tools trên còn rất nhiều hạn chế, check live chậm như rùa bò... :), nếu bạn nào đã từng làm về ssh.net, có thể chạy Async các bạn có thể chia sẽ cho anh em để anh em tham khảo.
Bài viết tới, mình sẽ hướng dẫn các bạn sử dụng Http Request qua con SSH, mong các bạn đón đọc bài viết.
HAVE FUN :)