I am trying to create a SSH client using C#. I am using the Tamir.SharpSsh library. I am having issues sending the command and getting an appropriate response from the server.
The way I have been testing it is by sending over the ls command, followed by cd.. command, followed by ls command again -- to see whether or not the cd.. command is being executed properly. However, there is no difference between the first ls command and the second ls command. I am not entirely sure what I am doing wrong. Perhaps I am using the wrong Ssh type. I have provided the code and the output I am getting. I have also added the output that I expect.
using Tamir.SharpSsh;
namespace SSHNetExample
{
class Program
{
static void Main(string[] args)
{
SshExec ssh = null;
try
{
Console.Write("-Connecting...");
ssh = new SshExec(host, username, password);
ssh.Connect();
Console.WriteLine("OK ({0}/{1})", ssh.Cipher, ssh.Mac);
Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
Console.WriteLine("-Use the 'exit' command to disconnect.");
Console.WriteLine();
while(true)
{
string command = Console.ReadLine();
if(command == "exit")
break;
string data = ssh.RunCommand(command);
Console.WriteLine(data);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
if(ssh != null)
{
Console.WriteLine("Disconnecting...");
ssh.Close();
Console.WriteLine("OK");
}
}
}
}
Output:
Expected Output:
The SshExec class is for executing a single command. It logs into the server for each command, and executes the command you asked it to. It's similar (but not the same) as you logging in, typing cd .., logging out again, logging in again and typing ls - when you logged in the second time you were redirected to the home directory.
If you want to continue to use the SshExec, you should combine your commands so you can just execute a single command. In this instance, for example, you would execute ls ...
If you want to be able to issue multiple commands like an interactive session, look at the SshShell class instead of SshExec.
Related
I want to change directory inside SSH using C# with SSH.NET library:
SshClient cSSH = new SshClient("192.168.80.21", 22, "appmi", "Appmi");
cSSH.Connect();
Console.WriteLine("current directory:");
Console.WriteLine(cSSH.CreateCommand("pwd").Execute());
Console.WriteLine("change directory");
Console.WriteLine(cSSH.CreateCommand("cdr abc-log").Execute());
Console.WriteLine("show directory");
Console.WriteLine(cSSH.CreateCommand("pwd").Execute());
cSSH.Disconnect();
cSSH.Dispose();
Console.ReadKey();
But it's not working. I have also checked below:
Console.WriteLine(cSSH.RunCommand("cdr abc-log").Execute());
but still is not working.
I believe you want the commands to affect the subsequent commands.
But SshClient.CreateCommand uses SSH "exec" channel to execute the command. That means that every command is executed in an isolated shell and has no effect on the other commands.
If you need to execute commands in a way that previous commands affect later commands (like changing a working directory or setting an environment variable), you have to execute all commands in the same channel. Use an appropriate construct of the server's shell for that. On most systems you can use semicolons:
Console.WriteLine(cSSH.CreateCommand("pwd ; cdr abc-log ; pwd").Execute());
On *nix servers, you can also use && to make the following commands be executed only when the previous commands succeeded:
Console.WriteLine(cSSH.CreateCommand("pwd && cdr abc-log && pwd").Execute());
Some less common systems (for example AIX) may not even have a way to execute multiple commands in one "command-line". In these cases, you may need to use a shell channel, what is otherwise not recommended.
Also when the other commands are actually sub commands of the first command, you may need different solution.
See Providing subcommands to a command (sudo/su) executed with SSH.NET SshClient.CreateShellStream.
this is what I have done and its working for me:
SshClient sshClient = new SshClient("some IP", 22, "loign", "pwd");
sshClient.Connect();
ShellStream shellStream = sshClient.CreateShellStream("xterm", 80, 40, 80, 40, 1024);
string cmd = "ls";
shellStream.WriteLine(cmd + "; echo !");
while (shellStream.Length == 0)
Thread.Sleep(500);
StringBuilder result = new StringBuilder();
string line;
string dbt = #"PuttyTest.txt";
StreamWriter sw = new StreamWriter(dbt, append: true);
while ((line = shellStream.ReadLine()) != "!")
{
result.AppendLine(line);
sw.WriteLine(line);
}
sw.Close();
sshClient.Disconnect();
sshClient.Dispose();
Console.ReadKey();
I'm using SSH.NET in order to get some informations of an external tape drive. Connecting and executing some basic commands works perfectly fine for me:
ConnectionInfo connNfo = new ConnectionInfo("10.12.2.97", 22, "loginuser",
new AuthenticationMethod[]
{
// Password based Authentication
new PasswordAuthenticationMethod("loginuser","password")
}
);
using (var client = new SshClient(connNfo))
{
client.Connect();
//This command works fine
using (var cmd = client.CreateCommand("ls"))
{
string result = cmd.Execute();
}
}
But now i have to switch to root user in order to execute extended commands i need. Initially logging in with root user is not possible because of security restrictions. Referring to this post How to run commands by sudo and enter password by ssh .net c# i tried this code block:
using (var cmd = ssh.RunCommand("echo 'rootpassword' | sudo -u root -S fsstate"))
{
if (cmd.ExitStatus == 0)
Console.WriteLine(cmd.Result);
else
Console.WriteLine(cmd.Error);
}
But then i always receive this error message:
sudo: unknown user: root
sudo: unable to initialize policy plugin
Update 1:
Working PuTTY commands
What i'm doing wrong here?
I have been following this post for developing a client for query notification. http://www.youdidwhatwithtsql.com/started-query-notifications-sql-server-2008-r2/1676/ I have tried this code on both visual studio and mono on my PC and these seem to fire the onDependencyChange event fine. However when I move it over to the raspberry pi with mono-complete installed it does not seem to fire. I cannot debug as the pi is in another location and I am using SSH to remote into it.
static void Main(string[] args)
{
Console.WriteLine ("-----------------APPLICATION STARTED------------------");
var connection = new SqlConnection(connectionString);
SqlDependency.Start(connectionString);
RefreshDataWithSqlDependency();
Console.WriteLine ("Why is it here?");
//blocks thread so you can read message
Console.ReadLine();
SqlDependency.Stop(connectionString);
}
static void RefreshDataWithSqlDependency()
{
//remove existing dependency if necessary
if (dependency != null)
{
dependency.OnChange -= onDependencyChange;
dependency = null;
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT ipAddress FROM dbo.dbDevices", connection);
//Create a dependency and associate it with command
dependency = new SqlDependency(command, null, 1);
//Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(onDependencyChange);
//Start dependency listener
SqlDependency.Start(connectionString);
//execute command and refresh data
RefreshData(command);
}
}
private static void onDependencyChange(Object o, SqlNotificationEventArgs args)
{
Console.WriteLine("ondep gets hit");
if ((args.Source.ToString() == "Data") || (args.Source.ToString() == "Timeout"))
{
Console.WriteLine("Refreshing data due to {0}", args.Source);
RefreshDataWithSqlDependency();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Data not refreshed due to unexpected SqlNotificationEventArgs: Source={0}, Info={1}, Type={2}", args.Source, args.Info, args.Type.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
}
}
private static void RefreshData(SqlCommand command)
{
using (var reader = command.ExecuteReader())
{
Console.Clear();
while (reader.Read())
{
Console.WriteLine("ip = {0}", reader[0]);
}
}
}
I have now put an extra Console.WriteLine just under RefreshDataWithSqlDependency method and when I use Run > Run With > Microsoft .NET or Mono 4.0.2 it seems to jump straight out of RefreshDataWithSqlDependency, however when I run with debugger it acts as it should. It will fire the event.
Remote debugging would be the way to go with SSH tunneling.
On the Ras-pi side, start your app:
mono \
--debug \
--debugger-agent=transport=dt_socket,address=0.0.0.0:10000,suspend=y,server=y,keepalive=250 \
foodata.exe
(Yes, the address of 0.0.0.0 is correct)
On the PC side, set an environment variable to enable a hidden debug feature of Xamarian Studio / MonoDevelop and start the IDE from that cmd line.
On Linux:
export MONODEVELOP_SDB_TEST=1
monodevelop
On OS-X (with Xam Studio, alter the cmd to use MonoDevelop if that is what you have:
export MONODEVELOP_SDB_TEST=1
/Applications/Xamarin\ Studio.app/Contents/MacOS/XamarinStudio
On Windows (update the path to match your install location):
set MONODEVELOP_SDB_TEST=1
<path>\XamarinStudio.exe
Once the IDE starts, load your solution/project that you are debugging, set your breakpoints and then select the following menu items:
Run / Run With / Custom Command Soft Mono Debugger
Note: That option will NOT be there if you you did not set the env var and run it from the cmd line.
In the Launch Soft Debugger Window that appeared:
Command : ssh YourRaspiLogin#RasPiPassword -L 10000:127.0.0.1:10000
Arguments : <leave empty>
IP : YourRasPiHostNameOrIPAddress
Port: : 10000
Click "Connect".
A ssh window will open, just leave it open (minimize it..), that is your reverse tunnel. If you are using some other ssh client; putty, etc.. change the 'ssh' in the command field to match your setup.
You should be debugging now and assuming you set a break point the IDE should be stopped on that line waiting for you ;-)
I need to execute this action using a C# code:
open putty.exe in the background (this is like a cmd window)
login to a remote host using its IP address
enter a user name and password
execute several commands one after the other.
run another command that gets a response telling me that the commands I ran before that where executed successfully
So I'm trying to do it like this:
ProcessStartInfo proc = new ProcessStartInfo()
{
FileName = #"C:\putty.exe",
UseShellExecute = true, //I think I need to use shell execute ?
RedirectStandardInput = false,
RedirectStandardOutput = false,
Arguments = string.Format("-ssh {0}#{1} 22 -pw {2}", userName, hostIP, password)
... //How do I send commands to be executed here ?
};
Process.Start(proc);
You could try https://github.com/sshnet/SSH.NET.
With this you wouldn't need putty or a window at all.
You can get the responses too.
It would look sth. like this.
SshClient sshclient = new SshClient("172.0.0.1", userName, password);
sshclient.Connect();
SshCommand sc= sshclient .CreateCommand("Your Commands here");
sc.Execute();
string answer = sc.Result;
Edit: Another approach would be to use a shellstream.
Create a ShellStream once like:
ShellStream stream = sshclient.CreateShellStream("customCommand", 80, 24, 800, 600, 1024);
Then you can use a command like this:
public StringBuilder sendCommand(string customCMD)
{
StringBuilder answer;
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
WriteStream(customCMD, writer, stream);
answer = ReadStream(reader);
return answer;
}
private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
writer.WriteLine(cmd);
while (stream.Length == 0)
{
Thread.Sleep(500);
}
}
private StringBuilder ReadStream(StreamReader reader)
{
StringBuilder result = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
result.AppendLine(line);
}
return result;
}
While the answer by #LzyPanda works, using an SSH "shell" channel (SshClient.CreateShellStream), let alone an interactive terminal, is not a good idea for automating commands execution. You get lot of side-effects from that, like command prompts, ANSI sequences, interactive behavior of some commands, etc.
For automation, use an SSH "exec" channel (SshClient.CreateCommand):
using (var command = ssh.CreateCommand("command"))
{
Console.Write(command.Execute());
}
If you need to execute multiple commands, repeat the above code. You can create any number of "exec" channels for one SSH connection.
Though if the commands depend on each other (first command modified the environment, e.g. variables, that are used by the latter commands), you have execute them within one channel. Use a shell syntax for that, like && or ;:
using (var command = ssh.CreateCommand("command1 && command2"))
{
Console.Write(command.Execute());
}
If you need to continuously read the commands output use:
using (var command = ssh.CreateCommand("command"))
{
var asyncExecute = command.BeginExecute();
command.OutputStream.CopyTo(Console.OpenStandardOutput());
command.EndExecute(asyncExecute);
}
You can also use ExtendedOutputStream, which contains stderr. See SSH.NET real-time command output monitoring.
Unfortunately implementation of "exec" channel in SSH.NET does not allow providing an input to the command. For that use case, you will need to resort to the "shell" channel, until this limitation is solved.
I am trying to export a database from c# using mysqldump.
When I run it i get this message: Unknown database 'mysqldump' when selecting the database.
I can't find the solution.
public static void mysqlBackup()
{
try
{
//string time = DateTime.Now.ToString("dd-MM-yyyy");
Log.Info("Starting MySQL dump");
Process MySqlDump = new Process();
MySqlDump.StartInfo.FileName = #"mysqldump.exe";
MySqlDump.StartInfo.UseShellExecute = false;
MySqlDump.StartInfo.Arguments =
"mysqldump -uroot -p******** b3 >"+
" C:/Users/Administrator/Documents/temp/backups/backup.sql";
MySqlDump.StartInfo.RedirectStandardInput = false;
MySqlDump.StartInfo.RedirectStandardOutput = false;
MySqlDump.Start();
MySqlDump.WaitForExit();
MySqlDump.Close();
Log.Info("Successfull created");
}
catch (IOException ex)
{
Log.Error("Unable to write the database file" + ex.ToString());
}
}
I tried to remove the mysqldump from the arguments kinda the same problem.
The redirection operator > is not an argument to mysqldump. When you execute it on the command line, it's being interpreted by the command line itself, not by mysqldump. You have two choices here:
Use the --result-file option as others have mentioned
Capture the stdout of the process and do what you like with the output by setting the RedirectStandardOutput property of StartInfo to be true. After this, you can read from the StandardOutput stream of the process.
I think you need to specify the name of the database you want to dump as the first argument. Thanks to nathan it goes after --databases at the end.
MySqlDump.StartInfo.Arguments = "-u root -p *** database_name --result-file [path]\backup.sql";
You don't need to specify mysqldump again in the command either (not that it should make much difference).
The Mysql documentation states there are 3 ways to use the mysqldump command:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
Ensure the command works fine via your command line. If it does that execute that command directly within your code. If that works then start extracting your arguments and replacing them with your own parameters within code.
Basically you want to get as basic as possible and work back up from there.
If the file works on the command line, try this:
using (Process p = new Process())
{
p.StartInfo.FileName = #"mysqldump.exe -u root -p *** --database b3 -r test.sql"; <~~~ note the change here
p.Start();
p.WaitForExit();
}
The file will be dumped to your project folders bin/debug or bin/release folder unless you change that code.
Here is your edited method:
public static void mysqlBackup()
{
try
{
//string time = DateTime.Now.ToString("dd-MM-yyyy");
Log.Info("Starting MySQL dump");
using(Process MySqlDump = new Process()
{
MySqlDump.StartInfo.FileName = #"mysqldump.exe";
MySqlDump.StartInfo.UseShellExecute = false;
MySqlDump.StartInfo.Arguments = "-uroot -p******** b3 --result-file=C:/Users/Administrator/Documents/temp/backups/backup.sql";
MySqlDump.StartInfo.RedirectStandardInput = false;
MySqlDump.StartInfo.RedirectStandardOutput = false; //You can redirect this as mention in other answers
MySqlDump.Start();
MySqlDump.WaitForExit();
MySqlDump.Close();
}
Log.Info("Successfully created");
}
catch (IOException ex)
{
Log.Error("Unable to write the database file" + ex.ToString());
}
}