SharpSSH with Persistent ShellExec connections - c#

I'm using SharpSSH to connect to an SSH server and I've tried using both SshShell and SshExec.
I need to be able to take a series of commands and send them to the server in order, so SshShell doesn't really do what I need since I would have to wrangle streams the whole time and it seems that it would be a bit of a kludge. So I've tried SshExec but found one problem with it, every time I send a command it seems to be making a new connection and losing the context of the last command. For example if I ran the following commands:
pwd
cd .ssh
pwd
I would expect it to output
/home/adam
/home/adam/.ssh
But, instead it just ouputs "/home/adam" both times, meaning that the directory change was lost in between.
Is there a way I can configure this so that it maintains a constant connection to the SSH server until I tell it to disconnect?

Do this:
exec.RunCommand("pwd; cd Desktop; pwd")
I am not sure how to do advanced commands, but I tried that and it outputs:
/Users/MyUser
/Users/MyUser/Desktop

To cd to a hidden directory (any directory beginning with a dot (.) character), you need to enclose the value in quotes.
According to the documentation:
4) If the first component of the directory operand is dot or dot-dot, proceed to step 6.
6) Set curpath to the string formed by the concatenation of the value of PWD , a slash character, and the operand.
In short, cd '.ssh' should do the trick.

Related

Executing command using SSH.NET on digi 6030dx modem - what are this character sequences starting with left square bracket?

