调试错误

时间:2016-03-02 09:01:19

标签: c# ssh ssh.net

所以我正在创建一个可以打开与远程设备的连接并执行不同命令的应用程序。所以昨天我离开工作之前,当我收到错误时,我正在调试。但是,由于我的应用程序忽略它并继续进行并且没有足够的时间来立即修复它,我今天决定这样做。当我想再次与我的程序建立连接时,它说它无法进行身份验证(注意*参数没有改变)。

所以我做了一些检查以确定问题,在登录服务器并运行netstat后,我发现有一个活动连接到端口22,它来自我的应用程序。

在我重启TWICE之前,我的SSH管理器中没有显示连接。

为了在生产环境中防止这样的事情,我该如何防止这样的事情。

我的Program.cs

class Program
    {
        static void Main(string[] args)
        {
            var ip="";
            var port=0;
            var user="";
            var pwd="";
            var cmdCommand="";
            ConnectionInfo ConnNfo;
            ExecuteCommand exec = new ExecuteCommand();
            SSHConnection sshConn = new SSHConnection();


            if (args.Length > 0)
            {
                ip = args[0];
                port = Convert.ToInt32(args[1]);
                user = args[2];
                pwd = args[3];
                cmdCommand = args[4];

                ConnNfo = sshConn.makeSSHConnection(ip, port, user, pwd);
                exec.executeCMDbySSH(ConnNfo, cmdCommand);

            }
            else {
                try
                {
                    XMLParser parser = new XMLParser();
                    List<List<string>> configVars = parser.createReader("C:\\Users\\myusername\\Desktop\\config.xml");
                    Console.WriteLine("this is from program.cs");

                    //iterate through array
                    for (int i = 0; i < configVars[0].Count; i++)
                    {
                        if ((configVars[0][i].ToString() == "device" && configVars[1][i].ToString() == "device") && (configVars[0][i + 6].ToString() == "device" && configVars[1][i + 6].ToString() == "no value"))
                        {
                            string ipAdress = configVars[1][i + 1].ToString();
                            int portNum = Convert.ToInt32(configVars[1][i + 2]);
                            string username = configVars[1][i + 3].ToString();
                            string passwd = configVars[1][i + 4].ToString();
                            string command = configVars[1][i + 5].ToString();
                            Console.WriteLine("making connection with:");
                            Console.WriteLine(ipAdress + " " + portNum + " " + username + " " + passwd + " " + command);
                            ConnNfo = sshConn.makeSSHConnection(ipAdress, portNum, username, passwd);
                            Console.WriteLine("executing command: ");
                            exec.executeCMDbySSH(ConnNfo, command);

                        }
                    }

                }
                catch (Exception e) { Console.WriteLine("Error occurred: " + e); }
            }

            Console.WriteLine("press a key to exit");
            Console.ReadKey();
        }
    }

我的executeCommand类:

 public class ExecuteCommand
    {
        public ExecuteCommand()
        {

        }
        public void executeCMDbySSH(ConnectionInfo ConnNfo, string cmdCommand )
        {
            try
            {

                using (var sshclient = new SshClient(ConnNfo))
                {
                     //the error appeared here at sshclient.Connect();
                    sshclient.Connect();
                    using (var cmd = sshclient.CreateCommand(cmdCommand))
                    {

                        cmd.Execute();
                        Console.WriteLine("Command>" + cmd.CommandText);
                        Console.WriteLine(cmd.Result);
                        Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                    }
                    sshclient.Disconnect();
                }
        }
            catch (Exception e) { Console.WriteLine("Error occurred: " + e); }
}
    }

和我上课的地方:

public class SSHConnection
    {
        public SSHConnection() { }

        public ConnectionInfo makeSSHConnection(string ipAdress, int port, string user, string pwd)
        {
            ConnectionInfo ConnNfo = new ConnectionInfo(ipAdress, port, user,
              new AuthenticationMethod[]{

                // Pasword based Authentication
                new PasswordAuthenticationMethod(user,pwd),
              }
                 );
            return ConnNfo;
        }
    }

注意*我没有包含我的XMLParser类,因为它与问题无关,也没有任何关于SSH的连接。

1 个答案:

答案 0 :(得分:0)

修改 我发现我编译了应用程序,它在命令行中运行。结果证明代码没有错误