R: change port to connect with SFTP server - r

I have a connection with an ftp server with the following code:
url <- "ftp://MyServer"
userpwd <- "MyUser:MyPass"
filenames <- getURL(url, userpwd = userpwd, ftp.use.epsv = FALSE, dirlistonly = TRUE, port = 22)
filen <- "MyFile.csv"
rawdata <- getURL(paste(url, filen, sep = ""), userpwd = userpwd, crlf = TRUE)
The file will be moved to an SFTP server, so I need to change the input. This new SFTP server is accessed via port 22 instead of the standard port 21. At the moment the connection fails with the following error
Error in function (type, msg, asError = TRUE) :
Failed to connect to MyServer port 21: Connection refused
It takes the wrong port, but how do I tell R to choose port 22?

You need to specify the SFTP protocol in the URL, so the line
url <- "ftp://MyServer"
should become
url <- "sftp://MyServer"
getUrl will then use the SSH port (22).

Related

Connection reset by peer when trying to connect to Postgres via SSL using R

I'm trying to connect to a database via SSL using the code suggested here: https://github.com/ropensci/ssh/issues/13
I listed the dummy code below that shows how I enable the connection and try to query some data. The solution works great for 'smaller' queries.
However, when I try to get 'larger' data, the query fails and R gives back the following error:
System failure for: recv() from user (Connection reset by peer) follwed by a fetching error Failed to fetch row: SSL error: decryption failed or bad record mac (see output in code snipped)
Accordingly to the 1st error message, I suppose the error occurs served-sided ('reset by peer' --> What does "connection reset by peer" mean?).
Is that true or is there a way to fix this error on the client side (in R)?
ssh::ssh_read_key(file = ssh::ssh_home("id_rsa"), password = "rsa_password")
cmd <- "session <- ssh::ssh_connect('user#host:port');ssh::ssh_tunnel(session, port = 5432, target = '127.0.0.1:5432')"
pid <- sys::r_background(std_out = T, args = c("-e", cmd))
dbcon <-DBI::dbConnect(drv = RPostgres::Postgres(),
dbname = "db_name",
host = "127.0.0.1",
port = 5432,
user = "db_user",
password = "db_password",
base::list(sslmode="require"),
service = NULL)
# example of working query
res <- DBI::dbGetQuery(conn = dbcon, statement = "SELECT * FROM small_table") #
# example of non-working query (see R-otutput)
res <- DBI::dbGetQuery(conn = dbcon, statement = "SELECT * FROM large_table;") #
## R-output
# Tunneled 31897311 bytes...Fehler: System failure for: recv() from user (Connection reset by peer)
# Ausführung angehalten
# Fehler: Failed to fetch row: SSL error: decryption failed or bad record mac
# Warnmeldung:
# Disconnecting from unused ssh session. Please use ssh_disconnect()
"Connection reset by peer" means that whatever you have tried connecting to has responded in an RST flag, meaning that they have reset the connection.

How to send a mail via SMTPS using the curl package in R?

