// To be able to use the BssCfgManip COM object as shown: // - Right click on C# project, Add > Reference..., COM > Type Libraries, BssCfg915Lib // - Replace "915" with the actual BssCfgManip version for your SSH Server installation, both in Add > Reference, and in this code // - BssCfgManip version does not increment if there are no changes, so e.g. BssCfg915 applies to 9.xx versions 9.16 and higher using System; using BssCfg915Lib; namespace BssCfgFromCs { class BssCfgFromCs { static int Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage:"); Console.WriteLine(" listVirt - list configured virtual accounts"); Console.WriteLine(" rmVirt - remove a virtual account identified by name"); Console.WriteLine(" mkVirt - create a virtual account"); Console.WriteLine(""); Console.WriteLine("This program must be run from an administrative, elevated console window."); return 2; } string command = args[0]; Console.WriteLine("Instantiating BssCfgManip COM object"); var cfg = new CBssCfg915(); cfg.SetInstance(""); Console.WriteLine("Locking SSH Server settings"); cfg.settings.Lock(); try { Console.WriteLine("Loading SSH Server settings"); cfg.settings.Load(); if (command.Equals("listVirt", StringComparison.OrdinalIgnoreCase)) { if (args.Length != 1) { Console.WriteLine("No parameters expected"); return 2; } Console.WriteLine("Listing virtual accounts"); foreach (IVirtAccount915 acct in cfg.settings.access.virtAccounts.entries) Console.WriteLine("Found account: {0}", acct.virtAccount); } else if (command.Equals("rmVirt", StringComparison.OrdinalIgnoreCase)) { if (args.Length != 2) { Console.WriteLine("One parameter expected"); return 2; } string accountToRemove = args[1]; bool accountFound = false; for (uint i=0; i!=cfg.settings.access.virtAccounts.count; ++i) { IVirtAccount915 acct = cfg.settings.access.virtAccounts.GetItem(i); if (acct.virtAccount.Equals(accountToRemove, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Account found"); accountFound = true; cfg.settings.access.virtAccounts.Erase(i); break; } } if (!accountFound) { Console.WriteLine("Account not found"); return 1; } Console.WriteLine("Account removed. Saving settings"); cfg.settings.Save(); } else if (command.Equals("mkVirt", StringComparison.OrdinalIgnoreCase)) { if (args.Length != 3) { Console.WriteLine("Two parameters expected"); return 2; } string accountName = args[1]; string password = args[2]; IVirtAccount915 acct = cfg.settings.access.virtAccounts.@new; acct.virtAccount = accountName; acct.virtPassword.Set(password); acct.group = "Virtual Users"; cfg.settings.access.virtAccounts.NewCommit(); Console.WriteLine("Account created. Saving settings"); cfg.settings.Save(); } } finally { cfg.settings.Unlock(); } return 0; } } }