Connecting putty (or plink) to DaHaus - c#

For school we need to build a webinterface which interacts with the program DaHaus (a sort of autonomation program).
As of now I first made contact simply with putty and that worked.
However I found out that I couldn't make that connection automated since putty had an UI.
Then I tried plink which could connect to the program but once it did I couldn't type a single thing.
How to connect: telnet with port number 11000 and localhost or 127.0.0.1
I am wondering if its possible to make plink do the commands in a way that works or how to do it with putty without the shell since it needs to be automated.
Also the code we need to make it in is c# with visual studio in asp.net

https://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library
using this library you can set up a telnet connection with whatever program you want. For using it inside a project simply copy the telnetinferface.cs file to the correct folder inside your project.
within your code simply state using TelnetInterface;

Related

Is there a way to run a shell script on a linux file server from a c# console application

I have a directory full of DICOM on a linux file server. I have the directory mapped as a network drive and have created a c# console application for modifying the DICOM tags, modifying DICOM tags is something I must do before I can import to our database.
When I am in the server I can run the following to import everything from this directory to my database:
for x in `ls -d1 *`;
do
dcmbase_import -U p_mfdid1 -n -l info -i "$x:spiromics:file:H:SPIROMICS";
done
I would like to know if there is a way that I can run the above code from within the c# application. For instance, is there a way to access the linux file server and execute a shell script?
Currently I have to run my c# application to change heads and then manual access the server and run the shell script. I would love to have this all encompassed in one application so that I could run it at the end of the day and have it processing and importing over night.
You can't run the shell script over the network share as that would run it on your local machine and you need to run it on the Linux machine.
The common method for executing code remotely is by running it with SSH or invoking with RPC. I would avoid RPC if possible because even though it's more powerful it also needs more complicated setup.
From Karl-Johan Sjögren's answer on this question, C# send a simple SSH command, I was able to find a git project for SSH calls in .NET as well as a small stub that could get you started. You would change hostnameOrIp to your Linux server, and change the command to be run to your script.
SSH.NET
using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
client.Connect();
client.RunCommand("etc/init.d/networking restart");
client.Disconnect();
}

Can I port my C# (SQL Server) app to a different PC?

I have created a project for my college using C# and with SQL Server 2016 and SQL Server 2017 Management Studio.
It is possible that my teacher would want to test it on a different PC, so how could I run it on different PC?
The database I used was made on a local server in my laptop.
Is it possible to port it to another PC?
Sorry if port is a bad word for this but its the best thing I can come up with.
Click here for image
Port is the wrong word, but I do think I understand what you want to do - "move" the application so he can run it on another endpoint.
Generally speaking:
- The other endpoint will need the same version of .Net you're dependent on
- He will need the same version of SQL Server running on his laptop
Step 1: Create a backup of your database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-a-full-database-backup-sql-server
Step 2: Create an archive (zip/rar, whatever) of Executable
If you haven't compiled it into a standalone EXE and have library dependencies, gather them all (normally in your build directory in your project folder)
Step 3: Move the .bak file and archive to the other computer
Step 4: Restore the database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/file-restores-simple-recovery-model
Notes:
As Osman Rahimi noted in the comment, you will need to make sure your connection string in your app isn't hardcoded to your IP or machine name. It is best to use localhost or 127.0.0.1 instead.
An alternative you might want to consider is putting the database IP into a variable you load in the .config file. That way you can concatenate it into your connection string and just instruct your teacher to make the change in the config file.
It could also mean you can leave the database on your workstations if it is on the same network and you have nothing blocking the required SQL Server ports between both endpoints.
The salient code/points of reference for the second point above are as follows:
//import statement to reference library
using System.Configuration;
//reference variable in your code (for your connection-string):
ConfigurationManager.AppSettings["SURVEYPATH_SERVER"].ToString()
//where to add variable in config file
<configuration>
<appSettings>
<add key="SQL_SERVER_IP" value="127.0.0.1" />
</appSettings>
</configuration>
To access to database on another machine
Step 1: both machines should be connected to same local network
Step 2: To share database on different machine please refer the following link
https://learn.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer
I hope this will help you
Simplest you can access it using IP address of your machine to browse it from different machine. make sure your firewall is open or at least the port 80 is open to be able to browse it from outside.
However if you want to copy all code to a different machine and run it from there you have the following options:
1) Do it manually by copying your code and do you configuration there which might take time and might even error prone especially if you miss some configuration. if you just want to copy code only and leave database in your machine, make sure your firewall is open (at least port 1433)
2) Another way if you are familiar with containerization, you can build and share docker's images and then run containers in the other machine easily.
Some articles:
Overview of .NET and Containers
Introduction to SQL Server Containers
Dockerizing Existing .NET MVC
3) Also you can use vagrant as a different solution to share your box and to download it there or through manual virtualization by developing your application from start (Virtualbox, VMware ..etc)

Renci.SshNet SshClient All commands are not working

I'm using SshClient from Renci.SshNet and i have noticed that all the commands are not effective on the remote server.
For example, that one works fine :
mySSH.RunCommand( "touch test");
will create a file on the remote server.
but this one won't do anything :
mySSH.RunCommand( "nohup dnx web &");
I searched all over the internet and i'm so disapointed cause nobody talk about that.
Mostly, all commands that are simple like create a file or a folder, but strong commands like executing a script or starting my application (the most important) just simply don't want to do anything.
Thank you in advance.
Can you run this command when you log in over ssh as the same user Ssh.Net is using?

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.

Categories