当前位置: 首页 > 编程语言 > C#

S#SSH .Net库中的Sudo命令分享

时间:2023-04-10 16:47:20 C#

C#学习教程:S#SSH.Net库中的Sudo命令但是,我无法让它工作。使用(varssh=newSshClient(“主机名”,“用户名”,“密码”)){ssh.Connect();varinput=newMemoryStream();varsr=newStreamWriter(输入);变种输出=控制台。打开标准输出();varshell=ssh.CreateShell(输入,输出,输出);shell.Stopped+=delegate{Console.WriteLine("nDisconnected...");};壳.开始();sr.WriteLine("sudols");sr.Flush();线程.睡眠(1000*1);sr.WriteLine("密码");sr.Flush();线程.睡眠(1000*100);shell.停止();每次上次登录时我都会收到以下错误:2015年1月14日星期三15:51:46来自我的计算机公司SshNet.Loggingverbose:1:ReceiveMessagefromserver:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。SshNet.Logging详细信息:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。[1;36m这是BASH[1;31m4.1[1;36m-显示在[1;31m:0.0[m]WedJan1415:55:50CST2015SshNet.Loggingverbose:1:来自服务器ReceiveMessage:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。SshNet.Logging详细信息:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。-bash:And,:commandnotfound[0;34musername@host[0;31m[15:55:50]>[0m以上都是C#学习教程:S#SSH.Net库中的Sudo命令分享,如果对你有用,需要多了解C#学习教程,希望大家多多关注——publicvoidExpectSSH(stringaddress,stringlogin,stringpassword,stringcommand){try{SshClientsshClient=newSshClient(地址,22,登录名,密码);sshClient.Connect();IDictionarytermkvp=newDictionary();termkvp.Add(Renci.SshNet.Common.TerminalModes.ECHO,53);ShellStreamshellStream=sshClient.CreateShellStream("xterm",80,24,800,600,1024,termkvp);//获取登录字符串rep=shellStream.Expect(newRegex(@"[$>]"));//期待用户提示this.writeOutput(results,rep);//发送命令shellStream.WriteLine(commandTxt.Text);rep=shellStream.Expect(newRegex(@"([$#>:])"));//需要密码或用户提示this.writeOutput(results,rep);//检查发送密码if(rep.Contains(":")){//发送密码shellStream.WriteLine(password);rep=shellStream.Expect(新正则表达式(@"[$#>]"));//期待用户或根提示this.writeOutput(results,rep);}sshClient.Disconnect();}//尝试打开连接catch(Exceptionex){System.Console.WriteLine(ex.ToString());throwex;}}本文采集自网络,不代表立场,如涉及侵权,请点击维权联系管理员删除,转载请注明来源: