Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2024-11-01 15:13:35 +07:00
parent 19964226bb
commit 22452c057b
4 changed files with 148 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ using System.Threading;
using System.Net;
using System.DirectoryServices;
using System.Security.Principal;
using Renci.SshNet;
namespace domain_utility
{
@@ -30,14 +31,15 @@ namespace domain_utility
{ "^[rR]\\d*[-]\\d+[a-zA-Z]+\\d+$", "" } // R54-630300THE01
};
private static string InputRemoteComputer(string str, Action callback)
private static string InputData(string str, Action callback, bool withClear = true)
{
Console.Clear();
if (withClear)
Console.Clear();
Console.WriteLine(str);
Console.Write("> ");
string remote = Console.ReadLine().Trim();
if (remote == string.Empty || remote.Length == 0)
InputRemoteComputer(str, callback);
InputData(str, callback);
Console.WriteLine();
return remote;
}
@@ -136,15 +138,15 @@ namespace domain_utility
private static void ShowDomainUserInfo()
{
string username = InputRemoteComputer("\nВведите имя пользователя (пр. 'lev.rusanov'): ", ShowDomainUserInfo);
string username = InputData("\nВведите имя пользователя (пр. 'lev.rusanov'): ", ShowDomainUserInfo);
username = $"user {username} /domain";
ProcessStartInfo procStartInfo = new ProcessStartInfo("net", username)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
UseShellExecute = false
};
Process proc = new Process { StartInfo = procStartInfo };
proc.Start();
@@ -188,7 +190,7 @@ namespace domain_utility
private static void ShowComputerBootupTime()
{
string remote = InputRemoteComputer("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'): ",
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
ShowComputerBootupTime);
if (!IsStringContainIp(remote))
@@ -224,7 +226,7 @@ namespace domain_utility
private static void StartPing()
{
string remote = InputRemoteComputer("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'): ",
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
StartPing);
if (!IsStringContainIp(remote))
@@ -266,7 +268,7 @@ namespace domain_utility
private static void OpenComputerCups()
{
string remote = InputRemoteComputer("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'): ",
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
OpenComputerCups);
if (!IsStringContainIp(remote))
@@ -287,7 +289,7 @@ namespace domain_utility
private static void StartRDPConnection()
{
string remote = InputRemoteComputer("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'): ",
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
StartRDPConnection);
if (!IsStringContainIp(remote))
@@ -310,17 +312,97 @@ namespace domain_utility
BackToMenu(StartRDPConnection);
}
private static void ExecuteCommandViaSSH()
{
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
ExecuteCommandViaSSH);
if (!IsStringContainIp(remote))
{
remote = СheckComputerName(remote);
if (remote == string.Empty)
{
Console.WriteLine("Имя компьютера или IP-адрес не распознаны! Попробуйте еще раз.");
BackToMenu(ExecuteCommandViaSSH);
return;
}
}
Console.WriteLine("Введите пароль от СВОЕЙ учетной записи:");
Console.Write("> ");
string password = string.Empty;
while (true)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
break;
if (key.Key == ConsoleKey.Backspace && password.Length > 0)
password = password.Substring(0, password.Length - 1);
else if (key.Key != ConsoleKey.Backspace)
password += key.KeyChar;
}
Console.Write("\n\n");
string commandToExecute = InputData("Введите команду для выполнения:", ExecuteCommandViaSSH, false);
string machineNameAndUser = WindowsIdentity.GetCurrent().Name.ToString();
int indexOfUserName = machineNameAndUser.IndexOf('\\') + 1;
string machineUser = machineNameAndUser.Substring(indexOfUserName, machineNameAndUser.Length - indexOfUserName).ToLower();
using (var client = new SshClient(remote, machineUser, password))
{
client.Connect();
// "sudo sed -i 's/update_interval = 0.5,/update_interval = 300,/' /etc/conky/conky.conf"
SshCommand command = client.RunCommand(commandToExecute);
Console.WriteLine(command.Result);
client.Disconnect();
}
Console.WriteLine("Вроде что-то произошло, но это не точно");
BackToMenu(ExecuteCommandViaSSH);
}
private static void RemoteRebootWindows()
{
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
RemoteRebootWindows);
if (!IsStringContainIp(remote))
{
remote = СheckComputerName(remote);
if (remote == string.Empty)
{
Console.WriteLine("Имя компьютера или IP-адрес не распознаны! Попробуйте еще раз.");
BackToMenu(RemoteRebootWindows);
return;
}
}
remote = $"/m \\\\{remote} /r /f /t 180 /c 'Через 3 минуты будет произведена перезагрузка ПК!'";
Process proc = new Process
{
StartInfo = new ProcessStartInfo("shutdown", remote)
};
proc.Start();
Console.WriteLine("Команда перезагрузки успешно отправлена!");
BackToMenu(RemoteRebootWindows);
}
private static void Menu()
{
Console.Clear();
string[] menuList = {
//"4 - сброс пароля локального администратора (только Windows)",
"Выберите действие:",
"1 - посмотреть информацию о пользователе (только Windows)",
"2 - посмотреть дату последней загрузки компьютера (только Windows)",
"3 - ping компьютера",
//"4 - сброс пароля локального администратора (только Windows)",
"4 - открыть CUPS выбранного компьютера (только Linux)",
"5 - удаленно подключиться к компьютеру (только Windows -> Windows, Windows -> Linux)"
"5 - удаленно подключиться к компьютеру (Windows -> Windows, Windows -> Linux)",
"6 - выполнить команду удаленно (только Windows -> Linux)",
"7 - удаленная перезагрузка компьютера (только Windows)",
};
for (int i = 0; i < menuList.Length; i++)
Console.WriteLine(menuList[i]);
@@ -334,6 +416,8 @@ namespace domain_utility
case 3: StartPing(); break;
case 4: OpenComputerCups(); break;
case 5: StartRDPConnection(); break;
case 6: ExecuteCommandViaSSH(); break;
case 7: RemoteRebootWindows(); break;
case 51422415: ResetAdminPassword(); break;
default:
{