托改革开放的福,家里用上了ipv6
虽然不是固定ip但是依然让我有了在外访问服务器的可能
于是想把ip地址发到邮箱里
整体上学习了linux服务器开机发送本机ip地址到指定邮箱 - 知乎
但是在获取ipv6地址上由于链接中的方法获取不到,使用了其他方法
代码如下:
def get_ip():
get_ipv6=os.popen("ip -6 address")
output=str(get_ipv6.read())
ip_strs=str.splitlines(output)
ip_strs_fixed=set([ip.strip() for ip in ip_strs])
global_ips=[ip for ip in ip_strs_fixed if ip.startswith("inet6 24")]
whether_to_send, send_ip = get_temp_ip(global_ips)
send_ip = json.dumps(send_ip)
return whether_to_send, send_ip
核心思路是 读取 ip -6 address的结果,然后进行分解
另外cron服务在CentOS中为cron,所以命令就改为
systemctl restart cron
systemctl enable cron
最后就是cron文件的编写注意下格式,
cron表达式 用户 命令(python或者python3)文件绝对地址
ipv6就是好,代码可以及时保存了(架设个git服务吧,我用的gogos)