如何运行与SSH.Net几个命令? [英] How to run several commands with SSH.Net?
本文介绍了如何运行与SSH.Net几个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用SSH.Net用户无线MAC地址添加到阿鲁巴无线控制器,因此我运行以下命令:
//连接到服务器删除
sshClient sshclient =新sshClient(主机IP,用户名,密码);
sshclient.Connect();
//进入启用命令并按回车键
SshCommand使= sshclient.CreateCommand(使能);
enable.Execute();
//的使能的管理员密码命令提示,所以输入密码,按输入
SshCommand PWD = sshclient.CreateCommand(enablePassword);
pwd.Execute();
//输入cofigure T命令并按回车键
SshCommand配置= sshclient.CreateCommand(配置T);
config.Execute();
SshCommand AAA = sshclient.CreateCommand(AAA推导规则用户ManagersDevices);
aaa.Execute();
SshCommand设置= sshclient.CreateCommand(设置角色状态MACADDR等于\我的Mac address\设定值来宾介绍\test\);
set.Execute();
SshCommand保存= sshclient.CreateCommand(写内存);
save.Execute();
但是没有什么发生,并且用户不添加...
< DIV CLASS =h2_lin>解决方案
得到了我的答案的这里
我从这个回答中使用的代码是:
sshclient =新SshClient(IP地址,username的密码);
sshclient.Connect();
ShellStream流= sshclient.CreateShellStream(customCommand,80,24,800,600,1024);
SendCommand(流,我要执行的命令);
私人静态字符串SendCommand(ShellStream流串customCMD)
{
StringBuilder的strAnswer =新的StringBuilder();
变种读者=新的StreamReader(流);
变种作家=新的StreamWriter(流);
writer.AutoFlush = TRUE;
WriteStream(customCMD,作家,流);
strAnswer.AppendLine(ReadStream(读卡器));
串答案= strAnswer.ToString();
返回answer.Trim();
}
私有静态无效WriteStream(字符串CMD,StreamWriter的作家,ShellStream流)
{
writer.WriteLine(CMD);
,而(stream.Length == 0)
的Thread.Sleep(500);
}
私人静态字符串ReadStream(StreamReader的读者)
{
StringBuilder的结果=新的StringBuilder();
串线;
,而((行= reader.ReadLine())!= NULL)
result.AppendLine(线);
返回result.ToString();
}
I'm trying to use SSH.Net to add users Wifi mac address to Aruba Wifi controller, so I'm running these commands:
//connect to the remove server
SshClient sshclient = new SshClient(hostIP, userName, password);
sshclient.Connect();
//enter the "enable" command and press enter
SshCommand enable = sshclient.CreateCommand("enable");
enable.Execute();
//the "enable" command prompts for admin password, so enter the password and press enter
SshCommand pwd = sshclient.CreateCommand(enablePassword);
pwd.Execute();
//enter "cofigure t" command and press enter
SshCommand config = sshclient.CreateCommand("configure t");
config.Execute();
SshCommand aaa = sshclient.CreateCommand("aaa derivation-rules user ManagersDevices");
aaa.Execute();
SshCommand set = sshclient.CreateCommand("set role condition macaddr equals \"my mac address\" set-value guest description \"test\"");
set.Execute();
SshCommand save = sshclient.CreateCommand("write memory");
save.Execute();
But nothings happens and the user is not added...
解决方案
Got my answer here
Code I used from this answer is:
sshclient = new SshClient("ip address", "userName", "password");
sshclient.Connect();
ShellStream stream = sshclient.CreateShellStream("customCommand", 80, 24, 800, 600, 1024);
SendCommand(stream, "my command to execute");
private static string SendCommand(ShellStream stream, string customCMD)
{
StringBuilder strAnswer = new StringBuilder();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
WriteStream(customCMD, writer, stream);
strAnswer.AppendLine(ReadStream(reader));
string answer = strAnswer.ToString();
return answer.Trim();
}
private static void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
writer.WriteLine(cmd);
while (stream.Length == 0)
Thread.Sleep(500);
}
private static string ReadStream(StreamReader reader)
{
StringBuilder result = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
result.AppendLine(line);
return result.ToString();
}
这篇关于如何运行与SSH.Net几个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文