diff --git a/.gitignore b/.gitignore index d75b79a..93621e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs bin/ obj/ +packages/ *.csproj.user diff --git a/Program.cs b/Program.cs index 44c9f7b..367a988 100644 --- a/Program.cs +++ b/Program.cs @@ -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: { diff --git a/domain_utility.csproj b/domain_utility.csproj index cdce015..bf02963 100644 --- a/domain_utility.csproj +++ b/domain_utility.csproj @@ -1,6 +1,7 @@  + Debug AnyCPU @@ -12,6 +13,8 @@ 512 true true + + AnyCPU @@ -33,10 +36,22 @@ 4 + + packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll + + + packages\SSH.NET.2024.1.0\lib\net462\Renci.SshNet.dll + + + packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + + packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + @@ -50,6 +65,35 @@ + + + + Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}. + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..42ea68e --- /dev/null +++ b/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file