windows 通过制作 .bat 文件进行远程桌面连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20810510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:12:50  来源:igfitidea点击:

Remote Desktop Connection by making .bat file

windowsbatch-fileremote-desktop

提问by user3141034

I want to connect my pc to another pc by making .bat file. When i run that file it should connect to the other pc. I wrote "mstsc /v:192.168.15.102" command when i execute this file it opens remote desktop window and demands username and password. How can i avoid that window and pass the credential in .bat file.

我想通过制作 .bat 文件将我的电脑连接到另一台电脑。当我运行该文件时,它应该连接到另一台电脑。我在执行此文件时编写了“mstsc /v:192.168.15.102”命令,它会打开远程桌面窗口并要求用户名和密码。如何避免该窗口并在 .bat 文件中传递凭据。

回答by brunch875

I found this

我找到了这个

    cmdkey /generic:TERMSRV/$server /user:$user /pass:$Password
mstsc /v:$Server

from archive(or original)

来自存档(或原始

But I think that's for powershell only and I'm inexperienced with windows.

但我认为这仅适用于 powershell,而且我对 Windows 缺乏经验。

Along the comments I also saw this one:

在评论中,我还看到了这个:

For people that were looking for this idea but want to use batch instead I created the following. Save this a mstscup.cmd from notepad (don't forget to remove the .txt at the end!) Call it from the command line as follows: mstscup “servername” “user” “pass” I didn't like the idea of leaving the user and pass in the password vault so it clears it after 120 seconds (leaves a command prompt window opened). Feel free to modify at will!

对于正在寻找这个想法但想要使用批处理的人,我创建了以下内容。从记事本保存一个 mstscup.cmd(不要忘记删除最后的 .txt!)从命令行调用它,如下所示: mstscup “servername” “user” “pass” 我不喜欢这个想法离开用户并传入密码库,以便在 120 秒后将其清除(打开命令提示符窗口)。随意随意修改!

@echo off
setlocal
:: Check if the user passed at least 1 argument
if “%1%” == “” (
echo Remoted Desktop connection with user and password
echo.
echo Missing arguments. Syntax:
echo %~nx0% “servername” “username” “password”
echo.
echo Jean Morin, v0.1, 2013-02-23
pause
goto :eof
)
:: Next line removes surrounding quotes from server name
set sServer=%~1%
:: Keep the quotes for the username and password (in case spaces exists)
set sUser=%2%
set sPass=%3%
:: Seconds to wait before clearing the newly added password from the vault (see control panel, manage your credentials)
:: You may want to modify this if the server takes longer to connect (WAN). You could add this as a fourth argument.
set sSeconds=120
:: Add a new connection definition method to the vault
cmdkey /generic:TERMSRV/%sServer% /user:%sUser% /pass:%sPass%
:: Connect to the server as a new task
start mstsc /v:%sServer%
:: ping ourselves for x seconds (acts like a pause) then removes the newly added password from the vault
ping -n %sSeconds% 127.0.0.1 >nul:
cmdkey /delete:TERMSRV/%sServer%

回答by Wasim Ansari

Remote Login using java and batch file by double click

通过双击使用java和批处理文件进行远程登录

  1. Create batch file Remote.bat and write the following code,

    @echo off
    java Remote DEV
    
  2. Create a java file Remote.java and write following code, also change the IP address of your remote computer in code.

    import java.awt.MouseInfo;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.concurrent.TimeUnit; 
    
    public class Remote
    {
        public static void main(String args[])
        {
            try 
            {
                //showPosition();
    
                System.out.println("Remote Desktop for-->"+args[0]);
                String IP = "";
    
                if("DEV".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.41.101";
                }
                else if("UAT".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.43";
                }
                else if("PRE-PROD".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.209";
                }
    
                Process p = Runtime. getRuntime(). exec(IP);
                Robot bot = new Robot();
                long mask = InputEvent.MOUSE_EVENT_MASK;
    
                TimeUnit.SECONDS.sleep((long) 2.5);
    
                bot.mouseMove(607, 290);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
                bot.keyPress(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_Y);
                bot.keyRelease(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_E);
                bot.keyPress(KeyEvent.VK_S);
                bot.keyPress(KeyEvent.VK_B);
                bot.keyPress(KeyEvent.VK_A);
                bot.keyPress(KeyEvent.VK_N);
                bot.keyPress(KeyEvent.VK_K);
                bot.keyPress(KeyEvent.VK_1);
    
                bot.mouseMove(765, 508);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
            } 
            catch (Exception e) 
            {
                System.out.println("Exception send--->"+e.getMessage());
                e.printStackTrace();
            } 
        }
    
        public static void showPosition() throws InterruptedException
        {
            try
            {
                while(true == true)
                {
                    TimeUnit.SECONDS.sleep(1/2);
                    double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
                    double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
                    System.out.println("X:" + mouseX);
                    System.out.println("Y:" + mouseY);
                    //make sure to import 
                }
            }
            catch(Exception e)
            {
                System.out.println("Excpetion inside showPosition-->"+e.getMessage());
            }
        }
    
    
    }
    
  1. 创建批处理文件 Remote.bat 并编写以下代码,

    @echo off
    java Remote DEV
    
  2. 创建一个java文件Remote.java并编写以下代码,同时在代码中更改远程计算机的IP地址。

    import java.awt.MouseInfo;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.concurrent.TimeUnit; 
    
    public class Remote
    {
        public static void main(String args[])
        {
            try 
            {
                //showPosition();
    
                System.out.println("Remote Desktop for-->"+args[0]);
                String IP = "";
    
                if("DEV".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.41.101";
                }
                else if("UAT".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.43";
                }
                else if("PRE-PROD".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.209";
                }
    
                Process p = Runtime. getRuntime(). exec(IP);
                Robot bot = new Robot();
                long mask = InputEvent.MOUSE_EVENT_MASK;
    
                TimeUnit.SECONDS.sleep((long) 2.5);
    
                bot.mouseMove(607, 290);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
                bot.keyPress(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_Y);
                bot.keyRelease(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_E);
                bot.keyPress(KeyEvent.VK_S);
                bot.keyPress(KeyEvent.VK_B);
                bot.keyPress(KeyEvent.VK_A);
                bot.keyPress(KeyEvent.VK_N);
                bot.keyPress(KeyEvent.VK_K);
                bot.keyPress(KeyEvent.VK_1);
    
                bot.mouseMove(765, 508);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
            } 
            catch (Exception e) 
            {
                System.out.println("Exception send--->"+e.getMessage());
                e.printStackTrace();
            } 
        }
    
        public static void showPosition() throws InterruptedException
        {
            try
            {
                while(true == true)
                {
                    TimeUnit.SECONDS.sleep(1/2);
                    double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
                    double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
                    System.out.println("X:" + mouseX);
                    System.out.println("Y:" + mouseY);
                    //make sure to import 
                }
            }
            catch(Exception e)
            {
                System.out.println("Excpetion inside showPosition-->"+e.getMessage());
            }
        }
    
    
    }
    

Now save the code and double click on Remote.bat. it will automatically open your remote computer. Enjoyyyyyyy

现在保存代码并双击 Remote.bat。它会自动打开您的远程计算机。享受yyyyyy