Determine OS from a single command line operation - linux

Introduction:
I have a 'magic' tool that can perform a command line operation on a machine if I provide the IP.
The tool knows the OS that machine is using and executes the command on cmd/shell based on whether it is windows/linux and returns the output of the command back blindly.
C:> tool.exe 172.140.56.2 "ipconfig"
Assumptions:
One OS per machine. Tool has no problem executing the command (whether it fails or not is a different problem)
The OS is either windows or linux always.
I determine the OS based on the command result
Problem:
Using this power of being able to execute a command, I want to determine the OS
My Solution:
Execute ipconfig command. If result is
-bash: ipconfig: command not found
It is linux.
Else if it is like this:
Windows IP Configuration
Ethernet adapter Local Area Connection:
...
then Windows.
Question:
I wanted to know if this is a foolproof way of doing this. I want a command which would not fail under certain scenarios. (say cygwin installed on windows allowing linux commands to succeed. Or ipconfig succeeding on linux under some special scneario.)
I can process the command output with some parser, if that helps in any way.
Just to clear any confusion. It can be ANY command. I ust used ipconfig in my example.

If I understood your problem correctly, then uname is the ideal command. If it's any Unix-system (including OSX), it'll return the correct variable, and if it's Windows it'll return command not found or similar.

Safest way to determine Linux/version is
cat /etc/*release
Sample output.
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=17
DISTRIB_CODENAME=qiana
DISTRIB_DESCRIPTION="Linux Mint 17 Qiana"
NAME="Ubuntu"
VERSION="14.04.1 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.1 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

Another command that should work is set, which displays name and value of each environment variable. Any shell on Linux supports it (although output is different between csh and bash). Output from a Windows system would have PROCESSOR_ARCHITECTURE and other standard variables in it (see https://ss64.com/nt/syntax-variables.html), which are very unlikely to be set in Linux.

Related

Running Matlab code on a cluster

I have a university account for the university's cluster, but I don't know how can I use it to run my Matlab code. Could anyone help? I connect to the cluster by typing below code in the terminal of my laptop:
ssh myusername#192.168.194.222
Then it asks me to type my password.After that, below text appears:
Welcome to gav 9.1.1 (3.12.60-ql-generic-9.1-74) based on Ubuntu 14.04.5 LTS
Last login: Sun Apr 16 10:45:49 2017 from 192.168.41.213
gav:~ >
How can I run my code after these processes? Could anyone help me?
It looks like you have a Linux shell, so you can run your script (for instance yourScript.m)
> matlab -nojvm -nodisplay -nosplash < yourScript.m
(see also https://uk.mathworks.com/help/matlab/ref/matlablinux.html)
As far as I know, there are two possibilities:
Conventional Matlab is installed on the Cluster
The Matlab Distributed Computing server is installed on the cluster
Conventional Matlab is installed on the Cluster
You execute Matlab on the cluster as you would on your local computer. I guess that you work on Windows on your local computer, given that you quote a simple shell prompt in your question ;) All right, all right, bad psychic skillz ;) see edit below.
What you see is the cluster awaiting a program name to execute. This is called the "Shell". Google "Linux shell tutorial" or start with this tutorial to get information about how to operate a Linux system without a graphical desktop.
Try to start matlab by simply typing matlab after the text you've seen. If it works, you see Matlab's welcome message and the Matlab prompt as you would see it in Matlab's command window on your local PC.
Bonus: you can try to execute Matlab on the cluster but see a graphical interface by replacing your ssh call by ssh -X myusername#192.168.194.222, so add an additional -X.
Upload your Matlab scripts to the cluster, for example by using WinSCP (tutorial)
Execute your Matlab functions like you would locally by navigating into the correct folder and typing the function name.
EDIT: As you use Linux, you may use gio mount ssh://myusername#192.168.194.222 to access your home folder on the cluster via your file manager. If that fails, try gvfs-mount ssh://myusername#192.168.194.222 (the old name of the tool). The packages gvfs-backends and gvfs-fuse (I assume that you use ubuntu, other distributions may have different package names) must be installed for this; use your Package manager to install them if you get an error like "command not found".
Distributed Computing Server
This provides a set of Matlab "Workers" which are sent tasks from your local Computer. You use your local Matlab installation to connect to the Distributed computing server. Start with the Matlab Help Pages for the Distributed Computing Server

One command to tell if it is windows, osx or linux/unix

Is there a way to determine the underlying OS platform just by running one command, irrespective of whether it is run on a command prompt in windows or a terminal (bash/sh/etc.) in OSX/Linux/Unix ?
Note: there may not be Python/Perl etc. or any such interpreters already present on the platform, so we can run any of these to figure out the same.
Short answer is "No", one command is not enough (at least not the same command). That's because environments are too different. You still can do this for OS X/Linux because they share same origin - Unix and thus uname -a would work. However Windows doesn't have this concept because it's not Unix-based. In windows you need to run another command to check version: winver
If anything, you can check host OS type with the help of come cross-platform tools, for example, some simple java utility or either of these Python oneliners:
python -c "import platform; print platform.platform()"
Gives more details
python -c "import platform; print platform.system()"
Gives only OS type

I want to open git bash in a linux server using putty. How will I do that?

I am following this https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git and I am stack on step 5, I want to open git bash in a linux server using putty. How will I do that? In windows, there is gitbash cmd desktop app. But in linux how will you get from normal command line to git bash?
Note: I am using putty to access the linux server. If there is other way also, please recommend me.
“Git bash” under Windows simulates a Linux environment (it uses MinGW behind the scenes, afaik).
Under a Linux distribution, you just have to have a version of Git installed. With a Debian based distribution you would for example run sudo apt-get install git (or git-core for older releases) on the command line.
Once installed, you can use it from the command line without further ado.
Actually Windows gitbash is an emulation of a bash command interpreter designed for UNIX systems. On many Linux distributives bash is a default command shell. So, when you're connecting to a Linux via putty, you're actually entering a bash shell. There's nothing more you need. You can use it the similar way as you're using windows gitbash. In a nutshell, just omit the first item in the spet 5 of your tutorial: you probably already in the bash shell.
To check out which shell you're using, run this command: ps -p $$. It will output something like this:
PID TTY TIME CMD
10967 ttys000 0:01.68 -bash
The CMD field is the shell name you're using.

How to detect if my server is running centos or other from a perl script

I want to display some text in a script only if the Operating System is Centos .
How can i do that in a perl script ?
To answer your exact question, you can identify CentOS by reading the contents of /etc/redhat-release. E.g.
$ cat /etc/redhat-release
CentOS release 5.9 (Final)
As other commenters have made clear, it is better to depend on the exact OS features you want, or write code to be portable, rather than limiting it to a particular distribution of Linux.
Try $^O. It contains the OS that was used to build your version of Perl. Here's what perlvar has to say about it.
The name of the operating system under which this copy of Perl was
built, as determined during the configuration process. For examples
see PLATFORMS in perlport. The value is identical to $Config{'osname'}
. See also Config and the -V command-line switch documented in
perlrun. In Windows platforms, $^O is not very helpful: since it is
always MSWin32 , it doesn't tell the difference between
95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or
Win32::GetOSVersion() (see Win32 and perlport) to distinguish between
the variants. This variable was added in Perl 5.003.
Also see perlport.

Change size of files on Windows machine from remote Linux machine

I need to change somehow size of files (increase or decrease) on Windows machine using bash scripts. (Content of files doesnt matter) But i have to run this scripts from remote linux machine. I've selected truncate command for size changing, this is exactly that i need, cause i need to change size exactly of chosen file, without changing it's descriptor. It is very important.
But i DO NOT have truncate on my linux machine and i CANNOT install it there (So dont tell me to install it there plz :)). I cannot install nothing on my linux machine it has specific kernel - this is a main option of all my problems.
So i've decided to install cygwin on my Windows machine, cause it has truncate command. Also i know that there are fallocate command, but my linux machine also doesnt have it, and cygwin doesnt have it too. So if there is some another command i wanna know it :)
Then after this steps i tried to change file size from cygwin terminal via truncate and all works perfectly. And the last problem that i had to solve was just run cygwin's bash from my remote linux, i've chosen winexe for that.
Finally the way that i've chosen is:
I run winexe command on my linux machine that runs:
winexe myHost "c:\cygwin\bin\bash.exe myScriptWithTRUNCATE"
on my Win machine.
But it doesnt work and i dunno why. truncate command doesnt change size of files at all. When i type
truncate --help
all works, i can see result of help option on my linux terminal, but e.g.
truncate -s someSize myFile
doesnt work, size of file doesnt change. Also error code from truncate -s someSize myFile is 0
Any suggestions?
try giving the name of your script that is "myScriptWithTRUNCATE" directly in winexe command .
example:-
winexe myHost "c:\cygwin\bin\bash.exe myScriptWithTRUNCATE"
also check debug log of winexe by modifying winexe command as :-
winexe -d 5 myHost "c:\cygwin\bin\bash.exe myScriptWithTRUNCATE"
see in this log what actually is sent over to windows as command in place for your script.
specifically see in " CTRL: sending command : run xxxxx"
see what "xxxxx" is in that debug log.
winexe gives you the control of windows command line(cmd.exe).
Try running you script after it has got control of cmd.exe.
Based on some findings above , try this link for more help
http://blog.dotsmart.net/2011/01/27/executing-cygwin-bash-scripts-on-windows/

Resources