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
How to Fix Urllib3 Runtimeerror: Requests Dependency 'Urllib3' Must Be Version >= 1.21.1, < 1.22
Laravel-Mix No Build Notification
Bash: Ctrl+C During Input Breaks Current Terminal
Understanding Glibc Malloc Trimming
Get Man Pages for Driver Functions
Linux >2.6.33: Could Sendfile() Be Used to Implement a Faster 'Cat'
Which Is Faster of Two Case or If
Qfiledialog: How to Filter Only Executables (Under Linux)
Linux: How to Send a Whole Packet to a Specific Port on Another Host
Infinite Loop Receive from Serial Port
Gatttool: Limited to 5 Connections
Get Canonical Path from Pathname
Avoid Copying of Data Between User and Kernel Space and Vice-Versa
Why Is Capeff All Zeros in /Proc/$Pid/Status