简体   繁体   中英

Execute the batch file on the server side after uploading it: 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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM