Execute the batch file on the server side after uploading it: C# - c#

I transfer (upload) the batch file to the server using FTP in C# (FtpWebRequest). Batch files have some commands. My requirement is to transfer the batch file to the server (which I am able to do) and execute that batch file.
How do I execute the commands in the batch file on the server from my client? Can I use Process.Start() approach or rsh?? Which is better way of doing it and a link to the example would be appreciated.

Last time I needed to copy and run a batch file remotely, I used PSExec. However, that needs administrator rights on the remote machine.

You can try to use SITE FTP command to run the file, but this is server- and OS-specific (i.e. you can't start a .BAT file on Unix or .sh file on Windows server, and also not all servers support execution of external command via SITE command).
If this doesn't help, you would have to setup some server (eg. SSH server) on the remote side, and after uploading via FTP connect to that SSH server using SSH protocol (but if you use SSH, you don't need FTP - you can use SSH-based SFTP instead).

You could run a scheduled task (say every five minutes) that runs your batch file. You might have to do some clean up tasks like remove the batch file after you have run it (so it doesn't inadvertently run again).
(If your batch file had a different name every time...might have to rethink this a bit.)
Nice thing about this is that it is very simple, it uses the very reliable Windows scheduler and no 3rd party applications are required.

Nayan Soni For Execute Bath File System.Diagnostics.Process.Start(FileName, Parameters);
And For Socket Ref : http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using
this sample http://diaryanakku.files.wordpress.com/2013/01/socket.jpeg (change file .jpeg to .7z) and Extract. sorry for limitation.

another choice you can use socket programming, when bath file finish to transfer, send command to server for execute bath file. I think this 2nd Choice.

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 remotely run an ACCESS query on a DB located on an FTP server?

I want to run a sql query on ms-access database that is located at ftp server and get only the result back of that query in my application written in c#. Is it possible?
No. JET must be able to open the file via a standard SMB or local location.
See if you can map the FTP location to an SMB type share using some tool first perhaps. Or copy the file locally.
Edit: A possible workaround (based on #baconsah's answer)
You could actually improve the #baconsah's design, by writing a file to the FTP server. Then have a process on the remote server that picks up the write and which then does the query at that point and making the the results available on the FTP output. You lose the latency but increase the complexity.
A horrible alternative is having an access database run the query on the remote system, on a timed job basis, and output the results to a file in that ftp directory.
You could use the FTP command quote or literal to execute a program (or VBS script) on the FTP server machine.
Your "solution" could work as follows:
Transfer the SQL command text as file to the FTP server
Run the remote command which executes the SQL command on the ms-access DB
Transfer the result of step 2. back to your local machine
As mentioned before, this szenario is unreliable, slow, unsecure, needs error handling, ...

Send a xml file from windows to ubuntu

Visual Studio 2008
I have a xml file I want to send to a ubuntu server.
Before I was using the following to save on the current machine in the application folder. However, the customer has changed and now wants to store it on a remote network ubuntu server.
dt.WriteXml(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Config.xml"));
What is the best way to send files across the network? Is it possible to send files from a windows machine to a ubuntu server machine?
Many thanks for any advice,
The easiest solution is probably to use a samba daemon and plain old file copying. If your customer wants to use the Ubuntu server as a file server, they may have already set this up.
Another option is to run an FTP daemon on the server and use the WebClient class to upload the file to it. You can use the WebClient.UploadFile method to upload a file to the server or WebClient.UploadString to upload the string directly, using the FTP STOR command.
The easiest thing to do is probably to SCP it onto the remote machine.
Run a samba daemon on the ubuntu server and you can use shared folders (SMB) to transfer the file.

File.Copy from Unc path to (same server's) Unc path inquiry

Will data traffic go through the host application program or will be dealt remotely in scenarios where C#'s File.Copy is used:
File.Copy(#"\\SERVER13\LOL\ROFL.txt", #"\\SERVER13\ROFL.txt")
Cheers n thx!
First of all you have a small bug in the path of the destination file.
Second, there are no remote copy operation. There exist a remote move operation (rename, but with a destination in other directory) like MoveFile (see native API http://msdn.microsoft.com/en-us/library/aa365239%28VS.85%29.aspx).
UPDATED: Probably you came from unix and knows utility rcp, but it works with respect of remote shell service (rshd) and not with respect of direct file system features. You can also use PsExec utility from SysInternals (see http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to start some program on the remote computer, but all this is not a subject of programming.
It will go through the local application. The file system does not know what the application is going to do with the bytes it reads from the share, or where the bytes that are written to the share come from.
In addition, the application does not know (in the case of DFS) if the two shares are on the same machine.
If you want to let the server handle it, you have to remotely run a copy program.

How to correctly write dynamic files to an FTP server?

I'm using C# and i have written a locally installed application that dynamically generates files which need to be on an FTP server.
Do i generate them to disk then upload them to the FTP server? or is there a way to open a stream to an FTP server and write the files directly?
Check the code sample I gave in this answer, doesn't rely on writing to files. It's not SQL specific and was just a suggestion on how to use SQL CLR integration assemblies to upload output from sql queries to an FTP server. The for loop in the method is just to demonstrate writing to the FTP stream. You should be able to rework to you needs:
How to write stored procedure output directly to a file on an FTP without using local or temp files?
You should follow the class:
System.Net.FtpWebRequest
You will see that its arguments are streams and you can send data to them from any source.
When seaching for .Net capabilities you should be aware of the object browser for visual studio acessible in:
View > other windows > object browser
Is supplies a search over all known assembly .Net objects.
The better way is to save file locally, and upload it later, since there could be problems with upload process.
Since you are using c# I'm thinking maybe you are in a Windows Env. Something I know little about :)
If you are dealing with a unix env, you could just pipe your output thru SSH, which would also take care of encryption overhead.

Categories