Donald Jansen
Donald Jansen

Reputation: 1985

C# Console and .bat instructions

I am writing a Console application that will work with a set of instructions.

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)
    {

    }
}

The batch file I am using will look something like this

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

And when I run the .bat file it opens SSHUpdate.exe and I can connect using the args, But I am unable to access the rest of the lines from the same process

All Servers will use the same password or private key

Currently I am doing it like the following SSHUpdate.exe "SSHPWD" "Username" "Password" "Instruction File" where the Instruction File contains the instructions

Should I stick with that or is there a way where I can get the next line of instructions and only need to have the .exe and the .bat files?

Upvotes: 1

Views: 179

Answers (1)

Heinzi
Heinzi

Reputation: 172270

You can use the ^ character to escape your line breaks and send everything to your program. However, it's not very elegant and the length of the command line is limited.

Alternatively, if you want to stick to two files, you can create the third file on demand:

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"

Upvotes: 1

Related Questions