`
net_liu
  • 浏览: 231372 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

利用网页强制重启服务器

阅读更多

利用网页强制重启服务器

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
namespace WebApplication1_advapi
{
  /**//// <summary>
  /// WebForm1 的摘要说明。
  /// </summary>
  public class WebForm1 : System.Web.UI.Page
  {
    protected const int SE_PRIVILEGE_ENABLED = 0x2;
    protected const int TOKEN_QUERY = 0x8;
    protected const int TOKEN_ADJUST_PRIVILEGES = 0x20;
    protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    protected const int EWX_LOGOFF = 0x0;
    protected const int EWX_SHUTDOWN = 0x1;
    protected const int EWX_REBOOT = 0x2;
    protected const int EWX_FORCE = 0x4;
    protected const int EWX_POWEROFF = 0x8;
    protected System.Web.UI.WebControls.Button Button1;
    protected const int EWX_FORCEIFHUNG = 0x10;
    [StructLayout(LayoutKind.Sequential, Pack=1)]
      protected struct LuidStruct
    {
      public int Count;
      public long Luid;
      public int Attr;
    }
    [DllImport("kernel32.dll", ExactSpelling=true)]
    protected static extern IntPtr GetCurrentProcess();
    [DllImport("advapi32.dll", SetLastError=true)]
    protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
    [DllImport("advapi32.dll", SetLastError=true)]
    protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
    [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]
    protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);
    [DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]
    protected static extern bool ExitWindowsEx(int flg, int rea);
    protected static void DoExitWindows(int flg)
    {
      LuidStruct tp;
      IntPtr hproc = GetCurrentProcess();
      IntPtr htok = IntPtr.Zero;
      OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
      tp.Count = 1;
      tp.Luid = 0;
      tp.Attr = SE_PRIVILEGE_ENABLED;
      LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
      AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
      ExitWindowsEx(flg, 0);
    }
    public static void Shutdown()
    {
      DoExitWindows(EWX_SHUTDOWN);
    }
    public static void Reboot()
    {
      DoExitWindows(EWX_REBOOT | EWX_FORCE);
    }
    public static void Logoff()
    {
      DoExitWindows(EWX_LOGOFF);
    }
    [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=false)]
    protected static extern bool InitiateSystemShutdown(string name, string msg, int timeout, bool force, bool reboot);
    private void Page_Load(object sender, System.EventArgs e)
    {
      // 在此处放置用户代码以初始化页面
    }
    Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
      //
      InitializeComponent();
      base.OnInit(e);
    }
    /**//// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {  
      this.Button1.Click += new System.EventHandler(this.Button1_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void Button1_Click(object sender, System.EventArgs e)
    {
        InitiateSystemShutdown(null,null,0,true,true);
    }
  }
}

 

分享到:
评论

相关推荐

    远程重启服务器软件-适合于局域网内服务器

    "远程重启服务器软件"就是为了解决这种问题而设计的工具,尤其适用于那些无法通过常规远程控制服务进行登录和管理的场景。本文将深入探讨远程重启服务器的原理、应用场景及安全考虑。 首先,我们要理解远程重启的...

    win2003 服务器超出最大连接+强制重启服务器(方法)

    如果已连接的用户数量达到最大连接数限制,尝试用管理员权限登录服务器,然后使用“query user”命令查看当前所有用户,利用“logoff”命令强制踢掉管理员帐号登录的ID,从而使得本机连接到0个用户。 第三种方法是...

    巧定计划 让服务器定时重启

    在Windows Server 2003系统中,可以使用内置的`shutdown`命令来远程重启服务器。通过远程桌面连接到服务器,然后在命令提示符下输入`shutdown`命令,结合不同参数如`-r`(重启)、`-f`(强制关闭程序)和`-t`(设置...

    linux重启服务器的tomcat

    ### Linux重启服务器上的Tomcat知识点详解 #### 一、引言 在Linux环境下管理服务器时,经常需要重启Tomcat服务来更新应用或者处理故障。本文将详细介绍如何在Linux系统下重启Tomcat服务的过程,包括登录服务器、...

    服务器关机、重启、注销命令

    本文将深入解析Linux环境下,管理员如何利用一系列命令来实现对服务器的有效管理,特别聚焦于`shutdown`、`halt`、`init`及`logout`等核心命令。 #### 关机与重启命令详解 **1. `shutdown`命令** `shutdown`命令...

    服务器管理-windows服务器如果让服务器自动定时重启.pdf

    例如,`shutdown -g -f`是常用的重启命令,其中`-g`表示重启,`-f`表示强制关闭未响应的应用程序,以确保顺利重启。 2. **创建批处理文件**:新建一个记事本,输入你的重启命令,如`shutdown -g -f`,然后将文件...

    Linux服务器的安全隐患

    任何能够接触到服务器键盘的人都可以通过此组合键强制重启服务器,从而中断服务或导致数据丢失。 **示例**: 如果非法用户能够接触到服务器的键盘,他们可以通过按下“Ctrl+Alt+Del”组合键来重启服务器,进而中断...

    定时强制关机精灵

    同样,定时重启则有助于更新和维护,尤其在服务器环境中,定期重启可以清理内存中的临时文件,保持系统稳定运行。 注销功能则是在保留用户账户设置的同时,结束当前用户的所有活动,将用户从登录状态切换到登录界面...

    用C#实现远程关机,重启

    - `2 + 4`:强制重启 - `8`:关闭电源 - `8 + 4`:强制关闭电源 示例代码片段如下: ```csharp using System; using System.Management; public class RemoteShutdown { public static void Main(string[] args)...

    高效管理Windows服务器三大法宝.docx

    如果补丁支持`-z`参数,表示安装后无需强制重启。 - 使用`aaa -z`命令安装补丁,系统将不会要求重新启动。 2. **取消对服务器的限制访问** 默认情况下,Windows 2021 Terminal Server限制每个用户只能进行一个...

    Win7搭建局域网NTP服务器.docx

    本文档将详细介绍如何利用Windows 7系统搭建局域网内的NTP服务器,以及在搭建过程中可能遇到的问题及解决方案。 #### 二、基础知识 1. **NTP协议**:NTP协议的主要功能是使计算机时间同步化,它可以使计算机对其...

    定时自动关机或重启

    在IT领域,定时自动关机或重启是一项常用且实用的功能,尤其对于服务器管理和个人电脑的维护。这个小工具能够帮助用户根据预设的时间计划自动执行关机或重启操作,从而节省能源、保护设备或者在无人值守的情况下完成...

    华盾服务器管理专家 3.0.3.78.rar

    硬盘占用、在线重启IIS、在线重启服务器、进程查看、网络连接查看、系统服务查看、端口管理、启动项查看、远程桌面管理...功能如此之多以致不能一一列出。   安装简单便捷   不需要复杂的手工配置,不需要额外...

    定时关机或重启 某时某刻某秒定时关机或重启

    本节将详细介绍如何在Windows操作系统中实现定时关机和重启的功能,以及如何利用提供的"TimeForDownWindows.exe"程序进行设定。 一、Windows内置定时关机命令 Windows操作系统本身提供了名为“shutdown”的命令行...

    重启时使用代码

    - **故障恢复**: 当某些服务或应用程序出现故障且无法正常关闭时,可以通过这种方式强制重启计算机。 - **软件安装/更新**: 在软件安装或更新过程中,为了确保所有更改都能生效,可能需要重启计算机。 ### 总结 ...

    win2008服务器安全设置集锦.pdf

    4. 关闭“网络发现”选项,点击“应用”,然后重启服务器,这样服务器就不会出现在其他电脑的网络列表中。 二、关闭高风险端口 开放的端口是黑客攻击的入口,尤其是139和445等常见被利用的端口。关闭这些端口可以...

    DynCache服务器释放内存

    7. **定期重启**:如果其他方法无法有效解决内存问题,定期重启 DynCache 服务器可以强制释放所有内存,恢复到初始状态。 在提供的压缩包文件中,"Microsoft Windows Dynamic Cache Service EULA.docx"可能是 ...

    IBM服务器(366-1rc,345-i09)操作系统安装.docx

    首先,需要将Server Guide光盘插入,然后重启服务器。在安装过程中,用户需要按照提示操作,但需要注意的是,这个过程中会自动删除硬盘上的所有分区,导致数据丢失。因此,务必在安装前备份重要数据。此外,Server ...

Global site tag (gtag.js) - Google Analytics