Update
Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
173
Program.cs
173
Program.cs
@@ -1,5 +1,6 @@
|
||||
using Newtonsoft.Json;
|
||||
using Renci.SshNet;
|
||||
using Spectre.Console;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -19,32 +20,32 @@ namespace domain_utility
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static readonly string regularDateTime = @"(\d+[.]\d+[.]\d+[ ]\d+[:]\d+[:]\d+)";
|
||||
static readonly string regularDateTime = @"(\d+[.]\d+[.]\d+[ ]\d+[:]\d+[:]\d+)";
|
||||
|
||||
private static readonly string[,] stringsToFind = new string[,] {
|
||||
static readonly string[,] stringsToFind = new string[,] {
|
||||
{ "Учетная запись активна", @"(Yes|No)", "Учетная запись работает: " },
|
||||
{ "Последний пароль задан", regularDateTime, "Когда был сменен пароль: " },
|
||||
{ "Действие пароля завершается", regularDateTime, "Когда нужно менять пароль (крайний срок): "},
|
||||
{ "Членство в глобальных группах", @"([*].+)", "Член групп:\t" }
|
||||
};
|
||||
|
||||
private static readonly string[,] computerNameRegex = new string[,] {
|
||||
static readonly string[,] computerNameRegex = new string[,] {
|
||||
{ "^[a-zA-Z]+\\d+$", "R54-630300" }, // THE01
|
||||
{ "^\\d{6,}[a-zA-Z]+\\d+$", "R54-" }, // 630300THE01
|
||||
{ "^[rR]\\d{2,}[-]\\d{6,}[a-zA-Z]+\\d+$", "" } // R54-630300THE01
|
||||
};
|
||||
|
||||
private static string domain_user = string.Empty;
|
||||
private static string domain_password = string.Empty;
|
||||
static string domain_user = string.Empty;
|
||||
static string domain_password = string.Empty;
|
||||
|
||||
private static bool _click_cancel = false;
|
||||
static bool _click_cancel = false;
|
||||
|
||||
private static bool IsStringContainIp(string ip)
|
||||
static bool IsStringContainIp(string ip)
|
||||
{
|
||||
return Regex.Match(ip, @"^(\d+[.]\d+[.]\d+[.]\d+)$").Success;
|
||||
}
|
||||
|
||||
private static string CheckComputerName(string pcName)
|
||||
static string CheckComputerName(string pcName)
|
||||
{
|
||||
for (int i = 0; i < computerNameRegex.GetLength(0); i++)
|
||||
if (Regex.Match(pcName, computerNameRegex[i, 0]).Success)
|
||||
@@ -52,7 +53,7 @@ namespace domain_utility
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static string InputData(string message, Action callback, bool withClear = true, bool withChecks = true)
|
||||
static string InputData(string message, Action callback, bool withClear = true, bool withChecks = true)
|
||||
{
|
||||
if (withClear)
|
||||
Console.Clear();
|
||||
@@ -76,7 +77,7 @@ namespace domain_utility
|
||||
return data;
|
||||
}
|
||||
|
||||
private static void BackToMenu(Action callback, bool withMessage = true)
|
||||
static void BackToMenu(Action callback, bool withMessage = true)
|
||||
{
|
||||
if (withMessage)
|
||||
Console.WriteLine("\nНажмите Enter чтобы продолжить, ESC чтобы вернуться на главную.");
|
||||
@@ -92,7 +93,7 @@ namespace domain_utility
|
||||
}
|
||||
}
|
||||
|
||||
private static void InputDomainCredentials()
|
||||
static void InputDomainCredentials()
|
||||
{
|
||||
if (domain_user == string.Empty)
|
||||
{
|
||||
@@ -127,7 +128,7 @@ namespace domain_utility
|
||||
domain_password = password;
|
||||
}
|
||||
|
||||
private static void ExecuteCommandViaSSH(string remote, string commandToExecute)
|
||||
static void ExecuteCommandViaSSH(string remote, string commandToExecute)
|
||||
{
|
||||
InputDomainCredentials();
|
||||
try
|
||||
@@ -146,7 +147,7 @@ namespace domain_utility
|
||||
}
|
||||
}
|
||||
|
||||
private static void ResetAdminPassword()
|
||||
static void ResetAdminPassword()
|
||||
{
|
||||
string machineNameAndUser = WindowsIdentity.GetCurrent().Name.ToString();
|
||||
string machineName = machineNameAndUser.Substring(0, machineNameAndUser.IndexOf('\\'));
|
||||
@@ -163,7 +164,7 @@ namespace domain_utility
|
||||
BackToMenu(Menu);
|
||||
}
|
||||
|
||||
private static string PingHost(string host)
|
||||
static string PingHost(string host)
|
||||
{
|
||||
string returnMessage = string.Empty;
|
||||
PingOptions pingOptions = new PingOptions() { DontFragment = true }; // TTL = 128
|
||||
@@ -211,15 +212,15 @@ namespace domain_utility
|
||||
return returnMessage;
|
||||
}
|
||||
|
||||
private static void PingClickCancel(object sender, ConsoleCancelEventArgs e)
|
||||
static void PingClickCancel(object sender, ConsoleCancelEventArgs e)
|
||||
{
|
||||
e.Cancel = true;
|
||||
_click_cancel = true;
|
||||
}
|
||||
|
||||
private static void StartPing()
|
||||
static void StartPing()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("ping компьютера\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
StartPing);
|
||||
|
||||
Console.WriteLine("Нажмите Ctrl + C чтобы остановить\n");
|
||||
@@ -255,9 +256,9 @@ namespace domain_utility
|
||||
BackToMenu(StartPing);
|
||||
}
|
||||
|
||||
private static void StartRDPConnection()
|
||||
static void StartRDPConnection()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("удаленный доступ к компьютеру\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
StartRDPConnection);
|
||||
|
||||
remote = $"/v:{remote} /f";
|
||||
@@ -270,9 +271,9 @@ namespace domain_utility
|
||||
BackToMenu(StartRDPConnection);
|
||||
}
|
||||
|
||||
private static void ShowDomainUserInfo()
|
||||
static void ShowDomainUserInfo()
|
||||
{
|
||||
string username = InputData("\nВведите имя пользователя (пр. 'lev.rusanov'): ", ShowDomainUserInfo, withChecks: false);
|
||||
string username = InputData("посмотреть информацию о пользователе\n\nВведите имя пользователя (пр. 'lev.rusanov'): ", ShowDomainUserInfo, withChecks: false);
|
||||
|
||||
username = $"user {username} /domain";
|
||||
|
||||
@@ -321,9 +322,9 @@ namespace domain_utility
|
||||
BackToMenu(ShowDomainUserInfo);
|
||||
}
|
||||
|
||||
private static void ShowComputerBootupTime()
|
||||
static void ShowComputerBootupTime()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("посмотреть дату последней загрузки компьютера\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
ShowComputerBootupTime);
|
||||
|
||||
ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", remote));
|
||||
@@ -345,9 +346,9 @@ namespace domain_utility
|
||||
BackToMenu(ShowComputerBootupTime);
|
||||
}
|
||||
|
||||
private static void RemoteRebootWindows()
|
||||
static void RemoteRebootWindows()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("удаленная перезагрузка компьютера\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
RemoteRebootWindows);
|
||||
|
||||
remote = $"/m \\\\{remote} /r /f /t 180 /c \"Через 3 минуты будет произведена перезагрузка ПК!\"";
|
||||
@@ -360,9 +361,9 @@ namespace domain_utility
|
||||
BackToMenu(RemoteRebootWindows);
|
||||
}
|
||||
|
||||
private static void OpenComputerCups()
|
||||
static void OpenComputerCups()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("открыть CUPS выбранного компьютера (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
OpenComputerCups);
|
||||
|
||||
Process.Start($"https://{remote}:631/printers");
|
||||
@@ -372,9 +373,9 @@ namespace domain_utility
|
||||
BackToMenu(OpenComputerCups);
|
||||
}
|
||||
|
||||
private static void ExecuteCustomCommandViaSSH()
|
||||
static void ExecuteCustomCommandViaSSH()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("выполнить команду через SSH (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
ExecuteCustomCommandViaSSH);
|
||||
|
||||
string commandToExecute = InputData("Введите команду для выполнения:", ExecuteCustomCommandViaSSH, withClear: false, withChecks: false);
|
||||
@@ -386,9 +387,9 @@ namespace domain_utility
|
||||
BackToMenu(ExecuteCustomCommandViaSSH);
|
||||
}
|
||||
|
||||
private static void FixConky()
|
||||
static void FixConky()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("изменить время обновления conky с 0.5 на 300 (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
FixConky);
|
||||
|
||||
ExecuteCommandViaSSH(remote, "sudo sed -i 's/update_interval = 0.5,/update_interval = 300,/' /etc/conky/conky.conf");
|
||||
@@ -398,9 +399,9 @@ namespace domain_utility
|
||||
BackToMenu(FixConky);
|
||||
}
|
||||
|
||||
private static void RemoteRebootLinux()
|
||||
static void RemoteRebootLinux()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("удаленная перезагрузка компьютера (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
RemoteRebootLinux);
|
||||
|
||||
ExecuteCommandViaSSH(remote, "sudo shutdown -r +3 \"Через 3 минуты будет произведена перезагрузка ПК!\"");
|
||||
@@ -410,9 +411,9 @@ namespace domain_utility
|
||||
BackToMenu(RemoteRebootLinux);
|
||||
}
|
||||
|
||||
private static void ShowLinuxComputerBootupTime()
|
||||
static void ShowLinuxComputerBootupTime()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("посмотреть время работы компьютера (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
ShowLinuxComputerBootupTime);
|
||||
|
||||
ExecuteCommandViaSSH(remote, "uptime");
|
||||
@@ -420,9 +421,9 @@ namespace domain_utility
|
||||
BackToMenu(ShowLinuxComputerBootupTime);
|
||||
}
|
||||
|
||||
private static void ChangeRemote802Password()
|
||||
static void ChangeRemote802Password()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04')\nall - для массового изменения",
|
||||
string remote = InputData("сменить пароль для 802.1x (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04')\nall - для массового изменения",
|
||||
ChangeRemote802Password, withChecks: false);
|
||||
remote = remote.Trim().ToLower();
|
||||
if (remote != "all")
|
||||
@@ -478,9 +479,9 @@ namespace domain_utility
|
||||
BackToMenu(ChangeRemote802Password);
|
||||
}
|
||||
|
||||
private static void DisableKdeWallet()
|
||||
static void DisableKdeWallet()
|
||||
{
|
||||
string remote = InputData("\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
string remote = InputData("отключить KDE кошелек (Linux)\n\nВведите IP адрес или имя компьютера (пр. 10.234.16.129, 'IT04', '630300IT04', 'R54-630300IT04'):",
|
||||
DisableKdeWallet);
|
||||
|
||||
ExecuteCommandViaSSH(remote, "sudo sed -i 's/^Exec=/#Exec=/' /usr/share/dbus-1/services/org.kde.kwalletd5.service");
|
||||
@@ -490,69 +491,57 @@ namespace domain_utility
|
||||
BackToMenu(DisableKdeWallet);
|
||||
}
|
||||
|
||||
static readonly string[] menuList = {
|
||||
//"630300 - сброс пароля локального администратора (только Windows)", // ResetAdminPassword
|
||||
"Выберите действие:",
|
||||
"1 - ping компьютера", // StartPing
|
||||
"2 - удаленно подключиться к компьютеру", // StartRDPConnection
|
||||
"\nWindows:",
|
||||
"3 - посмотреть информацию о пользователе", // ShowDomainUserInfo
|
||||
"4 - посмотреть дату последней загрузки компьютера", // ShowComputerBootupTime
|
||||
"5 - удаленная перезагрузка компьютера", // RemoteRebootWindows
|
||||
"\nLinux:",
|
||||
"6 - открыть CUPS выбранного компьютера", // OpenComputerCups
|
||||
"7 - выполнить команду через SSH", // ExecuteCustomCommandViaSSH
|
||||
"8 - изменить время обновления conky с 0.5 на 300", // FixConky
|
||||
"9 - удаленная перезагрузка компьютера", // RemoteRebootLinux
|
||||
"10 - посмотреть время работы компьютера", // ShowLinuxComputerBootupTime
|
||||
"11 - сменить пароль для 802.1x", // ChangeRemote802Password
|
||||
"12 - отключить KDE кошелек", // DisableKdeWallet
|
||||
};
|
||||
|
||||
private static void Menu()
|
||||
private class MenuClass
|
||||
{
|
||||
Console.Clear();
|
||||
foreach (string item in menuList) // for (int i = 0; i < menuList.Length; i++)
|
||||
Console.WriteLine(item); // Console.WriteLine(menuList[i]);
|
||||
int choice;
|
||||
Console.Write("\n> ");
|
||||
while (!int.TryParse(Console.ReadLine(), out choice))
|
||||
public string Name { get; }
|
||||
public Action Executor { get; }
|
||||
|
||||
public MenuClass(string _name, Action _executor)
|
||||
{
|
||||
Console.WriteLine("Введите цифру!");
|
||||
Console.Write("> ");
|
||||
}
|
||||
switch (choice)
|
||||
{
|
||||
case 1: StartPing(); break;
|
||||
case 2: StartRDPConnection(); break;
|
||||
case 3: ShowDomainUserInfo(); break;
|
||||
case 4: ShowComputerBootupTime(); break;
|
||||
case 5: RemoteRebootWindows(); break;
|
||||
case 6: OpenComputerCups(); break;
|
||||
case 7: ExecuteCustomCommandViaSSH(); break;
|
||||
case 8: FixConky(); break;
|
||||
case 9: RemoteRebootLinux(); break;
|
||||
case 10: ShowLinuxComputerBootupTime(); break;
|
||||
case 11: ChangeRemote802Password(); break;
|
||||
case 12: DisableKdeWallet(); break;
|
||||
case 630300: ResetAdminPassword(); break;
|
||||
default:
|
||||
{
|
||||
Menu();
|
||||
break;
|
||||
}
|
||||
Name = _name;
|
||||
Executor = _executor;
|
||||
}
|
||||
}
|
||||
|
||||
private class JsonFile
|
||||
static readonly MenuClass[] availableOptions = new[]
|
||||
{
|
||||
new MenuClass("ping компьютера", () => StartPing()),
|
||||
new MenuClass("удаленный доступ к компьютеру", () => StartRDPConnection()),
|
||||
new MenuClass("посмотреть информацию о пользователе", () => ShowDomainUserInfo()),
|
||||
new MenuClass("посмотреть дату последней загрузки компьютера", () => ShowComputerBootupTime()),
|
||||
new MenuClass("удаленная перезагрузка компьютера", () => RemoteRebootWindows()),
|
||||
new MenuClass("открыть CUPS выбранного компьютера (Linux)", () => OpenComputerCups()),
|
||||
new MenuClass("выполнить команду через SSH (Linux)", () => ExecuteCustomCommandViaSSH()),
|
||||
new MenuClass("изменить время обновления conky с 0.5 на 300 (Linux)", () => FixConky()),
|
||||
new MenuClass("удаленная перезагрузка компьютера (Linux)", () => RemoteRebootLinux()),
|
||||
new MenuClass("посмотреть время работы компьютера (Linux)", () => ShowLinuxComputerBootupTime()),
|
||||
new MenuClass("сменить пароль для 802.1x (Linux)", () => ChangeRemote802Password()),
|
||||
new MenuClass("отключить KDE кошелек (Linux)", () => DisableKdeWallet()),
|
||||
};
|
||||
|
||||
static void Menu()
|
||||
{
|
||||
Console.Clear();
|
||||
var choice = AnsiConsole.Prompt(
|
||||
new SelectionPrompt<MenuClass>()
|
||||
.Title("Выберите действие:")
|
||||
.PageSize(15)
|
||||
//.MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
|
||||
.UseConverter(i => i.Name)
|
||||
.AddChoices(availableOptions)
|
||||
);
|
||||
choice.Executor();
|
||||
}
|
||||
|
||||
class JsonFile
|
||||
{
|
||||
public int sleep_time = 0;
|
||||
public int restart_time = 0;
|
||||
public string message = "";
|
||||
public Dictionary<string, List<int>> everyday_reboot = null;
|
||||
public Dictionary<string, List<int>> arm_list = null;
|
||||
}
|
||||
|
||||
private static void StartRestartComputers()
|
||||
static void StartRestartComputers()
|
||||
{
|
||||
string path = Path.Combine(Environment.CurrentDirectory, "settings.json");
|
||||
if (!File.Exists(path))
|
||||
@@ -564,7 +553,7 @@ namespace domain_utility
|
||||
JsonFile data = JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(path));
|
||||
string remote, unitKey;
|
||||
int sleep_time = data.sleep_time;
|
||||
foreach (var unit in data.everyday_reboot)
|
||||
foreach (KeyValuePair<string, List<int>> unit in data.arm_list)
|
||||
{
|
||||
unitKey = unit.Key;
|
||||
foreach (int position in unit.Value)
|
||||
|
||||
Reference in New Issue
Block a user