远程控制防火墙后的内网服务器一例

某单位实施项目后,需要部署两台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
            { 
            
            }
        }
    } 
 

 

 

 

外网服务器上运行的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;
            }
        }
    }

 

 

posted on 2010-04-18 19:32  谷夕  阅读(1545)  评论(2编辑  收藏  举报

导航