I ran this script #!/usr/bin/env python3 import requests import json import time from…
- User
I ran this script #!/usr/bin/env python3 import requests import json import time from cloudflare import CloudFlare
Set your Cloudflare API token and zone ID
api_token = ‘TOKEN_NAME_DDNS’ zone_id = ‘abcdefghijklmnop’
Set the A record to update
a_record_name = ‘ddns.toplevel.domain’
Create a new CloudFlare object
cf = CloudFlare(token=api_token)
Get the current public IP address of the Pi
def get_ip(): return requests.get(‘https://api.ipify.org’).text
Get the current IP address from CloudFlare
def get_current_ip(): records = cf.zones.dns_records.get(zone_id) for record in records: if record[‘name’] == a_record_name: return record[‘content’]
Update the A record with the current IP address
def update_ip(): current_ip = get_current_ip() new_ip = get_ip() if current_ip != new_ip: print(f’Updating A record {a_record_name} to IP {new_ip}') cf.zones.dns_records.put( zone_id, record_id, data={ ‘type’: ‘A’, ‘name’: a_record_name, ‘content’: new_ip, ‘proxied’: False, } )
Get the record ID of the A record
record_id = None records = cf.zones.dns_records.get(zone_id) for record in records: if record[‘name’] == a_record_name: record_id = record[‘id’]
Loop forever and update every 5 minutes
while True: update_ip() time.sleep(300)
and got these errors ./cloudflare_ddns.py: line 8: import: command not found ./cloudflare_ddns.py: line 9: import: command not found ./cloudflare_ddns.py: line 10: import: command not found ./cloudflare_ddns.py: line 11: from: command not found ./cloudflare_ddns.py: line 14: api_token: command not found ./cloudflare_ddns.py: line 15: zone_id: command not found ./cloudflare_ddns.py: line 18: a_record_name: command not found ./cloudflare_ddns.py: line 21: syntax error near unexpected token
(' ./cloudflare_ddns.py: line 21:cf = CloudFlare(token=api_token)' - Assistant
It seems like you are trying to run a Python script as a shell script. Make sure you are running this script using Python by running the command
python3 script_name.pyinstead of./script_name.py.Also, make sure you have installed the
requestsandcloudflarepackages using pip before running the script. You can install them by running the commandpip3 install requests cloudflare. - User
python3 cloudflare_ddns.py File "/home/rootcandy/scripts/ddns/cloudflare_ddns.py", line 14 api_token = ‘DDNS’ ^ SyntaxError: invalid character '‘' (U+2018)
- Assistant
The error message suggests that there is an issue with the quotes used in the script. It looks like you have used fancy quotes (
‘’) instead of regular single quotes ('') or double quotes ("") for theapi_tokenvariable.Replace the quotes in the
api_token,zone_id, anda_record_namevariables with regular quotes like this:api_token = 'TOKEN_NAME_DDNS' zone_id = 'abcdefghijklmnop' a_record_name = 'ddns.toplevel.domain'Then run the script again using the command
python3 cloudflare_ddns.py. - User
is there a terminal command I can run to replace all the incorrect quotes with the correct ones?
- Assistant
Yes, you can use the
sedcommand to replace all the incorrect quotes with the correct ones in the Python script.Assuming your Python script is called
cloudflare_ddns.py, you can use this command to replace all the incorrect quotes:sed -i "s/‘/'/g; s/’/'/g" cloudflare_ddns.pyThis command replaces all the fancy quotes (
‘and’) with regular single quotes (') in thecloudflare_ddns.pyfile. Once you run this command, you should be able to run the Python script without any quote-related errors.Make sure to backup your original Python script before running this command, in case anything goes wrong during the replacement process.
- User
sed: -e expression #1, char 1: unknown command: `▒' -bash: s/’/’/g”: No such file or directory
- Assistant
It looks like there are some hidden characters in the command that are causing the issue. Try copying the command again from the previous message and paste it into your terminal.
Alternatively, you can try typing the command manually instead of copying it:
sed -i "s/‘/'/g; s/’/'/g" cloudflare_ddns.pyThis command should replace all the fancy quotes with regular single quotes in your
cloudflare_ddns.pyfile.