public void RunOnNeighbor2(string mac, string identity, string user, string pass, string cmd) { using (var client = new SshClient(_host, _user, _pass)) { client.Connect(); var shellStream = new SshShellStream(client.CreateShellStream("xterm", 80, 600, 0, 0, 32 * 1024), debug: true); var macTelnet = new MacTelnet(shellStream, mac, identity); if (!macTelnet.Login(user, pass)) { throw new Exception("Can't login"); } var actions = new List <SshShellAction>(); const string startActions = "start_actions"; const string endActions = "end_actions"; actions.Add(shellStream.NewAction(startActions, $"{cmd}; :put {endActions};")); actions.Add(shellStream.NewAction(endActions, "/quit")); var result = macTelnet.Run($":put {startActions};", actions); if (!result) { throw new Exception("Can't run actions"); } client.Disconnect(); } }
public bool Login(string user = "******", string pass = "") { var firstprompt = new Regex(@"\[.*@.*\].>.$"); var mkpromptstr = @"\[.*@{identity}\].>.*$".Replace("{identity}", _identity); var secondprompt = new Regex(mkpromptstr); var actions = new List <SshShellAction> { _shellStream.NewAction(firstprompt, $"/tool mac-telnet {_mac}"), _shellStream.NewAction("Login:"******"Password:", pass), _shellStream.NewAction(secondprompt, Environment.NewLine) }; var results = _shellStream.RunActions(actions); _login = results.All(kv => kv.Value > 0); return(_login); }