mcdito13
mcdito13

Reputation: 321

I can ssh in manually, but not from a script - Permission denied (publickey)

So I'm currently working on a load balancing python script. In this script I will need to update a file on a server. My plan is to have my python script call a bash script.

In that bash script I'd like to ssh into the server, execute an awk command on a file, then logout.

I can currently ssh into this server manually, because I've set up an ssh key (using Google Cloud Platform). But when I try to run a bash script that only executes

'ssh username@externalIP'

I get the error: Permission denied (publickey)

What am I missing here?

Upvotes: 3

Views: 1981

Answers (2)

Dalvenjia
Dalvenjia

Reputation: 2033

Probably your private key is in your home directory and python is spawning the bash process as a limited user, try changing your script to include the private key explicitly, if still doesn't work you will have to copy the key and change its permission

To explicitly add the private key:

ssh -i /path/to/private/key user@host

Upvotes: 1

monte
monte

Reputation: 555

Why not use paramiko to connect via SSH? You can specify a key, as shown in this gist.

By using this, you can easily set which commands to execute on the server.

Upvotes: 1

Related Questions