简体   繁体   中英

Why am I getting an OSError: [Errno 2] trying to generate a public key?

I'm trying to create a public key from a private key that I'm generating with ec2/boto as seen below:

key_pair = ec2.create_key_pair(name)
private_key = '{}.pem'.format(name)

public_key = subprocess.check_output("ssh-keygen -y -f #{}".format(private_key))

At first, I thought maybe it was a permissions issue so I'm changing the permissions as such:

os.chmod(private_key, 0o400)

However, I'm still getting an OSError [Errno 2] error. The error can be seen below

Traceback (most recent call last):   
    File "infrastructure.py", line 425, in <module>
        main(arguments)   
    File "infrastructure.py", line 374, in main   
        key_pair_name=arguments['--keypair'])   
    File "infrastructure.py", line 387, in create
        key_pair_name, key_pair_fingerprint, key_pair_pem = create_key_pair(key_pair_name, region)   
    File "infrastructure.py",line 65, in create_key_pair
        raise e   
OSError: [Errno 2] No such file or directory

Try switching over to a non-shell-interpreted set of arguments for subprocess . In the process, consider also explicitly specifying the full path to ssh-keygen :

public_key = subprocess.check_output(["/usr/bin/ssh-keygen", "-y", "-f", private_key])

So I figured out my issue. There was an error on this line:

public_key = subprocess.check_output("ssh-keygen -y -f #{}".format(private_key))

I resolved it by doing the following:

os.chmod(private_key, 0o400) command = "ssh-keygen -y -f {}".format(private_key) public_key = subprocess.check_output(['bash','-c', command])

I still don't know exactly why this issue occurred but it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM