我正在开发一个应用程序,可以对我的工作服务器以及我在这些服务器上使用的服务进行实时诊断。
首先,我在单个远程服务器上创建了一个 C# 应用程序,该应用程序会 ping 另一个服务器地址,如果远程服务器关闭,则通过电子邮件返回结果。
然后我想查看某个服务是否在同一服务器地址上运行(systemctl status [servicename],为此,我创建了 RSA 密钥并使用此密钥连接到服务器,如下所述:https://hostpresto.com/community/tutorials/how-to-connect-to-a-linux-server-using-secure-shell-ssh/
这是我用来通过 ssh 连接到服务器的功能:
internal static void Command(string command, string arguments, string input = null)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = command;
process.StartInfo.RedirectStandardInput = input != null;
process.StartInfo.UseShellExecute = input == null;
process.Start();
if (input != null)
{
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
}
process.WaitForExit();
process.Close();
}
我的主要代码:
static void Main(string[] args)
{
string[] Hardware = { "ELog", "SIY", "API" };
while (true)
{
string file = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..");
string crypto = Tools.GetString("ELog");
foreach(string hard in Hardware)
{
crypto = Crypto.Decrypt(Tools.GetString(hard));
PingReply reply = Pinger.PingHost(crypto);
try
{
if (reply != null && ((reply.RoundtripTime > 200) || (reply.Status.ToString() != "Success") || (reply.Options.Ttl < 1)))
{
Console.WriteLine("Diag Server NOK" + " Addresse : " + reply.Address.ToString() + " Status : " + reply.Status.ToString()
+ " RoundTripTime : " + reply.RoundtripTime + " TTL : " + reply.Options.Ttl + " Buffer size : " + reply.Buffer.Length);
Tools.SendMail("mailaddress", "mailaddress2", "MySubject", " Addresse : " + reply.Address.ToString() + " Status : " + reply.Status.ToString()
+ " RoundTripTime : " + reply.RoundtripTime + " TTL : " + reply.Options.Ttl + " Buffer size : " + reply.Buffer.Length);
}
else
{
Console.WriteLine("Diag Server OK" + " Addresse : " + reply.Address.ToString() + " Status : " + reply.Status.ToString()
+ " RoundTripTime : " + reply.RoundtripTime + " TTL : " + reply.Options.Ttl + " Buffer size : " + reply.Buffer.Length);
}
}
catch(Exception e)
{
Tools.Exception(e);
}
}
try
{
Linux.Command("ssh", "-i mykeyfilepath root@myserver systemctl status myservice");
}
catch(Exception e)
{
Tools.Exception(e);
}
Thread.Sleep(60000);
}
}
`
如果我在 VM Oracle 中使用以下行调用该程序,则该程序可以正常工作:
mono Pinger.exe
Diag Server OK Addresse : hidden Status : Success RoundTripTime : 65 TTL : 128 Buffer size : 0
Diag Server OK Addresse : hidden Status : Success RoundTripTime : 30 TTL : 128 Buffer size : 0
Diag Server OK Addresse : hidden Status : Success RoundTripTime : 30 TTL : 128 Buffer size : 0
● service.service - Comanche Web Development Server
Loaded: loaded (servicepath; enabled)
Active: active (running) since Wed 2018-05-23 17:17:13 CEST; 1 day 18h ago
Main PID: 12368 (cli)
CGroup: ...
但是当我在服务 Unix 中调用 c# 应用程序时,出现权限错误......
systemctl start hello
systemctl status hello
● hello.service - FTP update
Loaded: loaded (/etc/systemd/system/hello.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2018-05-25 11:29:04 CEST; 6s ago
Main PID: 4038 (cli)
Tasks: 3 (limit: 1113)
CGroup: /system.slice/hello.service
└─4038 /usr/bin/cli /Pinger.exe
mai 25 11:29:04 nicolas-VirtualBox systemd[1]: Started FTP update.
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: Diag Server OK Addresse : hidden 2 Status : Success RoundTripTime : 25 TTL : 128 Buffer size : 0
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: Diag Server OK Addresse : hidden Status : Success RoundTripTime : 23 TTL : 128 Buffer size : 0
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: Diag Server OK Addresse : hidden Status : Success RoundTripTime : 27 TTL : 128 Buffer size : 0
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: Permission denied, please try again.
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: Permission denied, please try again.
mai 25 11:29:05 nicolas-VirtualBox Pinger.exe[4038]: root@serveraddress: Permission denied (publickey,password).
这应该是权限错误,当我创建密钥文件时,我用密码保护了它们,可能是这样吗?
我正在尝试重新创建没有密码的密钥文件,并在完成后发布结果。
编辑1 我尝试生成另一个没有密码的密钥,结果是一样的......我还改变了文件的权限,keygen 可以被所有人读取(只是为了尝试只有我能读取),Pinger.exe 可以被所有人执行,服务器中的公钥也是可读的。
任何想法都将不胜感激