generating SSH key for github: "zsh: command not found: $" - macos

I'm trying to configure github with my macOS system. I use iTerm and zsh. When I try to generate a new ssh key according to the instructions from the https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key I get an error "zsh: command not found: $". Please help.

$ is what you see in sh. In zsh you probably see [path#user] $ or something like that. You just mustn't copy this dollar sign. What you copy should by ONLY:
ssh-keygen -t rsa -b 4096 -C "your_email#example.com"

Runing it in bash instead works for me
exec bash
then
ssh-add -K ~/.ssh/id_ed25519
And you can switch back to zsh by
exec zsh

avoid copying dollar sign while generation ssh keys.
use this ----> ssh-keygen -t rsa -b 4096 -C "your_email#example.com"

Related

Run a script on remote server with ssh password or key

I'm trying to run a script on a remote server with either password credentials or .pem key access and I'm getting errors no matter which solution I've found etc.
bash script content:
#!/bin/bash
sudo fdisk -l
ssh -T -i "~/.ssh/keys/key.pem" ubuntu#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: bash: /dev/fd/63: No such file or director
ssh user#host.com 'echo "password" | sudo bash <(wget -qO- http://www.host.io/do.sh)'
Error: sudo: a password is required
ssh -t user#host.com "echo password | sudo fdisk -l"
Works but still gives me the password propmt
echo -t pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
echo -tt pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: bash: /dev/fd/63: No such file or directory
// And I also get the password prompt
echo -tT pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
// And I also get the password prompt
// This works but I still get the password propmt
ssh user#host 'echo "password" | sudo -S sudo fdisk -l'
These are different variations of the supposed solutions from other places.
What I'm trying to do:
Is to run a script from a URL on the remote server while echoing the password to the cmd so I don't get propmt to input the password manually.
To be able to do the same thing above with using the .pem key variant also
For an explanation for commands except the first one, You can't do stdin-redirect a password to ssh if ssh requires interactively. ssh only allows manual typing if you use a password.
Your first error said that bash can't read a file descriptor. So ssh via ~/.ssh/keys/key.pem works. To run the shell command on the fly,
ssh -T -i "~/.ssh/keys/key.pem" ubuntu#host "curl -fsSL http://host.com/do.sh | sudo bash"
Does your script really need to run with sudo??
If not, then try this:
ssh user#host "curl -s -o do.sh 'http://host.com/do.sh'; source do.sh"

Bash remote ssh command is not working because of premature expansion

I'm trying to delete the contents of a remote directory in a bash script and leaving the folder intact by using ssh like this:
# First attempt
inboxResult=$(ssh -t -t username#host sudo -u rootUser rm -Rf /my/path/here/inbox/*)
# Second attempt
inboxResult=`ssh -t -t username#host sudo -u rootUser rm -Rf /my/path/here/inbox/*`
but it keeps failing silently. I've done my research and it seems like the '*' is being expanded before the command is sent via ssh to the remote host, but I would want the opposite. I couldn't find any solution and I've tried more than these two but they seem to be far from what I was looking.

Powershell issue - executing command in Git Bash

I'm trying to write a script that's part of a much bigger automation script that configures the GitHub ssh key on a local dev machine.
This is the line I'm trying to run but for some reason the 'eval $(ssh-agent -s)' fails as it errors and outputs this message.
'eval' is not recognized as an internal or external command,
operable program or batch file.
cmd.exe /c "ssh-keygen -t rsa -b 4096 -C "$githubEmailAddress" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub"
I have looked around and I'm having no luck getting past this issue. I can't work out how to launch the Git Bash terminal where the command works from the ps1 script.
You should consider creating an alias for bash.exe that way it is only referenced one time in the script and will be easier to change if you need to in the future.
You can then create the SSH key as shown:
New-Alias -Name gitBash -Value "$Env:ProgramFiles\Git\bin\bash.exe
gitBash -c "ssh-keygen -t rsa -b 4096 -C "blah#gmail.com" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub && exit"
You also won't have to do as much encoding as the key gen parameters won't need to be wrapped in a string delimiter anymore.
You also avoid managing the current directory manually like you are with the cd command.
Also note the use of the $Env:ProgramFiles to get the base path of the program files directory, its not common for it to be configured differently than "C:\Program Files" but it can be and it avoids issues with spaces in the path name this way.
After a lot of trial and error the way you can open a git bash prompt from powershell is to find the bin\bash.exe file normally found in a folder called Git within the Program Files folder.
From the location of the ps1 file you might need to modify the ....\ part but the script below allow you to open a bash prompt execture a bunch of calls and then exit the prompt
cmd.exe /c 'cd "..\..\Program Files\Git\bin" && bash.exe -c "ssh-keygen -t rsa -b 4096 -C "blah#gmail.com" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub && exit"'

Why -f flag of ssh-keygen fails to follow symlink?

My ~/.ssh directory is a symlink to /somewhere/else/.ssh/
Now, the following works perfectly; and the demo key ends up getting created at /somewhere/else/.ssh/Official, as expected.
export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo
However, when -f is supplied with the same path but via a variable, it fails with the following error:
Saving key "~/.ssh/Official/demo" failed: No such file or directory
export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}
I have tried several ways to supply this variable, but nothing worked. I'm not able to find anything about variables in the documentation either. I wish to know why does -f fail to follow the symlink path ONLY if passed via a variable? Is there a workaround? I'm not sure but, would it be recommended to bring this to notice here?
EDIT: Updating question after debugging. The original question has been preserved below:
I am aware that ssh-keygen has a flag -f to specify input_keyfile - With which, once can create a key with custom name at a custom location. However, this fails if the input_keyfile is a variable.
How do I provide the key path as a variable to ssh-keygen?
Following are oversimplified snippets from the bigger code:
This works perfectly:
export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo
However, this fails with the following error Saving key "~/.ssh/Official/demo" failed: No such file or directory
export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}
I have tried wrapping ${SSHKey_Path} in several ways, but nothing worked:
", ', $(echo ${SSHKey_Path}), and many more.
The failure is not in the variable, but in the interpretation of ~.
Try
export SSHKey_Path=~/.ssh/Official/demo
or
export SSHKey_Path="$HOME/.ssh/Official/demo"

Syntax error when calling variable in bash

Here is my code:
#!bin/bash
id=$(sshpass -p password ssh -tt username#ipaddress -p PORT "grep --include=\*.cr -rlw '/usr/local/bin/' -e '$1' | cut -c16-")
echo $id
sshpass -p password rsync -avHPe 'ssh -p PORT' username#ipaddress:/usr/local/bin/"$id" /usr/local/bin/
id echos correctly, but I get an rsync error when trying to call the variable.
If I manually populate and run rsync, the command works, so I'm not sure what is going on.
Rsync gives me the following output on error.
rsync: link_stat "/usr/local/bin/match.cr\#015" failed: No such file or directory (2)
It seems to be grabbing extra characters? Any help is appreciated :)
Looks like your file contains Windows specific "CR LF" characters. You need to convert these to Linux specific "LF" characters in your script. You can use a tool like dos2unix or Notepad++.

Resources