简体   繁体   English

上传批处理文件后,在服务器端执行它:C#

[英]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). 我使用C#(FtpWebRequest)中的FTP将批处理文件传输(上传)到服务器。 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?? 我可以使用Process.Start()方法还是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 . 上一次我需要远程复制和运行批处理文件时,我使用了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). 您可以尝试使用SITE FTP命令运行该文件,但这是特定于服务器和操作系统的(即,您无法在Unix上启动.BAT文件或在Windows服务器上启动.sh文件,而且并非所有服务器都支持执行)通过SITE命令访问外部命令)。

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). 如果这样做没有帮助,则您必须在远程端设置一些服务器(例如SSH服务器),并且在通过FTP上传后,使用SSH协议连接到该SSH服务器(但是如果您使用SSH,则不需要FTP-您可以改用基于SSH的SFTP)。

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. 这样做的好处是,它非常简单,它使用了非常可靠的Windows调度程序,不需要第三方应用程序。

Nayan Soni For Execute Bath File System.Diagnostics.Process.Start(FileName, Parameters); Nayan Soni用于执行Bath文件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. 对于套接字参考: http : //www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-使用此示例http://diaryanakku.files.wordpress.com/2013/ 01 / socket.jpeg (将.jpeg文件更改为.7z)并提取。 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. 我认为这是第二选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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