I am developping an application that give a real time diagnosis of my working servers and of the services I am using on these server.
First of all I created a C# application on an individual remote server that pings the another server adress and gives back the result in an email if remote server is down.
I would like then to see if a certain service is running on the same server address(systemctl status [servicename], for this purpose, I created RSA keys and connect to the server with this key as mentionned here : How to Connect to a Linux Server Using Secure Shell (SSH)
Here is the function I use to connect to my server via 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();
}
and my main code :
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);
}
}
This program works fine if I call it with the following line in 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: ...
but when I call the c# application in a service Unix, I get permissions error....
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).
It should be a permission error, when i created the keyfiles, i protected them with a passphrase could it be that ?
I am trying to recreate keyfile without passphrase and post the result when it's done.
EDIT 1
I tryied to generate another key without a passphrase, same result....
I also changed the permission of the files keygen is readable by everyone (just to try after it should be readable only by me), Pinger.exe is executable by everyone and public key in server is also readable.
Any thoughts would be appreciated