File: //proc/self/root/usr/local/ucloud/reset_account.sh
#!/bin/sh -e
# centos 8.x, rocky 8.5 supports custom python, which is located in "/usr/libexec/platform-python";
# others, include redhat and debian, supports normal python.
if [ -x /usr/libexec/platform-python ]; then
sys_python=/usr/libexec/platform-python
else
sys_python=$(which python python3 python2 2>/dev/null | head -n1)
fi
if [ -z "${sys_python}" ]; then
echo "[WARNING]no available python, change passwd failed."
exit 1
fi
# send feedback to UCloud first
#curl -s http://100.80.80.80/uhost/feedback -d "reset-account=ok"
${sys_python} - <<EOF
def http_post(url, data):
try:
import urllib, urllib2
body = urllib2.urlopen(url, urllib.urlencode(data)).read()
except ImportError:
import urllib.request as request, urllib.parse as parse
req = request.Request(url, parse.urlencode(data).encode('utf-8'))
body = request.urlopen(req).read()
return body.decode('utf-8')
import sys
sys.stdout.write(http_post('http://100.80.80.80/uhost/feedback', {'reset-account': 'ok'}))
EOF
# reset users & passwords & ssh pubkeys
cloud-init single --frequency always --name cc_users_groups
cloud-init single --frequency always --name cc_set_passwords
cloud-init single --frequency always --name cc_ssh
# restart sshd
service sshd restart 2>/dev/null || service ssh restart