How to configure parallel remote kernels in Mathematica? - wolfram-mathematica

When I try to configure remote kernels in mathematica via Evaluation>Parallel Kernel Configuration ... then I go to "Remote Kernels" and add hosts. After that I try to Launch the remote kernels and only some of them get launched (the number of them varies). And I get a msg like the following.
KernelObject::rdead: Subkernel connected through remote[nodo2] appears
dead. >> LinkConnect::linkc: Unable to connect to
LinkObject[36154#192.168.1.104,49648#192.168.1.104,38,12]. >>
General::stop: Further output of LinkConnect::linkc will be suppressed
during this calculation. >>
Any ideas how to get this working?
Take into account it sometimes does load some of the remote kernels but never all of them. Thanks in advance.
This is my ouput for $ConfiguredKernels // InputForm
{SubKernels`LocalKernels`LocalMachine[4],
SubKernels`RemoteKernels`RemoteMachine["nodo2", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo1", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo3", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo4", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo5", 2]}
Once it did load all of the kernels, but it commonly doesn't, just one or two remote kernels.

There is very little information given, so this answer may not be 100% useful.
The first issue to always consider is licensing on the remote machine. If some kernels launch, but others don't, it is possible you have run out of licenses for kernels on that machine. The rest of this post will assume licensing is not the issue.
Connection Method
The remote kernel interface in Mathematica by default assumes the rsh protocol, which is not the right choice for many environments, because rsh is not a very secure protocol.
The other option is ssh, which is much more widely supported. There are many ssh clients, but I will focus on a client included with Mathematica, namely WolframSSH.jar. This client is java based, which has the added benefit of working the same on all platforms supported by Mathematica (Mac, Window and Linux).
To avoid having to type a password for every kernel connection, it is convenient to create a private/public key pair. The private key stays on your computer and the public key needs to be placed on the remote computer (usually in the .ssh folder of the remote home directory).
To generate a private/public key pair you can use the WolframSSHKeyGen.jar file, like so:
java -jar c:\path\to\mathematica\SystemFiles\Java\WolframSSHKeyGen.jar
and follow the instructions on the dialogs that come up. When done, copy the public key to the .ssh folder
on the remote machine. In my case, I called the keys kernel_key and kernel_key.pub was automatically named that way.
You can now test the connection from a command line, like so (using the ls command on the remote machine):
java -jar c:\path\to\mathematica\SystemFiles\Java\WolframSSH.jar --keyfile kernel_key arnoudb#machine.example.com ls
If this works, you should be able to finish on the Mathematica side of things.
Remote Kernel Connection
To make a connection you need the following settings, the name of the remote machine:
machine = "machine.example.com";
The login name, usually $UserName:
user = $UserName;
The ssh binary location:
ssh = FileNameJoin[{$InstallationDirectory, "SystemFiles", "Java", "WolframSSH.jar"}];
The private key as described above:
privatekey = "c:\\users\\arnoudb\\kernel_key";
The launch command for the kernel:
math = "math -mathlink -linkmode Connect `4` -linkname `2` -subkernel -noinit >& /dev/null &";
A configuration function to put everything together:
ConfigureKernel[machine_, user_, ssh_, privatekey_, math_, number_] :=
SubKernels`RemoteKernels`RemoteMachine[
machine,
"java -jar \"" <> ssh <> "\" --keyfile \"" <> privatekey <> "\" " <> user <> "#" <> machine <> " \"" <> math <> "\"", number]
This uses the configuration function and defines it to use 4 remote kernels:
remote = ConfigureKernel[machine, user, ssh, privatekey, math, 4]
This launches the kernels:
LaunchKernels[remote]
This command verifies if the kernels are all connected and remote:
ParallelEvaluate[$MachineName]

Related

Redirect input to a Java application inside a bash script

I am using the ftm cli by Oracle. When I run a command, it asks for a password by saying "Enter a password". I am writing a script which uses this command and so I want to enter the password programatically.
Here's what I have tried -
echo "password" | java -jar ftmcli.jar list
But this doesn't work. Any idea what to do to make it work?
Without being able to look into the source code of ftmcli, I presume that Oracle is using Console.readPassword() from the java.io.Console class. However, there is a snag with this function:
public final class Console
Methods to access the character-based console device, if any,
associated with the current Java virtual machine.
Whether a virtual machine has a console is dependent upon the
underlying platform and also upon the manner in which the virtual
machine is invoked. If the virtual machine is started from an
interactive command line without redirecting the standard input and
output streams then its console will exist and will typically be
connected to the keyboard and display from which the virtual machine
was launched. If the virtual machine is started automatically, for
example by a background job scheduler, then it will typically not have
a console.
If this virtual machine has a console then it is represented by a
unique instance of this class which can be obtained by invoking the
System.console() method. If no console device is available then an
invocation of that method will return null.
By piping/redirecting the stdout of echo to the JVM, the Java method System.console() will return null and hence not read anything from your redirection.
Look at the following questions which handle the same problem, but on Java side: "How to handle java passwd reading when System.console() returns null?" or How to pipe input to Java program with bash
On your side, there is more or less nothing you can really do. Executing echo $password | java -jar ftmcli.jar list or java -jar ftmcli.jar list <<< $password will always fail as long Oracle doesn't change the way how ftmcli reads the password from stdin.
If you are ready for a one time password setup, then follow the below steps.
run java -jar ftmcli.jar --save-auth-key
You will be prompted for teh password and once it is given, the file will be uploaded and a ftmclikeystore file is created under ftmcli folder.
Now as long as if you don't change the user, then ftmcli will take the password from this file.
Found this little but fantastic tool called expect does the magic:
$ ./expect.script
spawn java -cp /tmp/brm-test/oraclepki.jar:/tmp/brm-test/BRMActions.jar com.oracle.installer.brm.BRMInstallHelper 7 /tmp/brm-test/client upgC
Enter Password for the wallet:
$
Perhaps it's not very much visible in the snippet above, but it is working:
$ cat /tmp/brm-test/client/.wp
upgC=MyMag1cPa$$word#
What is in the expect.script?
$ cat expect.script
#!/usr/bin/expect
stty -echo
spawn java -cp /tmp/brm-test/oraclepki.jar:/tmp/brm-test/BRMActions.jar com.oracle.installer.brm.BRMInstallHelper 7 /tmp/brm-test/client upgC
expect "Enter Password for the wallet:"
send "MyMag1cPa$$word#\r"
interact
Seems this can be used also in Chef, see this cookbook.
I hope it will help,
Jarek

Writing Automated scripts to configure device

My requirement is like this:
I need to log in to a remote device (say Router/switch) and execute following commands.
telnet xx.xx.xx.xx
//give password here
sys
interface g x/x/x
shut
desc free-port
exit
There are Hundreds of devices for which I cannot waste time doing above damn thing 100 times. I need to write a automated script which does it. so My questions are as follows:
I use Windows system, so What is the best scripting language to be used : Ruby / shell script / perl ? (I was formerly ROR Developer, so i know Ruby, Linux terminal. Now I am working in networking domain. )
What I thought was : Put all Devices into an array and using for loop, call devices one by one and execute above said commands.
I don't have knowledge of scripting, so please guide me further. I don't know where to start from.
Step 1: decide the file structure of your program.
For example, this is the simplest structure
if_admin/
|--config.yml
|--run.rb
Step 2: write a config file or a bunch of config files that contain the different parts of the commands you need to run on the targets.
For example, you can use a yaml file like this:
xx.xx.xx.xx:
password: s3cret
router-shelf: x
slot: x
port: x
yy.yy.yy.yy:
...
Step 3: implement what you want to do
require 'yaml'
require 'net/telnet'
config = YAML.load_file('./config.yml')
config.each do |host, conf|
telnet = Net::Telnet.new('Host' => host)
telnet.login(conf['password'])
telnet.puts <<-CMD
sys
interface g #{conf['router-shelf']}/#{conf['slot']}/#{conf['port']}
shut
desc free-port
CMD
telnet.close
end
If you can use expect script , you are in luck.
#!/usr/bin/expect
set timeout 60
set cmds [list "ssh host1 ..." "ssh host2 ..." "ssh host3 ..."]
foreach cmd $cmds {
spawn -noecho bash -c $cmd
expect {
-re "password" {
exp_send "$env(PASS_WORD)\"
exp_continue
}
eof { wait } ; # at this time the last spawn'ed process has exited
}
}
Here is the rough idea of above script :-
set cmds [list.... will be used as list to store set of commands.
foreach will iterate though those commands
spawn will spawn process for each of the command. you can write multiple command with single telnet in bash, just break down commands using \ (backslash) so it is easily readable and extendable.
expect block will pass password whenever it encounter certain regex.
eof will wait till all commands in spawn process are finish.
set timeout -1 will keep loop running. i think default time for expect script is 10secs.
You can create one more foreach loop for host-list.
I think this will be enough to get you started for your automation process.
As to the question of "What is the best scripting language to be used", I would say go with one that does what you need and one that you're comfortable with using.
If you want to go with Perl, one module that you could use is Net::Telnet. Of course, you'll need Perl itself. I'd recommend using Strawberry Perl, which should already have Net::Telnet installed.
Another possible route is to use putty, which is a SSH and telnet client. You could combine that with TTY Plus, which provides an interface that uses tabs for different putty sessions. And it lets you issue commands to multiple putty sessions. This is one possibility that wouldn't involve a lot of code writing.

Execute multiple commands via SSH and PowerShell

I successfully managed to connect to a Cisco IE-2000-L switch via SSH. I used the Renci SSH.NET library.
Starting guide: http://vwiki.co.uk/SSH_Client_(PowerShell)
My working code is
# Load SSH library (for .NET 4.0 and PowerShell 3)
$DllPath = "D:\temp\Renci.SshNet.dll"
[void][reflection.assembly]::LoadFrom( (Resolve-Path $DllPath) )
# Connect to switch (Cisco IE2000-L) with IP, port, username, password
$SshClient = New-Object Renci.SshNet.SshClient('172.20.91.30', 22, 'admin', 'mypassword')
$SshClient.Connect()
# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
# show result
$SshCommand.Result
# close SSH connection
$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()
My problem is
The above code sends just one command. But I want to execute several commands consecutively without closing and reopening a session.
If I add a second command right after the first one
# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
$SshCommand = $SshClient.RunCommand('show start')
...the script hangs and never finishes. What am I doing wrong?
Minor relevant information
My main goal is to send multiple commands at once to a Cisco switch
I already tried Plink together with batch cmd input. It's not reliable enough. It works sometimes and sometimes not.
I already tried telnet scripting. Too awkward.

How to enter a password into another process prompt from Ruby

I am writing an application that needs to run command on a remote Raspberry PI using a revssh script. revssh is a custom script that implements to some level the Revssh protocol concepts. it uses ssh reverse tunneling to send commands from the server to the clients.
I am using Ruby 2.1, I tried to do this using IO.popen but it does not work, so I tried the following:
# revssh (short for reverse ssh ) enables the execution of remote commands
# from the server on connected clients, like the 'psu_pi_analytics' here. but it requires
# to enter a root password each time you want to run a command using 'revssh -c'
IO.popen('revssh -c psu_pi_analytics uname -a', 'w+') do|io|
io.puts 'password' # enter the password when prompted
puts io.gets
end
this code work if the command to execute run on the local machine, but not in my case.
So any thoughts, or suggestions.
What important here is how to deal with the new connection created by the revssh script using ssh, which is managed in the terminal if the script is run directly from the terminal.
Edit:
By not work I mean it still prompts for the password, even if I puts the password to the io.
You can use an Expect-like library (e.g. RExpect, Expect4r) for interacting with other processes.
Another question related to this: Is there an Expect equivalent gem for Ruby?

Scripting SFTP unattended download

I am trying to connect to an SFTP server via software called WinSCP which is a Secure FTP client, I can script it to work by sending keys and doing it in a psuedo unattended mode, but the Server has to have a user logged in to send the keys, I need WinSCP to logon and transfer the files like a service where the console doesn't launch.
I have tried following the Tutorials on the
WinSCP website (for automated/unattended transfers but it gives me errors: Cannot created object, or cannot find library (from the dll file that I have associated with the COM)
The error I get when I run the following code is:
line 13, Char: 2
Could not created object named "WinSCP.SessionOptions"
Code: 80040154
Source: Wscipt.CreateObject
I should also probably mention that I get a similar error about line 21 or 22 about creating the Session object, when I remove the code on line 13 to see if it was the only issue
<job>
<reference object="WinSCP.Session"/>
<script language="VBScript">
Option Explicit
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Sftp
.HostName = "host"
.UserName = "username"
.Password = "password"
End With
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Connect and Get
session.Open sessionOptions
session.GetFiles("/Fromn/*.*", "C:\testFilesFrom").Check()
' Disconnect, clean up
session.Dispose
</script>
</job>
Has anyone had any experience scripting this kind of job where the server is not logged on and the script can run an SFTP session? Is there somethign I am doing wrong or is this not possible?
WinSCP was made for interactive use. Use PSCP for non-interactive jobs. And I'd strongly recommend using public key authentication instead of password authentication.

Resources