I would like to send a mail using SMTPS in R. Currently, non of the available packages supports sending Mails via TLS (rmail & sendmaileR) or they have a hard to install Java dependency (mailr). I tried using curl and managed to send a mail using the following code snippet:
curl --url 'smtps://mail.server.com:465' --ssl-reqd --mail-from 'mail1#example.com' --mail-rcpt 'mail2#example.com' --upload-file mail.txt --user 'user:password'
Unfortunately, I could not translate that snippet into R using the brilliant curl package. While I managed to find all options, the curl statement crashes the R session every time. Furthermore, I was not able to add the mail.txt file to the request which I created in a temporary directory. Did someone manage sending mails using the curl package? Why does the program always crash? The goal should be to send mails on all platforms.
# input variables
to <- "mail1#example.com"
from <- Sys.getenv("MAIL_USER")
password <- Sys.getenv("MAIL_PASSWORD")
server <- Sys.getenv("MAIL_SERVER")
port <- 465
subject <- "Test Mail"
message <- c("Hi there!",
"This is a test message.",
"Cheers!")
# compose email body
header <- c(paste0('From: "', from, '" <', from, '>'),
paste0('To: "', to, '" <', to, '>'),
paste0('Subject: ', subject))
body <- c(header, "", message)
# create tmp file to save mail text
mail_file <- tempfile(pattern = "mail_", fileext = ".txt")
file_con <- file(mail_file)
writeLines(body, file_con)
close(file_con)
# define curl options
handle <- curl::new_handle()
curl::handle_setopt(handle = handle,
mail_from = from,
mail_rcpt = to,
use_ssl = TRUE,
port = port,
userpwd = paste(from, password, sep = ":"))
con <- curl::curl(url = server, handle = handle)
open(con, "r")
close(con)
# delete file
unlink(mail_file)

Error when using mailR package on Ubuntu machine

I have the following code:
text <- 'sample'
sender <- "***"
recipients <- c("***")
send.mail(from = sender,
to = recipients,
subject = text,
body = text,
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "***",
passwd = "***", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
The text variable is in my case a value that is fetched from a database but this makes it easier. The code above works fine when I run it on my local machine. However when I run it on Linux Ubuntu the exact same code gives me the following error:
org.apache.commons.mail.EmailException: Sending the email to the following
server failed : smtp.gmail.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl. java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43)
Can anybody tell me what could be the reason that I get this error on Linux enviroment (and not when I run it local)?

Cannot acess to a local database via R

I am trying to access to a database HWSD.md which can be downloaded here : http://webarchive.iiasa.ac.at/Research/LUC/External-World-soil-database/HTML/HWSD_Data.html?sb=4
I have a linux machine and I tried this command line but I have a message error
require(MonetDB.R)
Mydb <- src_monetdb("*/HWSD.mdb")
Error in socketConnection(host = host, port = port, blocking = TRUE, open = "r+b", :
cannot open the connection
In addition: Warning message:
In socketConnection(host = host, port = port, blocking = TRUE, open = "r+b", :
localhost:50000 cannot be opened
Anyone can help me out with it?

RCurl::getURL exceeds maximum number of clients

I am trying to list files hosted on the FTP servers of the MODIS Global Evapotranspiration Project (MOD16) recurrently.
## required package
library(RCurl)
## ftp server
ch_ftp <- "ftp://ftp.ntsg.umt.edu/pub/MODIS/NTSG_Products/MOD16/MOD16A2.105_MERRAGMAO/"
## list and reformat available subfolders
ch_fls <- getURL(ch_ftp, verbose = TRUE, dirlistonly = TRUE)
ls_fls <- strsplit(ch_fls, "\n")
ch_fls <- unlist(ls_fls)
## list files in current folder
for (i in ch_fls) {
ch_hdf <- paste0(ch_ftp, i)
getURL(ch_hdf, verbose = TRUE, dirlistonly = TRUE)
}
After some iterations, RCurl::getURL throws the following error message.
< 530 Sorry, the maximum number of clients (5) from your host are already connected.
* Access denied: 530
* Closing connection 16
Show Traceback
Rerun with Debug
Error in function (type, msg, asError = TRUE) : Access denied: 530
Obviously, RCurl::getURL opens connections to the FTP server during each iteration without closing them fast enough. A few minutes later, the server is accessible again, but the same error message will be thrown when re-initializing the script and waiting for the first few iterations. Is there a way to manually close the connections established by RCurl::getURL immediately after having retrieved a list of files?
I was dealing with the same issue.
Using Sys.sleep(2) fixed it for me.
## list files in current folder
for (i in ch_fls) {
ch_hdf <- paste0(ch_ftp, i)
getURL(ch_hdf, verbose = TRUE, dirlistonly = TRUE)
Sys.sleep(2)
}

Resources