远程控制防火墙后的内网服务器一例
某单位实施项目后,需要部署两台Web服务器,网络拓扑大概如下
因为系统刚刚开始使用,有时候Web应用修改后,需要更新内部Web服务器的文件,但因为无法远程更新,于是采用了以下办法:
因为内部Web服务器可以访问外部的Web服务器1433端口(数据库安装在外网的Web服务器) ,于是在内部Web服务器安装一个服务器程序,定时(5秒)从数据库的CMD表中取出命令行,如果命令不为空,则执行命令,把执行结果写回数据表。另外建一image的字段,用于存放更新文件的二进制值,还有一个路径字段,定时服务如果检测到有文件需要更新,则可以把image字段内容取出,写入到特定路径中,实现文件更新。
示例代码:

public static class Cmd
{
static string Execute(string cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit(10000);
p.Close();
return strRst;
}
public static void Execute()
{
try
{
CmdDataClassesDataContext ctx = new CmdDataClassesDataContext();
CMD cmd = ctx.GetTable<CMD>().First();
if (cmd!=null)
{
string cmdString = cmd.COMMAND;
if (!string.IsNullOrEmpty(cmdString))
{
string res = Execute(cmdString);
cmd.COMMAND = string.Empty;
cmd.CMD_RES = res;
}
}
if (cmd.UPDATE_FILE!=null && cmd.UPDATE_FILE.Length != 0)
{
byte[] file = cmd.UPDATE_FILE.ToArray();
string path = cmd.FILE_PATH;
try
{
FileStream fs = new FileStream(path, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(file);
bw.Close();
fs.Close();
}
catch { }
cmd.UPDATE_FILE = null;
cmd.FILE_PATH = string.Empty;
}
ctx.SubmitChanges();
ctx.Dispose();
}
catch
{
}
}
}
{
static string Execute(string cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit(10000);
p.Close();
return strRst;
}
public static void Execute()
{
try
{
CmdDataClassesDataContext ctx = new CmdDataClassesDataContext();
CMD cmd = ctx.GetTable<CMD>().First();
if (cmd!=null)
{
string cmdString = cmd.COMMAND;
if (!string.IsNullOrEmpty(cmdString))
{
string res = Execute(cmdString);
cmd.COMMAND = string.Empty;
cmd.CMD_RES = res;
}
}
if (cmd.UPDATE_FILE!=null && cmd.UPDATE_FILE.Length != 0)
{
byte[] file = cmd.UPDATE_FILE.ToArray();
string path = cmd.FILE_PATH;
try
{
FileStream fs = new FileStream(path, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(file);
bw.Close();
fs.Close();
}
catch { }
cmd.UPDATE_FILE = null;
cmd.FILE_PATH = string.Empty;
}
ctx.SubmitChanges();
ctx.Dispose();
}
catch
{
}
}
}
外网服务器上运行的form代码:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
ctx.CMD.First().COMMAND = textBox1.Text;
ctx.SubmitChanges();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(textBox4.Text))
{
MessageBox.Show("请输入远程路径");
}
else
{
if (File.Exists(textBox3.Text))
{
try
{
byte[] b = File.ReadAllBytes(textBox3.Text);
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
CmdLib.CMD c = ctx.CMD.First();
c.UPDATE_FILE = b;
c.FILE_PATH = textBox4.Text;
ctx.SubmitChanges();
}
}
catch { }
}
else
{
MessageBox.Show("文件不存在");
}
}
}
catch(Exception ex)
{
MessageBox.Show("上传出错!" + ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fg = new OpenFileDialog();
if (DialogResult.OK == fg.ShowDialog())
{
textBox3.Text = fg.FileName;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
textBox2.Text = ctx.CMD.First().CMD_RES;
}
}
}
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
ctx.CMD.First().COMMAND = textBox1.Text;
ctx.SubmitChanges();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(textBox4.Text))
{
MessageBox.Show("请输入远程路径");
}
else
{
if (File.Exists(textBox3.Text))
{
try
{
byte[] b = File.ReadAllBytes(textBox3.Text);
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
CmdLib.CMD c = ctx.CMD.First();
c.UPDATE_FILE = b;
c.FILE_PATH = textBox4.Text;
ctx.SubmitChanges();
}
}
catch { }
}
else
{
MessageBox.Show("文件不存在");
}
}
}
catch(Exception ex)
{
MessageBox.Show("上传出错!" + ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fg = new OpenFileDialog();
if (DialogResult.OK == fg.ShowDialog())
{
textBox3.Text = fg.FileName;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
using (CmdLib.CmdDataClassesDataContext ctx = new CmdLib.CmdDataClassesDataContext())
{
textBox2.Text = ctx.CMD.First().CMD_RES;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?