How to programatically call executable in ruby - ruby

I'm trying to dynamically call a program depending if a user has it installed or not
#program_path = %x(which x)
unless $?.success?
#download program to a location
#set path to above location
#program_path = "$HOME/Downloads/location"
end
`#{#program_path} login -r #{HOST} -n #{NICKNAME} -u #{#username} -P #{#password}`
It runs properly when the program is not installed.
But when it is installed it seems to not call the full program I have in backticks - like its missing the arguments?
Similarly it doesn't work with %x either.
What am I missing?

chomp!
#program_path = %x(which x).chomp

Related

How do retrieve output from a bashscript that you run from a tcl script <- (modulefile script)

In my home dir, I have sub directories (CentOS, Ubuntu, etc) all for specific nodes I have access to.
Each OS will hold their own copy of programs, one of which is Python:
$HOME/{CentOS, Ubuntu, ...}/{python2,python3}
I am using environment modules so that when I ssh into a different computer (COMP), Python aliases will be set for that specific (COMP). For example:
COMP1 is CentOS
when I ssh into COMP1, "python3" should point to $HOME/Centos/python3/bin/python3
COMP2 is Ubuntu
when I ssh into COMP2 "python2" should point to $HOME/Ubuntu/python2/bin/python2
I can retrieve the OS name in bash using lsb_release -si, but I am working with modulefiles which are written in tcl, and haven't found something like lsb_release. Can I have a bash script that outputs lsb_release -si when called from a tcl script?
I tried doing this but no luck:
BASH SCRIPT:
#!/bin/bash
OS=$(lsb_release -si)
echo $OS
MODULEFILE SCRIPT:
#%Modulefile1.0
set OS [catch {exec bash /path/to/bash_file} output]
puts $OS
This doesn't do much.
Option A: export the variable in bash and access the environment variable in tcl.
#!/bin/bash
OS=$(lsb_release -si)
export OS
somescript.tcl
#!/usr/bin/tclsh
puts $::env(OS)
Option B: Use the platform package that comes with tcl.
#!/usr/bin/tclsh
package require platform
puts [platform::identify] ; # detailed OS-CPU
puts [platform::generic] ; # more generic OS-CPU
References: env platform
Your code mostly doesn't look obviously wrong.
But following the [catch {exec ...} output] the value that you are looking for should be in the output variable; the OS variable will have a code indicating effectively whether the bash script produced any output to stderr. Since you're definitely not interested in that debugging output which might be produced for reasons not under your easy control, you can probably do this:
catch {exec bash /path/to/bash_file 2>/dev/null} output
puts $output
Also make sure your bash script has an explicit exit at the end. Might as well ensure that it stops correctly. That's the default behaviour, but it's better to be explicit here as this is a (small) program.

Execute basic windows commands

I would like to achieve this using win32ole only and not any other way to execute shell commands in ruby.
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
my_username = shell.ShellExecute('cmd.exe', 'username', '', 'open', 0)
puts my_username
#Current output => nil
Just want to print my username but generally would like to execute any commands and get its output. I know we have ENV['user'] or echo %username% gives me what I want but want this using win32ole only.
Thanks a lot in advance.
You should try with whoami instead of username:
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
my_username = shell.ShellExecute('cmd.exe', 'whoami', '', 'open', 0)
puts my_username
You can't use ShellExecute() because it doesn't let you access the output of the command your run which is what you want. See Using ShellExecuteEx and capturing standard in/out/err for more informations about that point.
I would simply use puts ENV['USERNAME'] which works like a charm. (Or any command given by Ilia Aptsiauri in his answer)
If you just want to execute Windows CLI commands you need to take little bit different approach.
Instead of whoami you can put any command you want
system("whoami")
`whoami` (these are backticks)
spawn("whoami")
The difference between those are the following
system waits until "The command" has finished and outputs everything
to $stdout. Then it returns true or false depending on the exitstatus.
The backticks wait as well, but return the output made by the program.
spawn doesn't wait, but rather returns the PID of the subprocess. Note
that this requires Ruby 1.9 on Windows to work.
There is one more option check Open3 library it get's little bit more information during the output.

How do I to run a command in Linux as a Ruby script?

Let's say I have some terminal commands like:
sudo mycommand1
mycommand2
#.....
What should I do run them via ruby script (not bash) in Ubuntu?
UPDATE:
I have a ruby script:
def my_method1()
#calculating something.....
end
def method2(var1, var2)
#how do I sudo mycommand1 and any other Lunix command from here?
end
def method3(var4)
#calculating something2....
end
You can do system, exec, or place the command in backticks.
exec("mycommand") will replace the current process so that's really only pratical at the end of your ruby script.
system("mycommand") will create a new process and return true if the command succeeded and nil otherwise.
If you need to use the output of your command in your Ruby script use backticks:
response = 'mycommand`
There are many questions on SO that answer this. However you can run a command in many ways using system, exec, (backticks), %x{} or using open3. I prefer to use open3 -
require 'open3'
log = File.new("#{your_log_dir}/script.log", "w+")
command = "ls -altr ${HOME}"
Open3.popen3(command) do |stdin, stdout, stderr|
log.puts "[OUTPUT]:\n#{stdout.read}\n"
unless (err = stderr.read).empty? then
log.puts "[ERROR]:\n#{err}\n"
end
end
If you want to know more about other options you can refer to Ruby, Difference between exec, system and %x() or Backticks for links to relevant documentation.
You can try these approaches:
%x[command]
Kernel.system"command"
run "command"
make some file.rb with:
#!/path/to/ruby
system %{sudo mycommand1}
system %{mycommand2}
and the chmod the file with exec permissions (e.g. 755)
It you need to pass variables between the two commands, run them together:
system %{sudo mycommand1; \
mycommand2}

