PDA

View Full Version : چک کردن آپ تایم روتربورد میکروتیک



ehsan_faal
چهارشنبه 07 شهریور 1397, 11:37 صبح
سلام.
با استفاده از chilkat یه برنامه نوشتم که با ssh به روتربوردی که تو اتاق سرور هست و فقط برای چک کردن وضعیت برق شهر اونجا گذاشته شده، آپ تایمش رو بخونم.
با وینباکس هم میشه اینکار رو کرد اما وینباکس در صورت disconnect شدن هیچ alarm صداداری نمیده و فقط میگه که reconnect کن.
مشکلاتی که دارم اینا هستن:
۱- برنامه resource زیادی میکشه. از خود وینباکس با اونهمه امکانات هم بیشتر.
۲- توی لاگ روتر با هر بار فراخوانی تابع مربوط به ssh یه خط connect و disconnect‌میاد در صورتی که من یک بار فقط session ‌رو ایجاد و یک بار هم بستمش.
۳- dll های این کتابخونه رو نیمشه به خود فایل نهایی attach کرد؟
اینم کدی که استفاده میکنم در حال حاضر:
راه بهینه تری پیشنهاد بدید عالی میشه:

Program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

{$R 'MYDDL.res' 'MYDDL.rc'}

Uses
System.SysUtils,
System.StrUtils,
Ssh;

Var
Ssh: HCkSsh;
cmdOutput: PWideChar;

Procedure RunSSH;
Var
success: Boolean;
port: Integer;
host: PWideChar;
channelNum: Integer;
Begin
Ssh := CkSsh_Create();
success := CkSsh_UnlockComponent(Ssh, 's2yjlR.CB10750_Ig3zlV2R4C77');
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
port := ****;
host := '192.168.185.254';
success := CkSsh_Connect(Ssh, host, port);
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
CkSsh_putIdleTimeoutMs(Ssh, 5000);
success := CkSsh_AuthenticatePw(Ssh, '****', '*****');
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
channelNum := CkSsh_OpenSessionChannel(Ssh);
If (channelNum < 0) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;

success := CkSsh_SendReqExec(Ssh, channelNum, 'system resource print');
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
success := CkSsh_ChannelReceiveToClose(Ssh, channelNum);
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
cmdOutput := CkSsh__getReceivedText(Ssh, channelNum, 'ansi');
If (CkSsh_getLastMethodSuccess(Ssh) <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Exit;
End;
End;

Begin
Try
While True Do
Begin
RunSSH;
Writeln(Trim(LeftStr(cmdOutput, Pos(#10, cmdOutput))));
Sleep(5000);
End;
Finally
CkSsh_Disconnect(Ssh);
CkSsh_Dispose(Ssh);
End;
Readln;
End.

ehsan_faal
چهارشنبه 07 شهریور 1397, 16:10 عصر
فکر میکردم اینجوری بشه مشکل دوم رو حل کرد اما نمیشه ظاهرا:


Program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

Uses
System.SysUtils,
System.StrUtils,
Ssh;

Var
Ssh: HCkSsh;
cmdOutput: PWideChar;
success: Boolean;
port: Integer;
host: PWideChar;
channelNum: Integer;

Begin
Ssh := nil;
Try
Ssh := CkSsh_Create();
success := CkSsh_UnlockComponent(Ssh, 's2yjlR.CB10750_Ig3zlV2R4C77');
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
port := ****;
host := '192.168.185.254';
success := CkSsh_Connect(Ssh, host, port);
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Raise Exception.Create(CkSsh__lastErrorText(Ssh));

End;
CkSsh_putIdleTimeoutMs(Ssh, 5000);
success := CkSsh_AuthenticatePw(Ssh, '****', '******');
If (success <> True) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
channelNum := CkSsh_OpenSessionChannel(Ssh);
If (channelNum < 0) Then
Begin
Writeln(CkSsh__lastErrorText(Ssh));
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;

While True Do
Begin
success := CkSsh_SendReqExec(Ssh, channelNum, 'system resource print');
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
success := CkSsh_ChannelReceiveToClose(Ssh, channelNum);
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
cmdOutput := CkSsh__getReceivedText(Ssh, channelNum, 'ansi');
If (CkSsh_getLastMethodSuccess(Ssh) <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
Writeln(Trim(LeftStr(cmdOutput, Pos(#10, cmdOutput))));
Sleep(3000);
End;
Except
On E: Exception Do
Begin
Writeln('Exception class name = ' + E.ClassName);
Writeln('Exception message = ' + E.Message);
CkSsh_Disconnect(Ssh);
CkSsh_Dispose(Ssh);
Readln;
End;
End;
End.

ehsan_faal
چهارشنبه 07 شهریور 1397, 16:53 عصر
مشکل دوم حل شد، ظاهرا برای کامندی که از طریق ssh میزنم باید یک چنل دیگه درست بشه.

Program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

Uses
System.SysUtils,
System.StrUtils,
Ssh;

Var
Ssh: HCkSsh;
cmdOutput: PWideChar;
success: Boolean;
port: Integer;
host: PWideChar;
channelNum: Integer;

Begin
Ssh := nil;
Try
Ssh := CkSsh_Create();
success := CkSsh_UnlockComponent(Ssh, 's2yjlR.CB10750_Ig3zlV2R4C77');
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
port := ****;
host := '192.168.185.254';
success := CkSsh_Connect(Ssh, host, port);
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));

End;
CkSsh_putIdleTimeoutMs(Ssh, 5000);
success := CkSsh_AuthenticatePw(Ssh, '*****', '******');
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
channelNum := CkSsh_OpenSessionChannel(Ssh);
If (channelNum < 0) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;

While True Do
Begin
success := CkSsh_SendReqExec(Ssh, channelNum, 'system resource print');
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
success := CkSsh_ChannelReceiveToClose(Ssh, channelNum);
If (success <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
cmdOutput := CkSsh__getReceivedText(Ssh, channelNum, 'ansi');
If (CkSsh_getLastMethodSuccess(Ssh) <> True) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
Writeln(Trim(LeftStr(cmdOutput, Pos(#10, cmdOutput))));
Sleep(3000);
channelNum := CkSsh_OpenSessionChannel(Ssh);
If (channelNum < 0) Then
Begin
Raise Exception.Create(CkSsh__lastErrorText(Ssh));
End;
End;
Except
On E: Exception Do
Begin
CkSsh_Disconnect(Ssh);
CkSsh_Dispose(Ssh);
Writeln('Exception class name = ' + E.ClassName);
Writeln('Exception Message = ' + E.Message);
Readln;
End;
End;
End.