I have some large project that connect to many devices over SSH.NET
Now I have to add support for new modem witch is digi 6030dx.
I added it and I am able to connect with no issues
But when I send some/any command like show config the output is:
[16C[0K
[16C[0Ks
[17C[0Kh
[18C[0Ko
[19C[0Kw
[20C[0K
[20C[0Kc
[21C[0Ko
[22C[0Kn
[23C[0Kf
[24C[0Ki
[25C[0Kg
Commands
-------------------------------------------------------------------------------
config View and modify the configuration
exit Exit the CLI
analyzer Analyzer commands.
cli-legacy Enter the legacy Admin CLI.
cp Copy a file or directory.
help Show CLI editing and navigation commands.
ls List a directory.
mkdir Create a directory.
modem Modem commands.
more View a file.
mv Move a file or directory.
ping Ping a host.
reboot Reboot the system.
rm Remove a file or directory.
scp Copy a file or directory over SSH.
show Show instance statistics.
system System commands.
traceroute Print the route packets trace to network host.
update Update firmware.
dra.wk.0001> ashowconfigdra.wk.0001> ashowconfig
[16C[0K
Does anyone known what are this strange signs / why my command is splited by this and newlines?
It is first device that have this issue and the app support over 200 other with no issues.
I guess some coding issue or something ? putty does not show this signs so probably 'understand' them somehow?
Those are ANSI escape codes.
In general, with SSH, you get these only if your client (library) declares support for terminal emulation.
SSH.NET library does that always, when you use the "shell" channel (SshClient.CreateShell or SshClient.CreateShellStream).
In general (were you connecting to well behaving SSH server), to avoid getting the codes:
Use "exec" channel (use SshClient.RunCommand). SSH.NET does not use terminal emulation on "exec" channel. Though SSH servers on "devices" (contrary to full servers) usually do not implement the "exec" channel. See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
Modify SSH.NET code not to request terminal emulation for the "shell" channel. – Remove SendPseudoTerminalRequest request from Shell.Start implementation.
Though as you are connecting to some "device" and telneting further to another device and the codes probably come from the far device, the question is whether this will in fact have any effect on that device at all. Possibly you won't be able to avoid getting the codes. Lack of terminal emulation on the first SSH connection possibly won't have any effect on the second telnet connection.
In the end, you may have to deal with the codes.
See also How to strip ANSI escape codes from AIX topas command result in C#.
i did for now
this.answer = Regex.Replace(this.answer, #"\r\[\d{0,3}C\[0K", "", RegexOptions.IgnoreCase);
will see if have any negative impact but looks perfect for now :D

C# SSH Create.Command Newline Syntax

I'm definitely new to C# but learning it to pickup where another left things at work so be gentle if I ask silly questions (I think this is an easy one). I'm using SSH.NET and the current code isn't wrong but the way the previous guy wrote it can leave open ssh connections on the other side.
Current working code opens the SSH session as normal but uses something like this to run commands on the remote system:
Stream.Writeline("part of the command")
Stream.Readline();
Stream.Writeline("second part of command")
Stream.Readline();
Stream.Writeline("last part of command")
Stream.Readline();
This works and gathers the data exactly as it should but I've seen repeated times where the SSH connection stays open afterwards on the remote system. What I would like to do is send this all within a single command via create.command like:
command1 = client.CreateCommand("full command to run")
result = command1.Execute()
My problem is that the command I'd like to run on the remote system requires returns to run, it's a database command:
mysql -u user <<EOF
select COUNT(*) from table1
go
EOF
What I'd like to do:
command1 = client.CreateCommand("mysql -u user <<EOF\nselect COUNT(*) from table1\ngo\nEOF\n")
How/can one insert returns into the Create.Command? I'm assuming so, see no reason why not but I'm having a hard time finding the correct syntax to send a database command that requires returns after each line like above via the Create.Command. I've tried using \n for newlines but 99% sure I'm using it incorrectly.
And just to be clear I used mysql as the DB example, it's actually a sybase database but uses that syntax to connect.
Why do you need the breaks in the first place? They're just to nicely format the query for humans. MySQL isn't human. It couldn't care less. why not just do it this?
mysql -u user -e 'select count(*) from table1'
you don't need a go command, period.
If you DO want to send multiple commands, then you still don't need any line breaks, just a ;:
mysql -u user -e 'command1; command2; command3; ....'
USE \r\n like in here:
command1 = client.CreateCommand("mysql -u user <<EOF\r\nselect COUNT(*) from table1\r\ngo\r\nEOF\n")

Command Line through C# -- commands not executing

I'm using C# and AWS's CLI for S3. I seem to be having the same or a similar issue in two different places. In the first, I'm trying to execute multiple commands with the same process. The first command is to set the environment variables for the access key and secret key for AWS S3. The next two commands are just a simple ls to see what's in the bucket already and a sync to update its contents. If I take the commands to set the environment variables out, it works fine (because I'm already configured on the machine I'm working on), but since I need this to work on other machines, the environment variables need to be set. However, if they are, the following commands do not execute. Here's code for this:
String commands = #"set AWS_ACCESS_KEY_ID=keykeykey set AWS_SECRET_ACCESS_KEY=secretsecretsecret";
commands += #" & aws s3 ls s3://bucket";
commands += #" & aws s3 sync C:\test s3://bucket/kaaaaay";
The issue reappears in a different method. There, I need to get the value from a registry key. I know the command is correct, because it works just fine in a command line window. Here's code for that:
String commands = #"Reg.exe QUERY HKEY_LOCAL_MACHINE\Software\GIJOE\HASAKEY";
Any help would be much appreciated, because I'm stumped.

Configuring a Windows Protocol in a VS2010 Deployment Project

I have an application that is called in a command line fashion. I wish to associate a protocol with it, let's call that protocol runapp. runapp will call runapp.exe.
I plan to have this configured through the Registry portion of the application Deployment Project. I started by looking at other protocols, specifically mailto. I noticed that I need the following:
a new key under HKEY_CLASSES_ROOT called runapp
a new series of keys to create HKEY_CLASSES_ROOT\runapp\shell\open\command
under the runapp key, I will add two strings, (Default) with value URL:runapp protocol and a second string URL Protocol and no value
also under the command key, I need to add one string, (Default) with value "C:\path\runapp.exe" "%1"
I set all these up just fine. When I actually run the installer, though, before my (Default) strings, there is already another valueless (Default) string. So it looks like I am settings a (Default) string in the wrong way. What's the right way?
Secondly, I manually hacked this incorrect key in regedit after backing everything up. Now, with my keys looking very much like mailto's, I finally stop getting the "No program found" error, but my application actually doesn't seem to execute at all.
Any thoughts on how to correctly set (Default) strings and further troubleshoot protocol issues? Thanks.

FtpWebRequest Connecting to an AS/400

I need to download some files via ftp from an old AS/400 server. My code looks more or less like:
FtpWebRequest _request = (FtpWebRequest)WebRequest.Create("ftp://ftpaddress/FOO.CSV");
_request.Credentials = new NetworkCredential(_ftpUsername, _ftpPassword);
_request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)_request.GetResponse();
However, an exception is being thrown with the message:
501 Character (/) not allowed in object name.
I'm guessing the AS400 uses a different path separator than / but I can't figure out how to phrase the uri in a way (1) FtpWebRequest accepts and (2) the AS400 understands.
Anyone else bumped into this?
According to this page, fwd slash is the path separator character:
The forward slash is the separator character for paths sent to the FTP server.
A similar conversation over at Microsoft's forums (2005 era) indicates it's a bug in FtpWebRequest:
Currently FtpWebRequest does not support quote and I cannot think of a way you'll be able to overide the method without exposing our code Mariya Atanasova [NCL]MSFT, Moderator, Nov 2005
Try updating to the most recent versions or try a different library; the MS forum thread has several.
I've had this message often in the past, and it meant that I forgot to change the name format.
There are two name formats possible when doing FTP with an AS400, and it can be changed with the FTP command NAMEFMT:
0 is for the library system files (library\filename.member)
1 is for the files in the IFS, where a CSV file would be
By default, it is set to 0.
Change it to 1 and it should work. However I'm not sure how it can be changed with a FtpWebRequest.
To make life a little bit easier, the FTP server decides what NameFormat you want to use, based on your first command. If you start with "cd /home", then the FTP server does automatically set NAMEFMT to 1 for you.
Indeed, you can change this manually during your session with the remote FTP command NAMEFMT. Please, notice that you don't need the (old) iSeries way. You can address EVERY object on the iSeries with NAMEFMT 1. For example, "get /QSYS.LIB/MYLIBRARY.LIB/MYFILE.FILE/MYMEMBER.MBR" will do the trick for any iSeries database table. Even for multimember files!
This is an aggregate answer from the ones previously provided, but I was able to get this working by using the following structure:
ftp://[HostName]/%2F/[directory]/[subdirectory]/[filename].csv
The '%2F' was required and serves as a separator between the host name and the path.

Categories

Resources