Skip to content

Instantly share code, notes, and snippets.

@ceilfors
Last active June 3, 2023 01:48
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save ceilfors/fb6908dc8ac96e8fc983 to your computer and use it in GitHub Desktop.
Save ceilfors/fb6908dc8ac96e8fc983 to your computer and use it in GitHub Desktop.
ssh-copy-id for Windows
"""ssh-copy-id for Windows.
Example usage: python ssh-copy-id.py ceilfors@my-remote-machine
This script is dependent on msysgit by default as it requires scp and ssh.
For convenience you can also try that comes http://bliker.github.io/cmder/.
"""
import argparse, os
from subprocess import call
def winToPosix(win):
"""Converts the specified windows path as a POSIX path in msysgit.
Example:
win: C:\\home\\user
posix: /c/home/user
"""
posix = win.replace('\\', '/')
return "/" + posix.replace(':', '', 1)
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--identity_file", help="identity file, default to ~\\.ssh\\idrsa.pub", default=os.environ['HOME']+"\\.ssh\\id_rsa.pub")
parser.add_argument("-d", "--dry", help="run in the dry run mode and display the running commands.", action="store_true")
parser.add_argument("remote", metavar="user@machine")
args = parser.parse_args()
local_key = winToPosix(args.identity_file)
remote_key = "~/temp_id_rsa.pub"
# Copy the public key over to the remote temporarily
scp_command = "scp {} {}:{}".format(local_key, args.remote, remote_key)
print(scp_command)
if not args.dry:
call(scp_command)
# Append the temporary copied public key to authorized_key file and then remove the temporary public key
ssh_command = ("ssh {} "
"mkdir ~/.ssh;"
"touch ~/.ssh/authorized_keys;"
"cat {} >> ~/.ssh/authorized_keys;"
"rm {};").format(args.remote, remote_key, remote_key)
print(ssh_command)
if not args.dry:
call(ssh_command)
@steph-ben
Copy link

Greate snippets, thanks ! Also you can use the following command to convert "Putty-style" public key to "Open SSH" style

ssh-keygen -i -f keyfile.pub > newkeyfile.pub

See http://stackoverflow.com/questions/2224066/how-to-convert-ssh-keypairs-generated-using-puttygenwindows-into-key-pairs-use

Copy link

ghost commented Jun 14, 2015

Very useful. Had to comment out the mkdir and touch at lines 39 and 40 because my server already has those but otherwise does what it says. Excellente!

@amosbird
Copy link

nice snippet. Line 39 should change to mkdir -p xxx

@ferielboa
Copy link

i'd like to know how i could complie this code with the Cmd of windows.
Please could u write me down the command line.
Thanks

@csaladenes
Copy link

csaladenes commented Jul 8, 2017

Any chance to add a custom port?
UPDATE: just did, see my fork ;)

@csaladenes
Copy link

@ferielboa: just copy this text and save it as ssh-copy-id.py. Then, from the folder where you saved it call python ssh-copy-id.py ceilfors@my-remote-machine. You can also add a custom port for the ssh/scp connection in my fork with the -p flag. It will ask you to login twice, once for the scp and once for the ssh connection.

@eliassal
Copy link

Hi, can you please tell me with which version of python this script is compatible? I am trying to run it on win 10 python 3.8 but I get the following error

python ssh-copy-id.py salam@fedora
Traceback (most recent call last):
  File "ssh-copy-id.py", line 23, in <module>
    parser.add_argument("-i", "--identity_file", help="identity file, default to ~\\.ssh\\idrsa.pub", default=os.environ['HOME']+"\\.ssh\\id_rsa.pub")
  File "C:\ProgramData\Anaconda3\lib\os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'HOME'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment