Пример #1
0
        public Script AddScript(string Name)
        {
            Script s = new Script(Name);

            scripts.Add(s);
            return(s);
        }
Пример #2
0
 public void AddHost(Host host)
 {
     hosts.Add(host);
 }
Пример #3
0
        private void WrkrRunScript_DoWork(object sender, DoWorkEventArgs e)
        {
            Action actnEnableDisableButtons = () => btnRun.Enabled = !(btnStop.Enabled = true);

            btnRun.Invoke(actnEnableDisableButtons);
            MinListe <Script> scripts = new MinListe <Script>();

            if (e.Argument != null)
            {
                scripts.Add((Script)e.Argument);
            }
            else
            {
                scripts = scol.Scripts;
            }

            foreach (Script script in scripts)
            {
                WriteOutput($"---Running script: {script.Name}");

                foreach (Host host in script.Hosts)
                {
                    if (wrkrRunScript.CancellationPending)
                    {
                        break;
                    }
                    WriteOutput($"Connecting to {host.Name}... ");

                    //if (host.ConType == null)
                    //    WriteOutput("Connection-mode has not been set!", false);
                    //else
                    //{
                    try
                    {
                        //if (host.ConType == connectionType.SSH)
                        //{
                        using (SshClient client = new SshClient(host.Address, txtUsername.Text, txtPassword.Text))
                        {
                            if (wrkrRunScript.CancellationPending)
                            {
                                break;
                            }
                            client.Connect();

                            if (client.IsConnected)
                            {
                                if (wrkrRunScript.CancellationPending)
                                {
                                    break;
                                }
                                WriteOutput("Connected!", false);

                                using (ShellStream ss = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
                                {
                                    WriteOutput(scom.ReadToEnd(ss, 300), false);
                                    foreach (string c in script.Commands)
                                    {
                                        if (wrkrRunScript.CancellationPending)
                                        {
                                            break;
                                        }
                                        //WriteOutput(scom.SendCommandCR(c, ss, 300), false);
                                        WriteOutput(scom.SendCommandLF(c, ss, 300), false);
                                    }
                                }

                                client.Disconnect();
                                WriteOutput("Disconnected!", false);
                            }
                            else
                            {
                                WriteOutput("Connection failed!", false);
                            }
                        }
                        //}
                        //else
                        //{
                        //    if (wrkrRunScript.CancellationPending) break;
                        //    WriteOutput("Connecting to {0}...", host.Name);
                        //    TelnetCOM tc = new TelnetCOM(host.Address, 23);
                        //    string s = tc.Login(txtPassword.Text, 1000);
                        //    string prompt = s.TrimEnd();
                        //    prompt = s.Substring(prompt.Length - 1, 1);
                        //    if (prompt != "$" && prompt != ">")
                        //        WriteOutput("Connection failed!", false);
                        //    else
                        //    {
                        //        foreach (string c in script.Commands)
                        //        {
                        //            if (wrkrRunScript.CancellationPending) break;
                        //            tc.WriteLine(c);
                        //            WriteOutput(tc.Read());
                        //        }
                        //    }
                        //    WriteOutput("Disconnected!");
                        //}
                    }
                    catch (Renci.SshNet.Common.SshAuthenticationException)
                    {
                        WriteOutput("Authentication failed!", false);
                    }
                    catch (ArgumentException ex)
                    {
                        if (ex.Message == "username")
                        {
                            WriteOutput("Username missing!", false);
                        }
                        else
                        {
                            WriteOutput("CAUGHT: " + ex.ToString());
                        }
                    }
                    catch (System.Net.Sockets.SocketException se)
                    {
                        switch (se.ErrorCode)
                        {
                        case 10060:
                            WriteOutput("Connection timed out!", false);
                            break;

                        case 11001:
                            WriteOutput("Could not find host!", false);
                            break;

                        default:
                            WriteOutput("CAUGHT: " + se.ToString());
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteOutput("CAUGHT: " + ex.ToString());
                    }
                    //}
                }

                WriteOutput($"---Finished script: {script.Name}{Environment.NewLine}");
            }
        }