Jeremy Collins
Jeremy Collins

Reputation: 27

Automating a task through SSH with a script

I am having an issue with my web host changing the permission of one of my configuration files for my website. No matter how many times I change the permissions, they always revert back to writable after a day or so. The web host has been unable to resolve the issue, so I thought I'd try to use a script to ssh into my account and change the permissions daily.

My only problem so far is that it prompts me for my ssh key password in the terminal when I execute the script. How can I get this to work automatically so that I can set it to run daily from my computer without my intervention?

#!/bin/sh
ssh mydomain 'bash -s' << EOF
    cd public_html
    chmod 400 configuration.php
EOF

Thanks for any advice!

Upvotes: 0

Views: 49

Answers (2)

Jeremy Collins
Jeremy Collins

Reputation: 27

I was able to answer my own question after coming across a script on another user's question. I just had to think of a different way of getting the task done. Instead of logging in to my web host via ssh, I just created a script on my web host account and put it in the crontab.

#!/bin/bash

file=configuration.php

if [ -w "$file" ]
then
    chmod 400 "$file" && echo "The file permissions have been set to 400." >> log.txt
elif [ ! -w "$file" ]
    echo "The file is not writable." >> log.txt
fi

Upvotes: 0

matli
matli

Reputation: 28590

Add your public key to ~/.ssh/authorized_keys on the remote host. The key you are using should not have a password if you want to use it in this way.

Nowadays, this is simply done with the command

ssh-copy-id user@remote_server

Upvotes: 2

Related Questions