I have this code:
using System;
using System.Text;
using Tamir.SharpSsh;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string output = exec.RunCommand("interface wireless scan wlan1 duration=5");
Console.WriteLine(output);
exec.Close();
}
}
}
The command will execute! I can see that, but! It immediately prints data. Or actually, it does'nt print the data from the command. It prints like the... logo for the mikrotik os. I actually need the data from the command, and I need SharpSSH to wait at least 5 seconds for data, then give it back to me...
Somebody knows how I can do this?
I'm pretty new to this! I appreciate all help! Thank you!
You may want to try the following overload:
SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string stdOut = null;
string stdError = null;
exec.RunCommand("interface wireless scan wlan1 duration=5", ref stdOut, ref stdError);
Console.WriteLine(stdOut);
exec.Close();
If their API does what the name implies, it should put the standard output of your command
in stdOut and the standard error in stdError.
For more information about standard streams, check this out: http://en.wikipedia.org/wiki/Standard_streams
Related
I've just installed Solr/Lucene on a Windows machine simply to test its capability. I've indexed a couple hundred files and I'm able to successfully query the indexed content through the web interface. Given that the web interface isn't too user friendly I thought I'd try to create a simple application to do a full text search over the indexed content. So far I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Lucene.Net;
namespace solrTest
{
class Program
{
static void Main(string[] args)
{
string indexFileLocation = #"C:\solr-5.3.1\server\solr\test\data\index";
Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(indexFileLocation);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir);
Lucene.Net.Index.Term searchTerm = new Lucene.Net.Index.Term("ID", "1");
Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm);
Lucene.Net.Search.TopDocs results = searcher.Search(query, null, 10);
foreach(Lucene.Net.Search.ScoreDoc test in results.ScoreDocs)
{
Console.WriteLine(test.ToString());
Console.ReadLine();
}
}
}
}
When running the code I receive an error stating:
An unhandled exception of type 'System.IO.IOException' occurred in Lucene.Net.dll
Additional information: read past EOF
I know I'm missing something obvious. Your help would be greatly appreciated.
Edit: I downloaded Luke.Net and received the same error.
I want to get position of my motor by using command "POS;", but I get this output "a ⌂▲ yI ° y" what with this if I can get numbers?
Then from time to time I get empty answer I was answered that it take some time to get output via Serial Port. What I have to add to my code to wait until I wil get full output to show?
Manual controller (update manual)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SerialPort sp = new SerialPort();
sp.PortName = "COM1";
sp.BaudRate = 9600;
sp.Open();
sp.Encoding = System.Text.Encoding.GetEncoding(28591);
if (sp.IsOpen)
{
sp.Write("ENA;");
sp.Write("POS;");
string msgPos = sp.ReadExisting();
Console.WriteLine(msgPos);
sp.Write("OFF;");
sp.Close();
Console.ReadKey();
}
}
}
}
Just add a Thread.Sleep(50) betwen the send the write and the read command. 50 miliseconds should be enough if not try a longer time.
//Do something
sp.Write("POS;");
Thread.Sleep(50);
string msgPos = sp.ReadExisting();
//Do something else
I can't find any command POS; in the manual you posted. Do you mean FBK on page 15?
How can i get the cpuload of running-threads of my application.
My application runs on linux, mac NOT windows.
I update mono to version 3.0.2.
Now i can get the correct thread-count of "Process.GetCurrentProcess().Threads" but no ProcessThread object is available to read the "TotalProcessorTime"
What can i do to calculate the cpu-usage/threads of my running application?
Can i get the linux-process-id of my running thread? If i can, i can read the proc directory structure but i can't find any way.
I hope someone can help me.
Apparently the Process.Threads property is only partially implemented at the moment:
// This'll return a correctly-sized array of empty ProcessThreads for now.
int error;
return new ProcessThreadCollection(new ProcessThread[GetProcessData (pid, 0, out error)]);
Not sure what trouble you have run into getting the process id, this code seems to work for me:
using System;
using System.Diagnostics;
using System.IO;
class MainClass
{
static void Main(string[] args)
{
int pid = Process.GetCurrentProcess().Id;
DirectoryInfo taskDir = new DirectoryInfo(String.Format("/proc/{0}/task", pid));
foreach(DirectoryInfo threadDir in taskDir.GetDirectories())
{
int tid = Int32.Parse(threadDir.Name);
Console.WriteLine(tid);
}
}
}
This is just a sample, but it will help illustrate what I'm trying to do.
I know how to get the current directory as shown in the script below, and I can can set a file variable.
The problem I'm having is that I can't figure out how to make it create a folder and put the file in the folder
For example (using the variables below)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var cd = Directory.GetCurrentDirectory();
Directory.CreateDirectory(cd: \5app\);
File.Copy(c:\xyz.txt, cd: \5app\xyz.txt
}
}
}
I know what I have written above is not correct because vs10 tells me so, but doesn't give me very much help.
You're missing a parenthesis and a semicolon, and, especially, arguments of methods Directory.CreateDirectory() and File.Copy() are strings, put them inside quotes:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var cd = Directory.GetCurrentDirectory();
Directory.CreateDirectory(cd + #"\5app\");
File.Copy(#"c:\xyz.txt", cd + #"\5app\xyz.txt");
}
}
}
MSDN references: Directory.CreateDirectory, File.Copy
Thanks to Cole Johnson for pointing out that it shouldn't be #"cd: \5app\".
You don't use quotes.
In addition, I recommend against explicit parameter naming. If you look at the CIL generated when using explicit parameters, there is a performance downgrade as the parameter variables are saved to a local variable, then passed. This results in an unneeded strfld command.
There are several problems with your code, which Compiler Errors will likely help you to unravel:
The method Directory.CreateDirectory(string path) requires a string, which is encased in "".
Here is an MSDN article on how to use Directory.CreateDirectory
Same with the method File.Copy(string source, string destination)
Here is an MSDN article on how to use File.Copy
Since Directory.GetDirectory() returns a string, you can just concatinate your specific directory to the result. But remember to use proper Escape Sequences in your strings for things like Backslash.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string cd = Directory.GetCurrentDirectory();
Directory.CreateDirectory(cd + "\\5app\\");
File.Copy("c:\\xyz.txt", cd + "\\5app\\xyz.txt");
}
}
}
I created a Process that is targeted to the Java.exe file. The process should run a Java.jar server file, and continue to run giving output feedback until the server crashed or I forcefully close it down. Everything works fine.. the server is running.. and when I set UseShellExecute to True I can see the black CMD window returning all the output. But when I set it to false and redirect the output... the Window is completely blank ( the server is still running though ) and the OutputDataReceived event doesn't fire at all ( I think I got it to once but that was when I closed the window it seemed the args was empty ) and as far as I can see StandardOutput.ReadToEnd() isnt returning anything either. Why is this Process not returning any Output feedback?? Here is my code:
gameServerProcess = new Process();
gameServerProcess.StartInfo.UseShellExecute = false;
gameServerProcess.StartInfo.RedirectStandardOutput = true;
gameServerProcess.StartInfo.RedirectStandardInput = true;
gameServerProcess.EnableRaisingEvents = true;
gameServerProcess.Exited += new EventHandler(gameServer_WindowExit);
window = new ServerWindow();
gameServerProcess.OutputDataReceived += new DataReceivedEventHandler(window.server_recievedOutputStream);
window.Show();
gameServerProcess.StartInfo.FileName = #"D:\Program Files\Java\jdk1.6.0_12\bin\java.exe";
gameServerProcess.StartInfo.WorkingDirectory = #"D:\Users\Zack\Desktop\ServerFiles\gameserver";
gameServerProcess.StartInfo.Arguments = #"-Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer";
gameServerProcess.Start();
gameServerProcess.BeginOutputReadLine();
And my 'window' form class code which should receive the DataReceivedEventArgs with the output data:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MWRemoteServer
{
public partial class ServerWindow : Form
{
private delegate void WriteOutputDelegate(string output);
private WriteOutputDelegate writeOutput;
public ServerWindow()
{
InitializeComponent();
logBox.BackColor = Color.White;
logBox.ForeColor = Color.Black;
writeOutput = new WriteOutputDelegate(write);
}
public void server_recievedOutputStream(object sender, DataReceivedEventArgs args)
{
MessageBox.Show("Called window output!");
if (args.Data != null)
{
BeginInvoke(writeOutput, new object[] { args.Data.ToString() });
}
}
private void write(string output)
{
logBox.AppendText(output + Environment.NewLine);
}
}
}
Again the process works completely fine and with the UseShellExecute set to true I can see it is providing all information I need to grab. But when I set that to false its blank and I cant seem to grab any output data!
Thank you so much in advance... I've been at this for hours and hours...
Are you sure it writes to stdout? Have you tried checking stderr? (RedirectStandardError/StandardError/ErrorDataReceived/BeginErrorReadLine).
If it writes directly to the display buffer (instead of stderr/stdout) then it might not be possible without a lot of work.