Ipsec IP Security Policy for win7 and ipsecwin7

Source: Internet
Author: User

Ipsec IP Security Policy for win7 and ipsecwin7

// Disable Windows 7 connection to public static void BannedWINRunCmd () {string str = Console. readLine (); System. diagnostics. process p = new System. diagnostics. process (); p. startInfo. fileName = "cmd.exe"; p. startInfo. useShellExecute = false; // whether to use the Operating System shell to start p. startInfo. redirectStandardInput = true; // accept the input information from the caller p. startInfo. redirectStandardOutput = true; // get the output information by the calling program p. startInfo. redirectStandardError = true; // redirection standard error False output p. startInfo. createNoWindow = true; // do not display the program window p. start (); // Start the program // create an Ip policy (block all connections) str = "netsh ipsec static add policy name = BannedConnectIP"; p. standardInput. writeLine (str); str = "netsh ipsec static add filterlist name = BannedConnectIP"; p. standardInput. writeLine (str); str = "netsh ipsec static add filter filterlist = BannedConnectIP srcaddr = me dstaddr = any"; p. standardInput. writeLine (str); str = "Netsh ipsec static add filteraction name = BannedConnectIP action = block"; p. standardInput. writeLine (str); str = "netsh ipsec static add rule name = BannedConnectIP policy = BannedConnectIP filterlist = BannedConnectIP filteraction = BannedConnectIP"; p. standardInput. writeLine (str); // run the configured IP address to access string StrIPArr = ConfigurationSettings. appSettings ["RemoteIPAddr"]; if (StrIPArr. contains (',') {string [] StrArr = StrIPArr. split (','); for (int I = 0; I <strArr. length; I ++) {string strarr = strArr [I]. toString (); str = "netsh ipsec static add filterlist name = AllowConnectIP (" + strarr + ")"; p. standardInput. writeLine (str); str = "netsh ipsec static add filter filterlist = AllowConnectIP (" + strarr + ") srcaddr = me dstaddr =" + strarr + ""; p. standardInput. writeLine (str); str = "netsh ipsec static add Filteraction name = AllowConnectIP ("+ strarr +") action = permit "; p. standardInput. writeLine (str); str = "netsh ipsec static add rule name = AllowConnectIP (" + strarr + ") policy = BannedConnectIP filterlist = AllowConnectIP (" + strarr + ") filteraction = AllowConnectIP ("+ strarr +") "; p. standardInput. writeLine (str) ;}} else {str = "netsh ipsec static add filterlist name = AllowConnectIP (" + StrIPArr + ")"; P. standardInput. writeLine (str); str = "netsh ipsec static add filter filterlist = AllowConnectIP (" + StrIPArr + ") srcaddr = me dstaddr =" + StrIPArr + ""; p. standardInput. writeLine (str); str = "netsh ipsec static add filteraction name = AllowConnectIP (" + StrIPArr + ") action = permit"; p. standardInput. writeLine (str); str = "netsh ipsec static add rule name = AllowConnectIP (" + StrIPArr + ") policy = BannedConnectIP filterlist = AllowConnectIP ("+ StrIPArr +") filteraction = AllowConnectIP ("+ StrIPArr +") "; p. standardInput. writeLine (str);} // assign str = "netsh ipsec static set policy name = BannedConnectIP assign = y"; p. standardInput. writeLine (str); p. standardInput. writeLine ("exit"); p. standardInput. autoFlush = true; // write the command to be executed to the standard input. Here, the & is the symbol of the batch processing command, indicating that the previous command executes the following (exit) command no matter whether the execution is successful or not. If the exit command is not executed, the ReadToEnd () command is called later () the method will be suspended. // symbols of the same type include & | the former indicates that the subsequent commands must be executed successfully before the previous command is executed successfully, the latter indicates that the subsequent command p. waitForExit (); // wait for the program to run and exit process p. close () ;}// open win7 connection public static void AllowWINRunCmd () {string str = Console. readLine (); System. diagnostics. process p = new System. diagnostics. process (); p. startInfo. fileName = "cmd.exe"; p. startInfo. useShellExecute = fal Se; // whether to use the Operating System shell to start p. startInfo. redirectStandardInput = true; // accept the input information from the caller p. startInfo. redirectStandardOutput = true; // get the output information by the calling program p. startInfo. redirectStandardError = true; // redirected standard error output p. startInfo. createNoWindow = true; // do not display the program window p. start (); // Start the program // Delete the policy str = "netsh ipsec static del policy name = BannedConnectIP"; p. standardInput. writeLine (str); p. standardInput. writeLine ("exit"); p. standardInput. A UtoFlush = true; // write the command to be executed to the standard input. Here, the & is the symbol of the batch processing command, indicating that the previous command executes the following (exit) command no matter whether the execution is successful or not. If the exit command is not executed, the ReadToEnd () command is called later () the method will be suspended. // symbols of the same type include & | the former indicates that the subsequent commands must be executed successfully before the previous command is executed successfully, the latter indicates that the subsequent command p. waitForExit (); // wait for the program to run and exit process p. close ();}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.