Execute linux command on Centos using dotnet core - centos

I'm running a .NET Core Console Application on CentOS box. The below code is executing for normal command like uptime, but not executing for lz4 -dc --no-sparse vnp.tar.lz4 | tar xf - Logs.pdf:
try
{
var connectionInfo = new ConnectionInfo("server", "username", new PasswordAuthenticationMethod("username", "pwd"));
using (var client = new SshClient(connectionInfo))
{
client.Connect();
Console.WriteLine("Hello World!");
var command = client.CreateCommand("lz4 -dc --no-sparse vnp.tar | tar xf - Logs.pdf");
var result = command.Execute();
Console.WriteLine("yup ! UNIX Commands Executed from C#.net Application");
Console.WriteLine("Response came form UNIX Shell" + result);
client.Disconnect();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Expected output is Logs.pdf file needs to be extracted and saved in the current location. Can someone correct me where im

If application is running on Linux machine then you can try this also:
string command = "write your command here";
string result = "";
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
result += proc.StandardOutput.ReadToEnd();
result += proc.StandardError.ReadToEnd();
proc.WaitForExit();
}
return result;

Related

Getting files in a directory using java API from Kubernetes pod

I am trying to get the list of files in a pod directory using this code this
Code I tried:
import io.kubernetes.client.Exec;
..........
Exec exec = new Exec();
boolean tty = System.console() != null;
String[] command = {"/bin/sh","-c", "ls", "/var/log"};
final Process proc =
exec.exec(namespace, podName, command , false, tty);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
but this always output "data", instead of the files in the dir. How can i make it work. If. i try this using kubectl command line this works.

Download file using Wget yammer export data

I create console application that export data from yammer to local using wget
third party tool
and this is the reference
https://developer.yammer.com/docs/data-export-api
the function that execute script:
internal static bool ExecuteScript()
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine("wget -O export.zip -t 1 --header \"Authorization: Bearer %Token%\" -ca-certificate cacert.pem https://www.yammer.com/api/v1/export?since=2016-02-09T00:00:00z");
p.StandardInput.WriteLine(#"exit");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
p.Close();
Console.WriteLine("Error:" + error);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
i replace %Token% with my token
but when run the code it cut off download and create export.zip file 0KB
it's not download complete file
it show this message in console
Console application output
although i take this script in batch file and run it from cmd in same path it download complete file
notes:
1- I add Wget path to Path Environment
2- I'm using windows 10
3- I'm using VS 2013
i discover the issue
p.StandardInput.WriteLine("wget -O export.zip -t 1 --header \"Authorization: Bearer <Access Token>\" --ca-certificate=cacert.pem cacert.pem https://www.yammer.com/api/v1/export?since=2016-02-09T00:00:00z");

Any way to access a NFS from Bluemix runtime?

I need to push an application to Bluemix that requires access to a remote file system via NFS.
I haven't found any NFS service. Existing Object Store services use Swift API and the application requires native file system access, not other kind of APIs.
I tried to execute "mkdir" and "mount" commands from app initialization but it seems there are restrictions for such executions from the user that runs the runtime. I got return codes that mean errors.
So I run out of ideas. Do you have any suggestion or idea to explore?
I think Dockers could be an option (haven't explored yet if I can mount the nfs file system) but currently it is in Beta, so no production ready.
Thanks!
NFS is not supported, however Cloud Foundry now supports FUSE, which is a pretty close alternative to NFS.
To take advantage of FUSE you need to use the cflinuxfs2 stack.
cflinuxfs2 is a new stack that supports FUSE, see below. Cloud Foundry recently added FUSE support, see here for more info.
name description
lucid64 Ubuntu 10.04
cflinuxfs2 Ubuntu 14.04.2 trusty
When you push your app you need to use the -s option, example below.
cf push myappname -s cflinuxfs2.
Update:
The poster posted a solution to his another question on how to do this. I have pasted it below...
Ok, finally I found the reason of the issue with the help of team colleagues. Problem was with permissions of the private ssh key. It has to be 600 and by default it was 644 after the cf push.
So here it is the final code (quick and dirty) that worked, just in case it can be useful to others...
1.- Include into the app the private key and the known_hosts files.
2.- Push the app adding "-s cflinuxfs2" parameter.
3.- Execute at runtime startup some code like this:
String s = null;
Process p = null;
BufferedReader br = null;
try
{
p = Runtime.getRuntime().exec("mkdir -p /home/vcap/misc");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("#### Executing command mkdir with exit: " + p.exitValue());
p.destroy();
br.close();
p = Runtime.getRuntime().exec("chmod 600 /home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/cloud.key");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("#### Executing command chmod with exit: " + p.exitValue());
p.destroy();
br.close();
p = Runtime.getRuntime().exec("chmod 600 /home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/known_hosts");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("#### Executing command chmod with exit: " + p.exitValue());
p.destroy();
br.close();
p = Runtime.getRuntime().exec("sshfs ibmcloud#129.41.133.34:/home/ibmcloud /home/vcap/misc -o IdentityFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/cloud.key -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/vcap/app/wlp/usr/servers/defaultServer/apps/myapp.ear/known_hosts -o idmap=user -o compression=no -o sshfs_debug");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("#### Executing command sshfs with exit: " + p.exitValue());
p.destroy();
br.close();
p = Runtime.getRuntime().exec("ls -l /home/vcap/misc");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("#### Executing command ls with exit: " + p.exitValue());
p.destroy();
br.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(br != null)
br.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
This snippet should create a folder, mount a remote file system into that folder and list the content of the remote file system.

error in build to process builder in java

I have devloped myself tagger and I want to host on the web. my server is based on jsp. But This tagger is based on svmtool and it has been written in Perl script. That's why, I have created one ".java" file. In this file I've created Processor builder and via the Runtime.getRuntime().exec through this process I am calling this file. It is working but it doesn't show my output. Plz help to solve this issue. For the better under standing below I am giving my java code and also given last line of output/stop the process:
import java.io.*;
public class ExeCommand { String outS = "", errS="";
try {
// run the Unix "type your terminal command" command
System.out.println ("Running tagger!");
String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt"; Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("It will take time so keep the patience:\n" + command);
System.out.println ("First error line: " + stdError.readLine());
// read any errors from the attempted command
System.out.println("Please check your command (if any):\n");
while ((errS = stdError.readLine()) != null) {
System.out.println("Error:\t" + errS);
}
stdError.close();
System.out.println ("First output line: " + stdInput.readLine());
while ((outS = stdInput.readLine()) != null) {
System.out.println("Output:\t" + outS);
}
stdInput.close();
System.out.println("Finished!");
}
catch (Exception e) {
System.out.println("found exception: " + e);
e.printStackTrace();
//System.exit(-1);
}
System.out.println("Finished all!");
}
}
After this nothing doing/ last output shows in terminal:
TAGGING < DIRECTION = left-to-right then right-to-left >
First, your perl script redirects its standard output to a file (
/home/tomcat4.1.40/webapps/pos/textfile/output.txt). So there is no way, you can capture standard output in Java.
Instead, you can use tee command which can write the output to both your output file and the standard output. Now, this can be captured in Java.
Second, use ProcessBuilder instead of Runtime. Process Builder is more flexible and gives you options to capture the standard output / input / error.
ProcessBuilder p = new ProcessBuilder();
p.inheritIO(); // Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.
p.command("perl", "somescript.pl");
p.start();
As I wrote here:
Sorry for the harsh words, but your code is a disaster. No compiler do something with it.
I have made no further tweaks to your code, I have the code modified so that it works at least:
package stackoverflow;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExeCommand {
/**
*
* run the Unix "type your terminal command" command
*
* #param args
*/
public static void main(String[] args) {
System.out.println("Running tagger!");
String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
// String command = "ls -og";
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
// read the output from the command
System.out.println("It will take time so keep the patience:\n" + command);
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
try {
System.out.println(": " + stdError.readLine());
} catch (IOException e) {
e.printStackTrace();
}
// read any errors from the attempted command
System.out.println("Please check your command (if any):\n");
try {
String errS = "";
while ((errS = stdError.readLine()) != null) {
System.out.println("Error:\t" + errS);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
stdError.close();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
try {
System.out.println("First output line: " + stdInput.readLine());
} catch (IOException e) {
e.printStackTrace();
}
try {
String outS = "";
while ((outS = stdInput.readLine()) != null) {
System.out.println("Output:\t" + outS);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
stdInput.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished!");
}
}
Replace the lines 19 and 20
String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
// String command = "ls -og";
with
// String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
String command = "ls -og";
Then you get this output:
Running tagger!
It will take time so keep the patience:
ls -og
: null
Please check your command (if any):
First output line: insgesamt 12
Output: drwxrwxr-x 3 4096 Mai 23 20:51 bin
Output: drwxrwxr-x 3 4096 Apr 26 15:49 src
Output: -rw-rw-r-- 1 28 Apr 26 15:51 test
Finished!

reading content of a file from server then sending it to client through socket C#

i am trying here to send the content of a text file by the server and send it to the client
this is the server
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(localEP);
server.Listen(10);
Console.WriteLine("Waiting for Client...");
Socket client = server.Accept();
IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
Console.WriteLine("Got connection from " + clientAddress);
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Welcome to my test server");
writer.Flush();
string line = null;
while ((line = reader.ReadLine()).Length != 0)
{
Console.WriteLine("loooking for this file:" + line);
System.IO.FileInfo fi = new System.IO.FileInfo(line);
Console.WriteLine("Found");
writer.WriteLine("File Size: " + fi.Length + "\nContent:");
StreamReader tr = new StreamReader(line);
string s = null;
//string b = "";
while((s= tr.ReadLine()).Length != 0)
{
writer.WriteLine(tr.ReadLine());
writer.Flush();
}
tr.Close();
}
client.Close(); server.Close();
the part of the client where it reads from the server is this
String line = null;
line = textBox3.Text;
writer.WriteLine(line); // Send line to Server
writer.Flush();
string s = null;
// Read line from server, then echo on the screen
while((s= reader.ReadLine()).Length != 0)
{
textBox4.Text += reader.ReadLine() + "\r\n\r\n";
}
when i run the code, no errors at all, but the client get stuck, and when i stop the server, the content of the file will show,,, BTW, its a GUI application
while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}
Sample code for StreamReader uses the construct below to detect end of stream. Also - do you really want to read two lines in that loop?
while (reader.Peek() >= 0)
{
s= reader.ReadLine();
textBox4.Text += s + Environment.NewLine + Environment.NewLine;
}
You mentioned that this is a GUI app? If so, on which thread are you doing the reading? If you are doing the read on the main thread, then the application messageloop will be frozen and nothing will show up until you stop the other side and kill the connection.