Hiding curl Window on Windows

One of the answers on a previous question mentioned that I can use curl to fetch a url; this can be done in Thread.new or in Process.spawn. But it seems that in either case, on Windows, I get a small command-prompt window appearing while curl is going out to the network.
I am invoking curl like this:
`curl "#{url}"`
Is there any way to hide the window so that it doesn't appear? Not only does it grab the focus away from the game (freezing it), but if I make frequent calls, it will be extremely annoying to the end user.
After some experimenting i got it working, no window when doubleclicking on the script, i use WMI from ruby, you just have to alter the path to curl.
IMPORTANT: save it with the extension .rbw and make sure that rubyw.exe is associated with that extenstion.
#hidden_curl.rbw
require 'win32ole'
HIDDEN_WINDOW = 0
cmd = '"C:\\Program Files\\curl\\curl.exe" --output c:\\test2.txt "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
objStartup = WIN32OLE.connect("winmgmts:\\\\.\\root\\cimv2:Win32_ProcessStartup")
objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
objProcess = WIN32OLE.connect("winmgmts:root\\cimv2:Win32_Process")
errReturn = objProcess.Create(cmd, nil, objConfig, nil)
Hope this works on your system.
You could try passing the -s option to curl which will make it run in silent mode.
Something like
`curl -s "#{url}"`
I am guessing this will not show up the console.
I think you problem is that you run the command in a subshell and the subshell in windows is CMD which is attached to a window.
I don't have a ruby to test the following but you could try :
IO#popen
Runs the specified command string as a subprocess;
The subprocess‘s standard input and output will be connected to the returned IO object.
=> http://ruby-doc.org/core-1.8.6/IO.html if IO is available to you
you could also try to run
start /B curl -s "#{url}" instead of just curl see http://www.computerhope.com/starthlp.htm for start specs.
This solution doesn't pop up a window on my W7 system
cmd = '"C:\Program Files\curl\curl.exe" --silent --output c:\test.txt "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
IO.popen(cmd)
or without creating a file
cmd = '"C:\Program Files\curl\curl.exe" --silent "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
IO.popen(cmd, "w+") { |io| puts io.readlines }

How can i execute 2 or more commands in the same ssh session?

I have the following script:
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh|
stdout = ""
ssh.exec("cd /var/example/engines/")
ssh.exec!( "pwd" ) do |channel, stream, data|
stdout << data if stream == :stdout
end
puts stdout
ssh.loop
end
and i get /root, instead of /var/example/engines/
ssh.exec("cd /var/example/engines/; pwd")
That will execute the cd command, then the pwd command in the new directory.
I'm not a ruby guy, but I'm going to guess there are probably more elegant solutions.
In Net::SSH, #exec & #exec! are the same, e.g. they execute a command (with the exceptions that exec! blocks other calls until it's done). The key thing to remember is that Net::SSH essentially runs every command from the user's directory when using exec/exec!. So, in your code, you are running cd /some/path from the /root directory and then pwd - again from the /root directory.
The simplest way I know how to run multiple commands in sequence is to chain them together with && (as mentioned above by other posters). So, it would look something like this:
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh|
stdout = ""
ssh.exec!( "cd /var/example/engines/ && pwd" ) do |channel, stream, data|
stdout << data if stream == :stdout
end
puts stdout
ssh.loop
end
Unfortunately, the Net::SSH shell service was removed in version 2.
You can just give different commands separated by a new line. Something like:
#result = ssh.exec!("cd /var/example/engines/
pwd
")
puts #result
Its probably easier (and clearer) to pass the command to a variable, then pass the variable into exec. Same principle though.
see if there's something analogous to the file(utils?) cd block syntax, otherwise just run the command in the same subshell, e.g. ssh.exec "cd /var/example/engines/; pwd" ?
Im not a ruby programmer, but you could try to concatenate your commands with ; or &&
There used to have a shell service which allow stateful command like your trying to do in net/ssh v1 but it has been remove in v2. However there's a side project of the author of net/ssh that allows you to do that. Have a look here: http://github.com/jamis/net-ssh-shell
The current location of net-ssh-shell is changed.
What I decided to use though to call a random shell script is to scp a file to the remote machine and source it into shell. Basically doing this:
File.write(script_path, script_str)
gear.ssh.scp_to(script_path, File.dirname(script_path))
gear.ssh.exec(". script_path")

Resources