1

I am using a switch and I want to connect it via ssh or telnet and send commands to specific ports. I succeeded connecting to the switch but I am not able to execute commands after login.

string command = "reboot";
using (Process process = new Process())
{
     process.StartInfo.FileName = "plink.exe";
     process.StartInfo.Arguments = "admin@192.168.42.7 -pw admin " + command;
     process.StartInfo.UseShellExecute = true;
     process.StartInfo.CreateNoWindow = false;
     process.Start();
}

Code given above logins to the switch interface but not execute the command. It hangs with switch# text.

I tried a couple of things from cmd and I am able to get command on the screen after login but now I cannot get the same result when I write same text in C# code.

plink.exe admin@192.168.42.7 -pw admin < D:\input.txt --> this works when executed from cmd not in C#.

using (Process process = new Process())
{
     process.StartInfo.FileName = "plink.exe";
     process.StartInfo.Arguments = "admin@192.168.42.7 -pw admin < D:\\input.txt";
     process.StartInfo.UseShellExecute = true;
     process.StartInfo.CreateNoWindow = false;
     process.Start();
}

What could be the reason? Thanks

snvngrc
  • 177
  • 2
  • 4
  • 12
  • Are you missing a space after "-pw admin"? – juharr Mar 08 '18 at 14:07
  • No, I am sorry but I think this is not that easy. I am facing with that problem more than 1 day. – snvngrc Mar 08 '18 at 14:08
  • The code you have is passing the arguments as "admin@192.168.42.7 -pw adminreboot". I don't think you want "adminreboot" all as one word do you? – juharr Mar 08 '18 at 14:09
  • I edited my question :) – snvngrc Mar 08 '18 at 14:11
  • 3
    It is the command processor that implements the `<` operator. You must therefore start Cmd.exe instead. Use its -c option to pass the command line. Start with -k so you can see errors. – Hans Passant Mar 08 '18 at 14:13
  • @snvngrc - Set the filename for process' StartInfo as `cmd`, and pass plink.exe in the Arguments. Check if that helps. – Am_I_Helpful Mar 08 '18 at 14:13
  • Do not drive an external console application for implement SSH. Use a native .NET class instead! – Martin Prikryl Mar 08 '18 at 14:16
  • @MartinPrikryl I tried SshClient already. But it also doesn't work. – snvngrc Mar 08 '18 at 14:20
  • So ask a question about `SshClient`, instead of hacking with an external application. – Martin Prikryl Mar 08 '18 at 14:22
  • @Hans Passant Thanks for the answer but it didn't work. – snvngrc Mar 08 '18 at 14:48
  • @MartinPrikryl If SshClient didn't work already, why am I asking my question about it? – snvngrc Mar 08 '18 at 14:49
  • You can do much more with `SshClient` than with Plink. In other words, there's nothing you can do with Plink, that you cannot do with `SshClient`. Just because you didn't manage to make `SshClient` work, does not mean that it cannot work. – Martin Prikryl Mar 08 '18 at 14:52
  • It seems like you can manage easily. Then why don't you help me instead of writing unnecessary comments? – snvngrc Mar 08 '18 at 14:56
  • I've linked a question with an answer to your question about plink. - If you want a help with `SshClient`, you have to ask a new question with a code that you have tried, and good description of problems that you face with it. – Martin Prikryl Mar 08 '18 at 15:16

0 Answers0