2

I know a similar question was posted, but I can't get it to work on my machine.

I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].

However, when trying to do so with my cURL command, I get no output.

This is my command:

curl -k --silent --ftp-pasv --ftp-ssl  --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt

and I use it with

x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)

but when I try to echo $x all I get is a newline...

I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server This Q is tagged with both sh, bash because I've tried it on both with same results

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • What do you get on `stdout` and `stderr` when you simply run `curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt` on bash? – alsjflalasjf.dev Aug 01 '18 at 16:35
  • I get `curl: (58) unable to use client certificate (no key found or wrong pass phrase?)` because the certificates are dummies, and that's OK, I just want this as a variable – CIsForCookies Aug 01 '18 at 16:42
  • Ok, so, at first glance at least, you have a problem with your `curl` command; no with `bash` or any `shell`; so, focus on get the desired output on the `curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt` command, and only then, assign that to any variable you like on `bash`; there are answers about that error on `curl` here, on SO – alsjflalasjf.dev Aug 01 '18 at 16:47
  • An FTP server doesn't produce an HTTP status. – tripleee Aug 01 '18 at 17:01
  • So that's why I can't get anything inside `x`? because the server didn't return output? – CIsForCookies Aug 01 '18 at 17:03
  • You can get 'anything' inside `x`; your problem is not to put the output of your `curl` command inside the variable `x`... your problem is before that, with your `curl` command. You curl command have an `stdin` (not important here), `stdout`, and `stderr`... if are not redirected, both of `stdout` and `stderr` goes to your terminal; when you do something like this `x=$(command)` you are assigning the `stdout` of your command to the variable `x`, and `stderr` goes to your terminal; so, before worrying about the `x`, fix your command... you need to get the desired output on your terminal first – alsjflalasjf.dev Aug 01 '18 at 21:07

1 Answers1

0

I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):

   --stderr <file>
          Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.

          If this option is used several times, the last one will be used.

When I use it like this:

curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl  --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt

I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.

I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124