.Net class to execute remotely on Linux over SSH? - c#

I'm not sure where to get started with simply executing a remote script and returning the output to a web form. In this case we have an application server that executes some 3rd party tools. We'd like to write an interface for our power users that allow them to check the status of various processes and also execute some of the 3rd party admin tools that support has available. The issue is that we only connect to this Linux (RedHat) machine via Putty over SSH. I looked into PsExec but it doesn't seem to handle SSH. I'd like to avoid any large security holes, if possible, but this is intended to be a limited number of users in a closed network.
Thanks for any assistance getting started!

There are several options to do SSH in .NET - although you need a library for that since nothing is built-in:
http://www.codeproject.com/KB/IP/sharpssh.aspx
http://www.rebex.net/ssh-shell.net/ (commercial)
https://www.eldos.com/sbb/net-ssh.php (commercial)
http://sshnet.codeplex.com/
http://granados.sourceforge.net/

Related

Interacting with the Chrome V8 debugger protocol through .NET (ClearScript)

I'm maintaining an enterprise C# application that provides end-user extensibility through user-defined JavaScript functions (with a select few C# types/proxies exposed) (think financial algorithms).
We evaluate those functions using ClearScript (prevously we were using another library called Noesis however we've moved away from that).
I'd like to offer a way for end-users to perform very basic debugging of the scripts (through a web interface). At a minimum - inspect variables and step over/into functions.
I've successfully connected to the ClearScript engine w/ Chrome when running the app locally and have been able to step through the scripts - however I don't think the same would make sense for end users whilst running in production for various reasons:
Might be too tricky for them to set up
Possible difficulties with port allocation
Probable firewall issues
What I'd really like is a SignalR interface to the V8 debugger - the web-app would interact with the SignalR, and SignalR would interact with the V8 debugger.
Has anyone done something along these lines before?
Are there any relevant .NET libraries I should be looking at?
Am I right in my thoughts that this seems like a fairly big piece of work?
Any other advice?
It's a web-socket based protocol - though you must make a request via HTTP to retrieve the tabId first (request to /json/list)
https://chromedevtools.github.io/devtools-protocol/1-2/Debugger describes the protocol - there are different revisions targetting different versions of Chrome but 1.2 (stable) seems to work w/ the version of ClearScript I used.
As well as request-response (command) type interactions on the websocket, V8 will also publish events - e.g. Debugger.scriptParsed & Debugger.paused on the same socket.
Some of the key commands:
Runtime.enable
Debugger.enable
Runtime.runIfWaitingForDebugger
Debugger.getScriptSource
Debugger.resume
Debugger.evaluateOnCallFrame
Runtime.getProperties
I've written a small demo project https://github.com/flakey-bit/ClearScriptDebugging/ that shows a target process being debugged by another process.
If you mentioned Chrome and Nodejs, You maybe know that Nodejs, Chrome and Edge support WebSocket naturally. Chrome and Edge better with all security options. For Nodejs you can find standard libraries like socket.io
For the server side DotNet WebSocket
WebSocket protocol is linga franka today.

c++/c# app to connect to linux server

I am planning to do an App either in c++/C# (through Visual Studio) in Windows 7.
OK what this app does is it will accept input through studio elements like text box or buttons ..then it should create an command based on input and run these command on the Linux server.
E.g : One simple example is to restart a certain process on a Linux machine
Now to proceed to my problem:
How do I connect my App to Linux machine?
I don't want to install any third party application as i am not allowed to do so.
If possible please list me the different ways i can do this
Well, you can do this. I am already familiar with this scenario. I have several application setup working this way. The only option that will allow you to do this without any 3rd party application is using SSH protocol.
The SSH protocol is the well known and most power full tool of linux sysadmins. You can execute any command over it on a linux box from any part of the world or any piece of shit which can run an SSH client. Ask me on comment if you need more info on this.
EDIT
One example, if you want to restart certain process.. You can pass the command over SSH as below.
ssh user#server_ip '/etc/init.d/httpd restart"
The above will restart the HTTPD(Apache) service.
Some libraries you can use in .NET
http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET
https://sshnet.codeplex.com/
I have not used above libraries in .Net, Instead I have done several things using linux box with python and bash.
if I understand correctly, to solve this you can implemente WebService.
But when you said :
I dont want to install any third party application as i am not allowed
to do so
I'm afraid you can not setup WebService on Linux server ?
Other possibility, use SSH if already install on linux server (some chance that is) and you probably could find a .Net lib to make SSH
This link will help .Net class to execute remotely on Linux over SSH?

Reading PuTTY output with c#

What i want: I want to read the Output of the PuttY Window with C#
What I've got: Our company has several hundreds of servers with at least 2-3 technical users (which are running applications). We got a database of all the users and passwords. So it's basically search, copy and paste to login.
What i want to do: Write a piece of software that does this automatically.
How far i am: Wrote a tool, that reads the logfile of a single PuTTY instance and looks for the password prompt. Determinates target user and server (based on current server and su - [username]). Retrieves the password and sends it via PostMessage to a selected PuTTY.
The Problem or what i want to change:
I want to be able to read the PuTTY output directly from the PuTTY window, because the logfile is kind of unreliable (TAB, ESCAPE, etc which scrambles the output).
I have used UISpy and other tools to get a control, but no luck.
I don't want to use a keylogger mechanism or something like that.
Maybe a hook or something, but have never done that before.
Additonal Info:
Connection is made over a JumpServer, only ssh connections from there to the target servers are allowed.
Direct login with the tech. user is disabled.
The PuTTY window is already opened and used to work on the server.
Writing a whole new Connection Manager is no option. Coworkers are familar with PuTTY and this solution should be some sort of standalone background worker.
Additonal Info 2:
The goal is to write a strict 3rd party software. Not to use other SSH libs, modify PuTTY source or other approaches. The question is: how to read text from the PuTTY window, beside the logfiles.
I don't know why this hasn't been suggested yet, but plink (which is part of the PuTTY suite) is the command-line version of PuTTY, you'll just need to redirect stdin and stdout to get a relatively powerful (as in features, you'll still need to interpret telnet stuff yourself) SSH client.
Have you considered using OCR?
Sketch of solution would be:
1 - Agent runs waiting to notice a Putty Window (either register a callback with OS for new processes or periodically check the list of running processes)
2 - When a putty is noticed, agent takes screenshot and extracts portion of screen occupied by putty. You would need to extract window location, but can be done via OS calls assuming you have a handle from step 1
3 - Pump this image data into tesseract or something, and get text output back. check to see if the password prompt is there
4 - If prompt is there, it sounds like you had the rest done after this (send info needed via PostMessage)
You should take a look at This Link (Putty StdIn / StdOut. Shows how to open putty from a c# app and how to handle Standard In, Out and Error Output to communicate with your putty instance.
If you are using putty as an SSH tool, use http://www.routrek.co.jp/en/product/varaterm/granados.html C# SSH library
If you are using putty as a serial tool use the inbuilt IO classes
Telnet also has C# libraries, none that I can give any knowledgabel input into what is good.
If you can purpose a modified version of putty, you should modify putty sources and including some pipes (or socket) that communicates with your c# application. Like that, your coworker keep the same software.

.Net TCP vs WCF vs Powershell

This has been asked a lot, but I'm after more specific answers.
I'm designing/developing a suite of applications that are dependent on communicating with a agent and a manager. They will be communicating commands (from manager to agent) and statistics (agent to manager). At the moment, this suite of applications will only run on the Windows platform, but eventually should scale out to other systems. Some places where this application would be used might not want to install a great load of applications across their systems, and some might not want the manager side sitting there sucking up juice (analysis!) and therefore might want it in the cloud.
So, I know that I can use .Net TCP sockets, which seems to have good raw performance and allows me a great deal of flexibility.
I also know that I could use Windows Communication Foundation, which seems like naturally a better choice.
But, seeing as I'm sending commands and receiving statistics, I could just use Powershell to connect remotely and use the plethora of commands available from what would be the server, ruling out writing a client application full stop.
Bearing in mind that these applications (client especially) should just sit there, be quiet, do their job and not interfere with general operations, which would you suggest to be better?
If you need more clarification I'll be happy to do so!
Thanks.
The way you discribe your application, I would reformulate it as an 'agent' located on clients and a manager located on the server. If it's so the manager sends commands to the agents and the agents respond or store statics available to the manager.
For such an architecture you can imagine using WMI, with a WMI provider on the agent, and wathever you want on the server. Powershell can be used on the server to query your agent. In the near future you can use the same architecture puting your agent on a Linux box with NanoWBEM on the top of WS-Man Protocol (see Standards-based Management in Windows Server “8”) .
They will be communicating commands (from server to client) and
statistics (client to server). At the moment, this suite of
applications will only run on the Windows platform, but eventually
should scale out to other systems.
In your description there is one requirement that is not compatible with Power Shell.
Power Shell works only in Windows world.
WCF service should not consume any resources if they are properly configured. IIS and WAS are able to load services on-demand (upon getting request from client) and unload when it is not necessary.
http://blogs.msdn.com/b/blambert/archive/2009/02/13/enable-iis.aspx
http://msdn.microsoft.com/en-us/library/ms731053.aspx
TCP sockets are the best from performance point of view but unfortunately implementation will require lots of extra DEV and QA work to implement all plumbing that already exists in WCF. When you finish that work you will have one more "bicycle" that is not compatible with industry standards.
My vote is WCF.

C# - command line application calls

I have done plenty of C# shell command calls, apps, batch files etc. The other day I was asked if it would be a problem if an executable that I currently run from my web site app, would move to another server on our intranet.
In other words the web site app and the executable that I am running through Process.Start(...) are located on the same box currently - all good there. Now there is a wish to separate the two on two different servers.
I done a few futile attempts to execute an app (located on server B) from server A (where the web site resides).
Is there a way that I have not run cross yet to do this ?
Thanks
PsExec is one way with minimal coding. Using System.Diagnostic.Process, you can call this command:
psexec \\ServerB (path)\myapp.exe arg0 arg1 ...
To control the processes of server A by running an application on server B, you would need an application running on server B that would get controlled remotely somehow.
As an example, let's say server A runs unix, so you could write a application that would connect to server A using ssh, authenticate and then control the processes and whatnot like you do in a shell. If server A does not allow ssh connection, you could write your own application that would be running on server A listening to some connection and commands that would come from an application in server B.
It's quite hard to understand what are your current settings and why would you even want to switch the application from server A to server B, so a little more information would wield you a better answer.
Austin's PSExec approach is probably the easiest approach to executing an EXE on a remote machine, but you may want to consider a potentially more robust solution:
You could modify your command-line app to run as a service and to respond to requests for work and/or data via a WCF (binaryXML/TCP or XML/HTTP) call.

Categories