.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to indicate the remote host’s os


This post introduce how to find the remote machine(s) is Window or Linux in the system UI – Data Server – Equipment Service. In this case, if remote DS is Windows, you have to install Cygwin which simulate Linux environment on Windows, so you can execute the same command for both platform. After that, you will use plink application to get the output of command uname into a file, we check the file out and there is some key word for every operation system like: Linux (Linux) or Cygwin (Windows)

@ECHO OFF
set ds_ip=192.168.1.1
set ds_user=nexus
set ds_pwd=nexus
set out_file=ping_temp.txt
set out_efile=ping_temp_error.txt
ping -n 4 %ds_ip% > nul
if errorlevel 1 goto pingerror
if errorlevel 0 goto pingok

:pingerror
	rem echo Unable to communicate with Data Server at %ds_ip%. 
	rem pause
    exit /B 6
:pingok 
rem echo Ability to communicate with Data Server confirmed.

plink -pw %ds_pwd% %ds_user%@%ds_ip% uname -a > %out_file% 2> %out_efile%

rem error 	1: Access denied
rem error 	2: Host does not exist
rem error 	3: uname: not found

rem success 4: Linux
rem success 5: Cygwin
rem error 	6: ping failed

set error_auth=Access denied
set error_conn=Host does not exist
set error_command=uname: not found
set success_linux=Linux
set success_window=Cygwin

findstr /I /C:"%error_auth%" %out_efile% > nul
if errorlevel 1 goto val_connection
if errorlevel 0 goto error_auth
:error_auth
	rem echo %error_auth%
	rem pause
	exit /B 1
:val_connection
findstr /I /C:"%error_conn%" %out_efile% > nul
if errorlevel 1 goto val_command
if errorlevel 0 goto error_conn
:error_conn
	rem echo %error_conn%
	rem pause
	exit /B 2
:val_command
findstr /I /C:"%error_command%" %out_efile% > nul
if errorlevel 1 goto val_linux
if errorlevel 0 goto error_command
:error_command
	rem echo %error_command%
	rem pause
	exit /B 3
:val_linux
findstr /I /C:"%success_linux%" %out_file% > nul
if errorlevel 1 goto success_window
if errorlevel 0 goto success_linux
:success_linux
	rem echo %success_linux%
	rem pause
	exit /B 4
:success_window
	rem echo %success_window%
	rem pause
	exit /B 5
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: