• windows使用python脚本实现ssh-copy-id免密钥ssh登录linux


    1 windows 安装openssh

    2 打开cmd,运行命令行 ssh-keygen -t rsa 

    3 进入用户名/.ssh文件夹

    4 使用ptyhon 同步 公钥 id_rsa.pub至linux服务器

     python ssh-copy-id.py -i :c:/users/{根换自己当前登录用户}/.ssh/id_rsa.pub root@192.168.56.101

    5 按提示要求输入linux密码2次就可以完成windows授权,再次使用 ssh root@192.xx.xx.xx 就可以直接登录了

      ps:如果直接 python ssh-copy-id.py root@192.168.56.101 会提示找不到公钥文件

    """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/.
    """
    #ssh-copy-id.py
    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)
  • 相关阅读:
    点击图片显示原图
    SQL判断语句
    窗口淡入淡出效果
    判断两段时间之间的时间差
    软件行业发展趋势
    VSS客户端不能访问问题“unable to open user login file\\服务器项目管理目录\data\logedin\用户名.log ”
    鑫哥儿子顺利降生了!
    面向对象原则之单一职责原则实现
    PHP编码,乱码问题
    泛型中的default(T)
  • 原文地址:https://www.cnblogs.com/martinsun/p/15143072.html
Copyright © 2020-2023  润新知