-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClientSSH.cs
95 lines (80 loc) · 2.31 KB
/
ClientSSH.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using Renci.SshNet;
namespace akash_dep
{
public class ClientSSH
{
public String SSH_IP = "127.0.0.1";
public int SSH_PORT = 22;
public String SSH_LOGIN;
public String SSH_PASS;
static SshClient m_client;
static String m_pushed = "";
public void Connect()
{
m_client = new SshClient(SSH_IP, SSH_PORT, SSH_LOGIN, SSH_PASS);
m_client.Connect();
}
public void LoadCfg(JToken cfg)
{
SSH_IP = cfg["SSH_IP"].ToString();
SSH_PORT = cfg["SSH_PORT"].ToObject<int>();
SSH_LOGIN = cfg["SSH_LOGIN"].ToString();
SSH_PASS = cfg["SSH_PASS"].ToString();
}
public void Push(String cmd)
{
if (m_pushed.Length > 0)
{
m_pushed += ";\n";
}
m_pushed += cmd;
}
public void Clear()
{
m_pushed = "";
}
public void ShowErrorInfo(SshCommand res)
{
String err = res.Error;
if (String.IsNullOrEmpty(err)) return;
if (res.ExitStatus == 0)
{
if (!err.Contains("gas estimate:"))
{
Console.WriteLine("info->\n" + err);
}
}
else
{
Console.WriteLine("err->\n" + err);
}
}
public String Send()
{
m_pushed += "\n";
var cmdRes = m_client.CreateCommand(m_pushed);
var res = cmdRes.Execute();
String err = cmdRes.Error;
ShowErrorInfo(cmdRes);
if (!String.IsNullOrEmpty(err))
{
int tries = 0;
while (err.Contains("post failed: Post") && err.Contains(": EOF") && tries < 2)
{
tries++;
cmdRes = m_client.CreateCommand(m_pushed);
res = cmdRes.Execute();
err = cmdRes.Error;
ShowErrorInfo(cmdRes);
}
}
File.WriteAllText("akash.txt", m_pushed);
Clear();
return res;
}
}
}