C#控制台和.bat说明

时间:2017-06-01 13:21:27

标签: c# batch-file

我正在编写一个可以使用一组指令的控制台应用程序。

class Program
{
    static void Main(string[] args)
    {
        var cmd = "";
        if (args.Length > 0)
        {
            cmd = args[0];
        }
        switch (cmd)
        {
            case "SSHPPK":
                InitPpk(InitCompleted, args); //Read from args
                break;
            case "SSHPWD":
                InitPwd(InitCompleted, args); //Read from args
                break;
            default:
                Console.WriteLine("Invalid Command");
                break;
        }
    }

    private static void InitCompleted(ConnectInstructions instructions)
    {
        //Read next lines from .bat file and execute untill the end  (Psuedo, While not at end of file)
        //Get command
        //Get server IP
        if (instructions.ConnectType == "SSHPPK")
        {
            //Connect using Private Key
        }
        else if (instructions.ConnectType == "SSHPWD")
        {
            //Connect using Password
        }
        //Get Root
        //Do update
    }

    private static void InitPwd(Action<ConnectInstructions> action, string[] args)
    {
    }

    private static void InitPpk(Action<ConnectInstructions> action, string[] args)
    {

    }
}

我正在使用的批处理文件看起来像这样

SSHUpdate.exe "SSHPWD" "Username" "Password"
update [Server1 IP] /var/root/site
update [Server2 IP] /var/root/site
update [Server3 IP] /var/root/site
update [Server4 IP] /var/root/siteA
update [Server4 IP] /var/root/siteB

当我运行.bat文件时,它打开SSHUpdate.exe,我可以使用args连接,但是我无法从同一进程访问其余的行

所有服务器都将使用相同的密码或私钥

目前我正在做以下情况 SSHUpdate.exe“SSHPWD”“用户名”“密码”“指令文件”,其中指令文件包含说明

我应该坚持使用,还是有办法获得下一行指令,只需要.exe和.bat文件?

1 个答案:

答案 0 :(得分:1)

您可以use the ^ character to escape your line breaks将所有内容发送到您的计划中。但是,它不是很优雅,而是length of the command line is limited

或者,如果您想要坚持使用两个文件,则可以按需创建第三个文件:

echo update [Server1 IP] /var/root/site > %temp%\myinstructions.txt
echo update [Server2 IP] /var/root/site >> %temp%\myinstructions.txt
echo update [Server3 IP] /var/root/site >> %temp%\myinstructions.txt
echo update [Server4 IP] /var/root/siteA >> %temp%\myinstructions.txt
echo update [Server4 IP] /var/root/siteB >> %temp%\myinstructions.txt
SSHUpdate.exe "SSHPWD" "Username" "Password" "%temp%\myinstructions.txt"