4

I have found a way to upload an SSH key to my GitHub account with command line, but there is small problem.

I am able to do this with following command:

curl -u "user:pass" --data '{"title":"test-key","key":"ssh-rsa Aaa"}'

https://api.github.com/user/keys

But I am using this in Chef to add my nodes' keys to my GitHub account:

curl -u "user:pass" --data '{"title":"test-key","key":"`cat ~/.ssh/id_rsa.pub`"}' https://api.github.com/user/keys

but it is giving error.

What could be the reason?

techraf
  • 5,941
  • You'll probably want to put the cat command in $() so that it gets ran rather than literally putting that string of text in as your SSH key. – Bratchley Jul 21 '16 at 08:06
  • On mobile so I can't type it all out but you'll probably have to change your quotes as well. – Bratchley Jul 21 '16 at 08:07
  • You could also used stdin instead of an option value. http://unix.stackexchange.com/questions/77588/passing-binary-data-to-curl-without-using-a-file – Bratchley Jul 21 '16 at 08:10
  • i am using this is chef so i want a generic way to do this – amit singh Jul 21 '16 at 08:12

1 Answers1

5

The cat command results must be expanded using command substitution.

The syntax for bash is:

curl -u "user:pass" --data '{"title":"test-key","key":"'"$(cat ~/.ssh/id_rsa.pub)"'"}' https://api.github.com/user/keys

You can also use a classic backtick notation:

curl -u "user:pass" --data '{"title":"test-key","key":"'`cat ~/.ssh/id_rsa.pub`'"}' https://api.github.com/user/keys
techraf
  • 5,941
  • i am using this in chef to update my node's ssh -key in github. i am editing this in my question – amit singh Jul 21 '16 at 08:21
  • you first option did the trick, thnks very much – amit singh Jul 21 '16 at 08:24
  • To make this work with 2FA/TOTP add the header like so: -H ‘x-github-otp: 123456‘ according to the docs here: https://developer.github.com/v3/auth/#working-with-two-factor-authentication – Sami Samhuri Mar 28 '20 at 04:41