各位,麻烦给改一下,使以下代码能提取到ubuntu server服务器的ipv6地址,谢谢。(目前只能提取到ipv4)
#!/usr/bin/python3
import socket
import os
import json
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from time import asctime
def send_an_email(email_content):
mail_host = "smtp.qq.com"
mail_user = "*******@qq.com"
mail_auth_code = "********"
mail_sender = mail_user
mail_receivers = "*****@qq.cn"
message = MIMEMultipart()
message['From'] = Header(mail_sender)
message['Subject'] = Header("ip地址")
message.attach(MIMEText(asctime(), 'plain', 'utf-8'))
message.attach(MIMEText(email_content, 'plain', 'utf-8'))
print("message is {}".format(message.as_string()))
smtpObj = smtplib.SMTP(mail_host)
smtpObj.login(mail_user, mail_auth_code)
smtpObj.sendmail(mail_sender, mail_receivers, message.as_string())
def get_temp_ip(current_ip):
temp_ip_json_path = "/var/tmp/ip.json"
if not os.path.exists(temp_ip_json_path):
print("No {}, dump it.".format(temp_ip_json_path))
with open(temp_ip_json_path, 'w') as jo:
json.dump(current_ip, jo)
return True, current_ip
else:
with open(temp_ip_json_path, 'r') as jo:
origin_ip = json.load(jo)
if origin_ip == current_ip:
print("Current ip {} do not change, no need to send".format(current_ip))
return False, current_ip
else:
print("The ip updated from {} to {}, update it.".format(origin_ip, current_ip))
os.remove(temp_ip_json_path)
with open(temp_ip_json_path, 'w') as jo:
json.dump(current_ip, jo)
return True, current_ip
def get_ip():
hostname = socket.gethostname()
addr_infos = socket.getaddrinfo(hostname, None)
ips = set([addr_info[-1][0] for addr_info in addr_infos])
global_ips = [ip for ip in ips if ip.startswith("24")]
whether_to_send, send_ip = get_temp_ip(global_ips)
send_ip = json.dumps(send_ip)
return whether_to_send, send_ip
if __name__ == "__main__":
whether_to_send, global_ips = get_ip()
if whether_to_send:
send_an_email(global_ips)
else:
print("wait and no send")