Supply a string variable instead of a filepath as a command line argument
bash, command-line, macos, posix
Solution
Process substitution of the form `<(...)` sends the stdout of the commands in parenthesis to a special temporary file, and returns the path to that file. This is so that commands that only take a filename as an argument can read the output of other commands. You can see this by just echo'ing a substitution:
$ echo <(true)
/dev/fd/63
So if you wanted the contents of that special file to be the string "privatekeystuffdis88s8dsf8h8hsd8fh8d", you would want to do:
ssh -N -i <(echo "privatekeystuffdis88s8dsf8h8hsd8fh8d") -R 16186:localhost:8888 hello.com
UPDATE
The "special temporary file" is really just the file descriptor of the read end of a `pipe(7)` created by the shell. `/dev/fd` is a symlink to `/proc/self/fd`, so in the above example, the real "file" is actually `/proc/self/fd/63`, which would look something like `lr-x------ 1 user group 64 Mar 14 12:26 63 -> pipe:[1955808]` in a long listing.
What's important here is that it's not a regular file. It's a named pipe, which means that once data is read from the pipe, it's removed from the pipe. This is a problem for your use-case because it appears that `ssh` opens/closes the identity file multiple times:
$ strace ssh -vv -n -i ./identity-test some.server true 2>&1 | grep open.*identity-test
open("./identity-test", O_RDONLY) = 4
open("./identity-test", O_RDONLY) = 4
Enter passphrase for RSA key './identity-test':
Which means it's going to get different and incomplete data the second time it tries to open and read. So it would appear that you cannot use process substitution in this case.
Problem
I want to call OpenSSH from my app - which I am currently doing with a private rsa key filepath as an argument. I don't want to store the rsa file on disc due to security issues - is there a way to create a temp file to reference with the contents of the rsa file as a string variable? Bash Process subsituition looks promising and seems to work in terminal. So instead of this: ``` ssh -N -i /path/to/privatekey.rsa -R 16186:localhost:8888 hello.com ``` I would like to do something like this psuedo code: ``` ssh -N -i <("privatekeystuffdis88s8dsf8h8hsd8fh8d") -R 16186:localhost:8888 hello.com ``` I'm on OSX. (as an aside I am calling this all from an NSTask in Objective C)