-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (103 loc) · 3.63 KB
/
Program.cs
File metadata and controls
125 lines (103 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Newtonsoft.Json;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace SSHBot
{
sealed class Config
{
public class Host
{
public string Hostname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string PrivateKeyFile { get; set; }
public FileTransfer[] UploadFiles { get; set; }
public string[] RunCommands { get; set; }
public FileTransfer[] DownloadFiles { get; set; }
}
public class FileTransfer
{
public string RemoteFile { get; set; }
public string LocalFile { get; set; }
}
[JsonProperty(Required = Required.Always)]
public Host[] Hosts { get; set; }
}
static class Program
{
static Config config;
static void Main(string[] args)
{
var exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
var configPath = args.Length >= 1 ? args[0] : Path.Combine(Path.GetDirectoryName(exePath), "config.json");
config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(configPath));
Console.WriteLine($"SSH Bot");
foreach (var host in config.Hosts)
{
try
{
Console.WriteLine($"Connecting to '{host.Hostname}' as '{host.Username}'.");
var connectionInfo = new ConnectionInfo(host.Hostname, host.Username, host.PrivateKeyFile != null ?
(AuthenticationMethod)new PrivateKeyAuthenticationMethod(host.Username, new PrivateKeyFile(host.PrivateKeyFile)) :
(AuthenticationMethod)new PasswordAuthenticationMethod(host.Username, host.Password));
// Disable ECDSA on platforms that don't support it.
try { using (var ecdsa = new System.Security.Cryptography.ECDsaCng()) ; }
catch (NotImplementedException)
{
var algsToRemove = connectionInfo.HostKeyAlgorithms.Keys.Where(algName => algName.StartsWith("ecdsa")).ToArray();
foreach (var algName in algsToRemove) connectionInfo.HostKeyAlgorithms.Remove(algName);
}
if (host.UploadFiles != null)
{
using (var sftp = new SftpClient(connectionInfo))
{
sftp.Connect();
Console.WriteLine($"Connected for uploading files.");
foreach (var fileTransfer in host.UploadFiles)
{
Console.WriteLine($"Uploading '{fileTransfer.LocalFile}' to '{fileTransfer.RemoteFile}'.");
using (var file = new FileStream(fileTransfer.LocalFile, FileMode.Open, FileAccess.Read))
sftp.UploadFile(file, fileTransfer.RemoteFile, true);
}
}
}
if (host.RunCommands != null)
{
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
Console.WriteLine($"Connected for running commands.");
foreach (var command in host.RunCommands)
{
Console.WriteLine($"Running '{command}'.");
var result = ssh.RunCommand(command);
Console.Write(result.Result);
Console.WriteLine($"Exit status = {result.ExitStatus}");
}
}
}
if (host.DownloadFiles != null)
{
using (var sftp = new SftpClient(connectionInfo))
{
sftp.Connect();
Console.WriteLine($"Connected for downloading files.");
foreach (var fileTransfer in host.DownloadFiles)
{
Console.WriteLine($"Downloading '{fileTransfer.RemoteFile}' to '{fileTransfer.LocalFile}'.");
using (var file = new FileStream(fileTransfer.LocalFile, FileMode.Create, FileAccess.Write))
sftp.DownloadFile(fileTransfer.RemoteFile, file);
}
}
}
}
catch (Exception e) { Console.WriteLine($"Error communicating with '{host.Hostname}'. {e.ToString()}"); }
}
}
}
}