Ssh Using R System() Command

ssh using R system() command

You could automate that acceptance (caveat: you have to be sure that the machines you're about to access are trusted!).

If the hosts have rsa keys, for instance, you can do:

system( 'ssh-keyscan  -t rsa Bs-ip-address  >> ~/.ssh/known_hosts' )

HIH.

Calling ssh with system in R shell eats subsequent commands

ssh eats up any stdin during the system() command. If you paste it line by line then ssh terminates before you submit a=a+1 and thus it gets passed to R instead of ssh. Use system("ssh .. < /dev/null") or system(..., input="") if you don't want terminal input to be eaten by the subprocess.

R - Connect via ssh and execute a command

Finally I solved it using the rPython package and the python's paramiko module, as there was no way to do it purely via R.

library(rPython)
python.exec(python.code = c("import paramiko",
"ssh = paramiko.SSHClient()",
"ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())",
sprintf('ssh.connect("%s", username="USER", password="PASSWORD") ', IP),
'stdin, stdout, stderr = ssh.exec_command("mca-status")',
'stats = stdout.readlines()'))

R Command not recognized when submitted with SSH

It appears that you have a PATH issue, where R is not on your PATH when you try to run the command through ssh.

If you specify the full path to R and Rscript on the remote host, it should resolve the problem.

If you are not sure what the full path is, try logging into the server and running which R to get the path.

how do you connect to a remote server with ssh in R

There is direct support for ssh/scp in RCurl:

x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username")

SSH within R script to get to MySQL Database

I accomplish a similar task with Postgres using SSH tunneling. Effectively, what you're doing with an SSH tunnel is saying "establish a connection to the remote server, and make a port from that server available as a port on my local machine."

You can set up a SSH tunnel using the following command on your local machine:

ssh -L local_port:lochalhost:remote_port username@remote_host

Specifically, what you're doing with this command is creating a Local Port Forwarding SSH tunnel, which is taking the port you'd connect to directly on the machine with your database installed (remote_port), and securely sending it to the machine you have R installed on as local_port.

For example, for a database server with the following options:

hostname: 192.168.1.3
username: mysql
server mysql port: 3306

You could use the following command (at the command line, or in R using system2) to create a tunnel to port 9000 on your machine:

ssh -L 9000:localhost:3306 mysql@192.168.1.3

Depending on what your exact DBI connection looks like in R, you may have to edit the connection configuration slightly to make it connect to your newly created tunneling port. The reason why I use a different localhost port is that it prevents conflicts with a local version of the database, if you've got one.

Sending system command with arguments in R

You can use

command = paste(
"export $(dbus-launch); \
export NSS_USE_SHARED_DB=ENABLED; \
libreoffice --calc",
FILE )
system(command)

How to do scp in R?

there is package ssh.utils which wraps ssh commands in R functions.

So your example would become:

library(ssh.utils)
cp.remote(remote.src = "",
remote.dest = "username@server_ip",
path.src = "localmachine/path_to_the_file",
path.dest = "path_to_remote_directory")

EDIT: I could install it but anyway it's not very flexible as it does not accept options...

With a system command this would be just:

system("scp localmachine/path_to_the_file username@server_ip:/path_to_remote_directory")


Related Topics



Leave a reply



Submit