diff --git a/README.md b/README.md index f14022f9..f75f73df 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Add features: * *~Check integrity of application~* Fix: +* ```swsh --show``` not working. Issue #8 * *~Make ```cd ..``` work~. Issue #2* * *~A connection attempt failed while using password. Issue #1~* ### Note: Public release will be made after beta-2.0 is fully tested. Current release: beta-1.0. diff --git a/SWSH/Program.cs b/SWSH/Program.cs index 00e7f092..50730aaa 100644 --- a/SWSH/Program.cs +++ b/SWSH/Program.cs @@ -21,7 +21,7 @@ namespace SWSH { class Program { - public const string _version = "1.1"; + public const string _version = "1.2"; public static string _command = "", _codename = "beta", _mainDirectory = "swsh-data/", _workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); static void Main(string[] args) @@ -47,192 +47,16 @@ private static void __start() if (_command == "--version" || _command == "-v") __version(); else if (_command.StartsWith("--add") || _command.StartsWith("-a")) __addConnection(); else if (_command.StartsWith("--help") || _command.StartsWith("-h")) __interactiveHelp(); - else if (_command.StartsWith("--connect") || _command.StartsWith("-c")) - { - #region SSH Control - ConnectionInfo ccinfo; - string nickname = (_command.StartsWith("--connect")) ? _command.Remove(0, 10) : _command.Remove(0, 3); - if (File.Exists(_mainDirectory + nickname + ".swsh")) - { - if (File.ReadAllLines(_mainDirectory + nickname + ".swsh")[0] == "-password") - { - Console.Write("Password for {0}: ", nickname); - ccinfo = __CreateConnectionInfoPassword(nickname, __getCommand()); - } - else ccinfo = __CreateConnectionInfoKey(nickname); - if (ccinfo != null) - { - Console.Write("Waiting for response from {0}@{1}...\n", ccinfo.Username, ccinfo.Host); - using (var ssh = new SshClient(ccinfo)) - { - ssh.Connect(); - __color("Connected to " + ccinfo.Username + "@" + ccinfo.Host + "...\n", ConsoleColor.Green); - string pwd = " ", home = ""; - home = pwd = ssh.CreateCommand("echo $HOME").Execute(); - while (true) - { - pwd = Regex.Replace(ssh.CreateCommand("cd " + pwd + "; pwd").Execute(), @"\t|\n|\r", ""); - if (pwd == Regex.Replace(home, @"\t|\n|\r", "")) pwd = "~"; - __color(pwd, ConsoleColor.Green); - Console.Write(":/ $ "); - _command = __getCommand(); - if (_command == "exit") - break; - else if (_command.StartsWith("cd")) - { - _command = _command.Remove(0, 3); - if (_command.StartsWith("/")) pwd = _command; - else if (_command.StartsWith("./")) pwd += "/" + _command.Remove(0, 2); - else if (_command.StartsWith("..")) pwd = Regex.Replace(ssh.CreateCommand("cd " + pwd + "; dirname $(pwd)").Execute(), @"\t|\n|\r", ""); - else if (_command.Trim() == String.Empty) pwd = "~"; - else pwd += "/" + _command; - } - else if (_command == "clear") Console.Clear(); - else - { - var result = ssh.CreateCommand("cd " + pwd + "; " + _command).Execute(); - Console.Write(result); - } - } - ssh.Disconnect(); - } - __color("Connection to " + ccinfo.Username + "@" + ccinfo.Host + ", closed.\n", ConsoleColor.Yellow); - } - else break; - } - else - { - __color("ERROR: ", ConsoleColor.Red); - Console.Write("SWSH -> {0} -> nickname does not exists\n", nickname); - } - #endregion - } - else if (_command.StartsWith("--show")) - { - - } - else if (_command.StartsWith("--delete")) - { - try - { - if (File.Exists(__getNickname(_command.Replace("--delete", "").Trim()))) - { - __color("Are you sure you want to delete this nickname? (y/n): ", ConsoleColor.Red); - var ans = __getCommand().ToUpper(); - if (ans == "Y") - { - Console.Write("Type the nickname to confirm: "); - var name = __getCommand(); - if (name != _command.Replace("--delete", "").Trim()) __color("Aborted.\n", ConsoleColor.Yellow); - else - { - File.Delete(__getNickname(_command.Replace("--delete", "").Trim())); - __color("Deleted.\n", ConsoleColor.Green); - } - } - else __color("Aborted.\n", ConsoleColor.Yellow); - } - else - { - __color("ERROR: ", ConsoleColor.Red); - Console.WriteLine("SWSH -> {0} -> nickname does not exists", _command.Replace("--delete", "").Trim()); - } - } - catch (Exception exp) - { - __color("ERROR: ", ConsoleColor.Red); - Console.WriteLine(exp.Message); - } - } - else if (_command.StartsWith("--edit")) - { - _command = _command.Remove(0, 6); - String[] data = _command.Split(' '); - if (File.Exists(__getNickname(data[1]))) - { - string[] arrLine = File.ReadAllLines(__getNickname(data[1])); - if (data[2] == "-user") arrLine[1] = data[3]; - else if (data[2] == "-server") arrLine[2] = data[3]; - else if (data[2] == "-key") - { - if (!Directory.Exists(data[3])) - { - __color("ERROR: ", ConsoleColor.Red); - Console.Write("SWSH -> {0} -> file is non existent.\n", data[3]); - __color("exit", ConsoleColor.Red); - Console.Write(" or "); - __color("-e", ConsoleColor.Red); - Console.Write(" to cancel.\n"); - while (true) - { - Console.Write("-key: "); - var key = __getCommand(); - if (key == "-e" || key == "exit") - { - __color("Aborted.\n", ConsoleColor.Yellow); - break; - } - if (File.Exists(key)) - { - arrLine[0] = key; - break; - } - else - { - __color("ERROR: ", ConsoleColor.Red); - Console.Write("SWSH -> {0} -> file is non existent.\n", key); - } - } - } - } - File.WriteAllLines(__getNickname(data[1]), arrLine); - __color("Updated.\n", ConsoleColor.Green); - } - else - { - __color("ERROR: ", ConsoleColor.Red); - Console.WriteLine("SWSH -> {0} -> nickname does not exists", data[1]); - } - } - else if (_command == "clear") - { - Console.Clear(); - __version(); - Console.Write("swsh --help or -h for help.\n\n"); - } + else if (_command.StartsWith("--connect") || _command.StartsWith("-c")) __connect(); + else if (_command.StartsWith("--show")) __show(); + else if (_command.StartsWith("--delete")) __delete(); + else if (_command.StartsWith("--edit")) __edit(); + else if (_command == "clear") __clear(); else if (_command == "exit") break; - else - { - Console.ForegroundColor = ConsoleColor.Red; - __help(); - Console.ResetColor(); - } - } - else if (_command == "ls") - { - if (Directory.GetFiles(_workingDirectory).Length > 0) - { - __color("files: \n", ConsoleColor.Cyan); - foreach (var file in Directory.GetFiles(_workingDirectory)) - Console.WriteLine(Path.GetFileName(file)); - } - if (Directory.GetDirectories(_workingDirectory).Length > 0) - { - __color("\ndirectories: \n", ConsoleColor.DarkCyan); - foreach (var dir in Directory.GetDirectories(_workingDirectory)) - Console.WriteLine((dir.Replace(Path.GetDirectoryName(dir) + Path.DirectorySeparatorChar, "")).Replace('\\', '/')); - } - if (Directory.GetDirectories(_workingDirectory).Length == 0 && Directory.GetFiles(_workingDirectory).Length == 0) __color("No file" + - "s or directories here.\n", ConsoleColor.Yellow); - } - else if (_command.StartsWith("cd")) - { - _command = _command.Remove(0, 3); - if (_command == "..") __changeWorkingDir(Path.GetDirectoryName(_workingDirectory)); - else if (_command.StartsWith("./")) __changeWorkingDir(_workingDirectory + "/" + _command.Remove(0, 2)); - else if (_command.StartsWith("/")) __changeWorkingDir(Path.GetPathRoot(_workingDirectory) + _command.Remove(0, 1)); - else __changeWorkingDir(_workingDirectory + "/" + _command); + else __help(); } + else if (_command == "ls") __ls(); + else if (_command.StartsWith("cd")) __cd(); else __color("ERROR: SWSH -> " + _command + " -> unknown command.\n", ConsoleColor.Red); } catch (Exception exp) @@ -242,13 +66,6 @@ private static void __start() } } } - private static void __version() - { - Console.Write(" ______ _______ __ __\n / ___/ | / / ___// / / /\n \\__ \\| | /| / /\\__ \\/ /_/ / \n ___/ /| |/ |/ /___/ / __" - + " / \n/____/ |__/|__//____/_/ /_/ \n Secure Windows Shell \n"); - Console.Write("\nRelease: {0}-{1}\n{2}", _codename, _version, "(c) Muhammad Muzzammil & Nabeel Omer\nSWSH is licensed under the GNU General Public License v" + - "3.0\n"); - } private static void __addConnection() { _command = (_command.StartsWith("--")) ? _command.Replace("--add", "").Trim() : _command.Replace("-a", "").Trim(); @@ -339,6 +156,152 @@ private static void __addConnection() if (!Directory.Exists(_mainDirectory)) Directory.CreateDirectory(_mainDirectory); File.AppendAllLines(__getNickname(nkn), data); } + private static void __interactiveHelp() + { + _command = (_command.StartsWith("--help") ? _command.Remove(0, 6).Trim() : _command.Remove(0, 2)).Trim(); + if (_command.Length > 0) + { + var title = "Help for " + _command; + Console.WriteLine(title); + for (int i = 0; i < title.Length; i++) Console.Write("="); + Console.WriteLine(); + switch (_command) + { + case "-v": + case "--version": + { + Console.WriteLine("Syntax: swsh --version"); + Console.WriteLine("Checks the version of swsh.\n\nUsage: swsh --version\n"); + break; + } + case "-a": + case "--add": + { + Console.WriteLine("Syntax: (swsh --add/swsh -a) [-password]"); + Console.WriteLine("Add a new connection either using private key or password.\n\nUsage to add using private " + + "key: swsh --add\nUsage to add using a password: swsh --add -password\n\nYou'll be asked for password " + + "each time as SWSH doesn't store them.\n"); + break; + } + case "--show": + { + Console.WriteLine("Syntax: swsh --show [nickname]"); + Console.WriteLine("Show nicknames if no arguments have passed. If nickname is provided, shows details of a n" + + "ickname.\nUsage to check all nicknames: swsh --show\nUsage to check a nickname: swsh --show myserver"); + break; + } + case "-c": + case "--connect": + { + Console.WriteLine("Syntax: (swsh --connect/swsh -c) [nickname]"); + Console.WriteLine("Connects to Server over SSH.\nUsage: swsh --connect myserver"); + break; + } + case "--delete": + { + Console.WriteLine("Syntax: swsh --delete [nickname]"); + Console.WriteLine("Deletes connection's nickname.\nUsage: swsh --delete myserver"); + break; + } + case "--edit": + { + Console.WriteLine("Syntax: swsh --edit [nickname] [arg]"); + Console.WriteLine("arg:\n\t-user [newUserName]\n\t-key [newKey]\n\t-server [newServer]"); + Console.WriteLine("Edits nickname, use one argument at a time.\nUsage: swsh --edit myserver -user newUSER"); + break; + } + case "-h": + case "--help": + { + Console.WriteLine("Syntax: (swsh --help/swsh -h) [command]"); + Console.WriteLine("Displays this help or command details.\nUsage: swsh --help -h"); + break; + } + default: + __color("ERROR: SWSH -> " + _command + " -> unknown command.\n", ConsoleColor.Red); + break; + } + } + else __help(); + } + private static void __help() + { + Console.Write("swsh [command] [arg]\n"); + Console.WriteLine("\t-v --version: -Check the version of swsh."); + Console.WriteLine("\t-a --add [-password]* -Add a new connection either using private key or password (-password)."); + Console.WriteLine("\t --show [nickname]* -Show nicknames/Details of a nickname."); + Console.WriteLine("\t-c --connect [nickname] -Connects to Server over SSH."); + Console.WriteLine("\t --delete [nickname] -Deletes connection's nickname."); + Console.WriteLine("\t --edit [nickname] [arg] -Edits nickname, use one argument at a time."); + Console.WriteLine("\t-h --help [command]* -Displays this help or command details."); + Console.WriteLine("\tclear -Clears the console."); + Console.WriteLine("\texit -Exits."); + Console.WriteLine("ls -Lists all files and directories in working directory."); + Console.WriteLine("cd [arg] -Changes directory to 'arg'. arg = directory name."); + Console.WriteLine("\n\nNOTES:\n[1] * = Optional."); + } + private static void __connect() + { + ConnectionInfo ccinfo; + string nickname = (_command.StartsWith("--connect")) ? _command.Remove(0, 10) : _command.Remove(0, 3); + if (File.Exists(_mainDirectory + nickname + ".swsh")) + { + if (File.ReadAllLines(_mainDirectory + nickname + ".swsh")[0] == "-password") + { + Console.Write("Password for {0}: ", nickname); + ccinfo = __CreateConnectionInfoPassword(nickname, __getCommand()); + } + else ccinfo = __CreateConnectionInfoKey(nickname); + if (ccinfo != null) + { + Console.Write("Waiting for response from {0}@{1}...\n", ccinfo.Username, ccinfo.Host); + using (var ssh = new SshClient(ccinfo)) + { + ssh.Connect(); + __color("Connected to " + ccinfo.Username + "@" + ccinfo.Host + "...\n", ConsoleColor.Green); + string pwd = " ", home = ""; + home = pwd = ssh.CreateCommand("echo $HOME").Execute(); + while (true) + { + pwd = Regex.Replace(ssh.CreateCommand("cd " + pwd + "; pwd").Execute(), @"\t|\n|\r", ""); + if (pwd == Regex.Replace(home, @"\t|\n|\r", "")) pwd = "~"; + __color(pwd, ConsoleColor.Green); + Console.Write(":/ $ "); + _command = __getCommand(); + if (_command == "exit") + break; + else if (_command.StartsWith("cd")) + { + _command = _command.Remove(0, 3); + if (_command.StartsWith("/")) pwd = _command; + else if (_command.StartsWith("./")) pwd += "/" + _command.Remove(0, 2); + else if (_command.StartsWith("..")) pwd = Regex.Replace(ssh.CreateCommand("cd " + pwd + "; dirname $(pwd)").Execute(), @"\t|\n|\r", ""); + else if (_command.Trim() == String.Empty) pwd = "~"; + else pwd += "/" + _command; + } + else if (_command == "clear") Console.Clear(); + else if(_command.StartsWith("swsh")) + { + __color("ERROR: ", ConsoleColor.Red); + Console.Write("SWSH -> can't execute swsh while in connection\n", nickname); + } + else + { + var result = ssh.CreateCommand("cd " + pwd + "; " + _command).Execute(); + Console.Write(result); + } + } + ssh.Disconnect(); + } + __color("Connection to " + ccinfo.Username + "@" + ccinfo.Host + ", closed.\n", ConsoleColor.Yellow); + } + } + else + { + __color("ERROR: ", ConsoleColor.Red); + Console.Write("SWSH -> {0} -> nickname does not exists\n", nickname); + } + } private static void __show() { _command = _command.Remove(0, 6); @@ -412,89 +375,126 @@ private static void __show() } } } - private static void __interactiveHelp() + private static void __delete() { - _command = (_command.StartsWith("--help") ? _command.Remove(0, 6).Trim() : _command.Remove(0, 2)).Trim(); - if (_command.Length > 0) + try { - var title = "Help for " + _command; - Console.WriteLine(title); - for (int i = 0; i < title.Length; i++) Console.Write("="); - Console.WriteLine(); - switch (_command) + if (File.Exists(__getNickname(_command.Replace("--delete", "").Trim()))) { - case "-v": - case "--version": - { - Console.WriteLine("Syntax: swsh --version"); - Console.WriteLine("Checks the version of swsh.\n\nUsage: swsh --version\n"); - break; - } - case "-a": - case "--add": - { - Console.WriteLine("Syntax: (swsh --add/swsh -a) [-password]"); - Console.WriteLine("Add a new connection either using private key or password.\n\nUsage to add using private " - + "key: swsh --add\nUsage to add using a password: swsh --add -password\n\nYou'll be asked for password " - + "each time as SWSH doesn't store them.\n"); - break; - } - case "--show": - { - Console.WriteLine("Syntax: swsh --show [nickname]"); - Console.WriteLine("Show nicknames if no arguments have passed. If nickname is provided, shows details of a n" - + "ickname.\nUsage to check all nicknames: swsh --show\nUsage to check a nickname: swsh --show myserver"); - break; - } - case "-c": - case "--connect": - { - Console.WriteLine("Syntax: (swsh --connect/swsh -c) [nickname]"); - Console.WriteLine("Connects to Server over SSH.\nUsage: swsh --connect myserver"); - break; - } - case "--delete": - { - Console.WriteLine("Syntax: swsh --delete [nickname]"); - Console.WriteLine("Deletes connection's nickname.\nUsage: swsh --delete myserver"); - break; - } - case "--edit": + __color("Are you sure you want to delete this nickname? (y/n): ", ConsoleColor.Red); + var ans = __getCommand().ToUpper(); + if (ans == "Y") + { + Console.Write("Type the nickname to confirm: "); + var name = __getCommand(); + if (name != _command.Replace("--delete", "").Trim()) __color("Aborted.\n", ConsoleColor.Yellow); + else { - Console.WriteLine("Syntax: swsh --edit [nickname] [arg]"); - Console.WriteLine("arg:\n\t-user [newUserName]\n\t-key [newKey]\n\t-server [newServer]"); - Console.WriteLine("Edits nickname, use one argument at a time.\nUsage: swsh --edit myserver -user newUSER"); - break; + File.Delete(__getNickname(_command.Replace("--delete", "").Trim())); + __color("Deleted.\n", ConsoleColor.Green); } - case "-h": - case "--help": + } + else __color("Aborted.\n", ConsoleColor.Yellow); + } + else + { + __color("ERROR: ", ConsoleColor.Red); + Console.WriteLine("SWSH -> {0} -> nickname does not exists", _command.Replace("--delete", "").Trim()); + } + } + catch (Exception exp) + { + __color("ERROR: ", ConsoleColor.Red); + Console.WriteLine(exp.Message); + } + } + private static void __edit() + { + _command = _command.Remove(0, 6); + String[] data = _command.Split(' '); + if (File.Exists(__getNickname(data[1]))) + { + string[] arrLine = File.ReadAllLines(__getNickname(data[1])); + if (data[2] == "-user") arrLine[1] = data[3]; + else if (data[2] == "-server") arrLine[2] = data[3]; + else if (data[2] == "-key") + { + if (!Directory.Exists(data[3])) + { + __color("ERROR: ", ConsoleColor.Red); + Console.Write("SWSH -> {0} -> file is non existent.\n", data[3]); + __color("exit", ConsoleColor.Red); + Console.Write(" or "); + __color("-e", ConsoleColor.Red); + Console.Write(" to cancel.\n"); + while (true) { - Console.WriteLine("Syntax: (swsh --help/swsh -h) [command]"); - Console.WriteLine("Displays this help or command details.\nUsage: swsh --help -h"); - break; + Console.Write("-key: "); + var key = __getCommand(); + if (key == "-e" || key == "exit") + { + __color("Aborted.\n", ConsoleColor.Yellow); + break; + } + if (File.Exists(key)) + { + arrLine[0] = key; + break; + } + else + { + __color("ERROR: ", ConsoleColor.Red); + Console.Write("SWSH -> {0} -> file is non existent.\n", key); + } } - default: - __color("ERROR: SWSH -> " + _command + " -> unknown command.\n", ConsoleColor.Red); - break; + } } + File.WriteAllLines(__getNickname(data[1]), arrLine); + __color("Updated.\n", ConsoleColor.Green); + } + else + { + __color("ERROR: ", ConsoleColor.Red); + Console.WriteLine("SWSH -> {0} -> nickname does not exists", data[1]); } - else __help(); } - private static void __help() + private static void __clear() { - Console.Write("swsh [command] [arg]\n"); - Console.WriteLine("\t-v --version: -Check the version of swsh."); - Console.WriteLine("\t-a --add [-password]* -Add a new connection either using private key or password (-password)."); - Console.WriteLine("\t --show [nickname]* -Show nicknames/Details of a nickname."); - Console.WriteLine("\t-c --connect [nickname] -Connects to Server over SSH."); - Console.WriteLine("\t --delete [nickname] -Deletes connection's nickname."); - Console.WriteLine("\t --edit [nickname] [arg] -Edits nickname, use one argument at a time."); - Console.WriteLine("\t-h --help [command]* -Displays this help or command details."); - Console.WriteLine("\tclear -Clears the console."); - Console.WriteLine("\texit -Exits."); - Console.WriteLine("ls -Lists all files and directories in working directory."); - Console.WriteLine("cd [arg] -Changes directory to 'arg'. arg = directory name."); - Console.WriteLine("\n\nNOTES:\n[1] * = Optional."); + Console.Clear(); + __version(); + Console.Write("swsh --help or -h for help.\n\n"); + } + private static void __ls() + { + if (Directory.GetFiles(_workingDirectory).Length > 0) + { + __color("files: \n", ConsoleColor.Cyan); + foreach (var file in Directory.GetFiles(_workingDirectory)) + Console.WriteLine(Path.GetFileName(file)); + } + if (Directory.GetDirectories(_workingDirectory).Length > 0) + { + __color("\ndirectories: \n", ConsoleColor.DarkCyan); + foreach (var dir in Directory.GetDirectories(_workingDirectory)) + Console.WriteLine((dir.Replace(Path.GetDirectoryName(dir) + Path.DirectorySeparatorChar, "")).Replace('\\', '/')); + } + if (Directory.GetDirectories(_workingDirectory).Length == 0 && Directory.GetFiles(_workingDirectory).Length == 0) __color("No file" + + "s or directories here.\n", ConsoleColor.Yellow); + } + private static void __cd() + { + _command = _command.Remove(0, 3); + if (_command == "..") __changeWorkingDir(Path.GetDirectoryName(_workingDirectory)); + else if (_command.StartsWith("./")) __changeWorkingDir(_workingDirectory + "/" + _command.Remove(0, 2)); + else if (_command.StartsWith("/")) __changeWorkingDir(Path.GetPathRoot(_workingDirectory) + _command.Remove(0, 1)); + else __changeWorkingDir(_workingDirectory + "/" + _command); + } + private static void __version() + { + Console.Write(" ______ _______ __ __\n / ___/ | / / ___// / / /\n \\__ \\| | /| / /\\__ \\/ /_/ / \n ___/ /| |/ |/ /___/ / __" + + " / \n/____/ |__/|__//____/_/ /_/ \n Secure Windows Shell \n"); + Console.Write("\nRelease: {0}-{1}\n{2}", _codename, _version, "(c) Muhammad Muzzammil & Nabeel Omer\nSWSH is licensed under the GNU General Public License v" + + "3.0\n"); } private static void __changeWorkingDir(string path) { @@ -608,4 +608,4 @@ private static ConnectionInfo __CreateConnectionInfoPassword(string nickname, st return null; } } -} +} \ No newline at end of file diff --git a/checksum b/checksum index 358c77f3..5d8f977d 100644 --- a/checksum +++ b/checksum @@ -1 +1 @@ -945e484c044706b200b141689a5697816f8af2a4 \ No newline at end of file +2f3ec79a89a8e9b66c77a80410266f2a53da751a \ No newline at end of file