Martin, I have been successful at pushing the file to multiple hosts. In the example below it is just to two sites;
I created a C# Console app called PushFirmware. I added the WinSCP API as a reference, and then in the code below, it will connect to both devices and send the file to each.
I need to add the code so that the PLINK command can execute a shell script on the Linux devices and install the firmware. Lastly, is to add Try/Catch to catch any issues.
I will update the code once I have completed and tested it.
Program.cs
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
namespace PushFirmware
{
class Program
{
static ConcurrentQueue<string> ConsoleQueue;
static System.Timers.Timer timer;
static void Main()
{
timer = new System.Timers.Timer(200);
timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
ConsoleQueue = new ConcurrentQueue<string>();
Console.WriteLine("Transfering Binaries to CNA's");
//Threading thr1 = new Threading();
UploadTask thr1 = new UploadTask("192.168.1.100", "ssh-rsa 1040 <insert SSHKEY here>", ConsoleQueue);
UploadTask thr2 = new UploadTask("192.168.1.101", "ssh-rsa 1040 <insert SSHKEY here>", ConsoleQueue);
while (true)
{
Thread.Sleep(2);
}
//timer.Enabled = false;
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (ConsoleQueue == null) return;
string s;
while (ConsoleQueue.TryDequeue(out s)) {
File.AppendAllText("C:\\test.log", s + Environment.NewLine);
Console.WriteLine(s);
}
}
}
}
Threading.cs
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WinSCP;
namespace PushFirmware
{
class UploadTask
{
ConcurrentQueue<string> ConsoleQueue;
string cna_ip;
string fprint;
public UploadTask(string cip, string fpr, ConcurrentQueue<string> q)
{
cna_ip = cip;
fprint = fpr;
ConsoleQueue = q;
start();
}
void start()
{
Thread tid1 = new Thread(new ThreadStart(Thread1));
tid1.Start();
}
public void Thread1()
{
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Scp,
HostName = cna_ip,
UserName = "<username>",
Password = "<password>",
SshHostKeyFingerprint = fprint
};
sessionOptions.AddRawSettings("AuthGSSAPI", "1");
sessionOptions.AddRawSettings("Cipher", "aes,blowfish,3des,chacha20,WARN,arcfour,des");
sessionOptions.AddRawSettings("KEX", "ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(@"E:\CNA1000\Firmware\CNA1504v1.1.7\CNA1504v1_1_7.run", "/var/tmp/*", false, transferOptions);
/*
catch(Exception ex){ConsoleQueue.Enqueue(ex.Message);}
*/
// transferResult.Check();
session.Close();
// Print results
//foreach (TransferEventArgs transfer in transferResult.Transfers)
//{
ConsoleQueue.Enqueue(string.Format("Upload to {0} succeeded", cna_ip));
//Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
//}
// run plink here
ConsoleQueue.Enqueue("running plink... add the code here to run the plink command and an update script to execute the uploaded file.");
}
}
}
}