{"id":125,"date":"2017-10-15T13:23:14","date_gmt":"2017-10-15T19:23:14","guid":{"rendered":"http:\/\/xux.in\/blog\/?p=125"},"modified":"2021-06-19T19:41:58","modified_gmt":"2021-06-20T01:41:58","slug":"change-cpanels-email-password-through-command-line","status":"publish","type":"post","link":"http:\/\/xux.in\/blog\/post\/change-cpanels-email-password-through-command-line\/","title":{"rendered":"Change cPanel&#8217;s email password through command line"},"content":{"rendered":"<p>This script will help you change any email&#8217;s account password without using cPanel, this is done through the command line. It was written in PHP but mostly uses shell commands so I guess it was easier to use elements from both worlds.<\/p>\n<p>Use it at your own risk, this script requires to be executed as root and was done for educational purposes.<\/p>\n<pre lang=\"php\"> &lt;?php\n\n\/\/ Inspired on\tthe following articles:\n\/\/ https:\/\/cpanelgeek.wordpress.com\/2015\/08\/26\/resetting-email-account-password-from-command-line-in-cpanel\/\n\/\/ http:\/\/www.sudosu.in\/2013\/11\/cpanel-bash-script-to-change-password_11.html\n\nif (!isset($argv[1])) die(\"Usage: $argv[0] full@email [password]\\n\");\n\n\/\/ generate random 8 character password\nif (!isset($argv[2])) {\n  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!.-@';\n  $argv[2] = substr(str_shuffle($chars),0,8);\n}\n\n\/\/ validate email contains at least a @ and break it\nif (!preg_match(\"\/\\@\/\",$argv[1])) die(\"Invalid Email!\\n\");\nlist($name,$domain) = explode(\"@\",trim($argv[1]));\n\n\/\/ get domain owner\n$user = trim(exec(\"\/scripts\/whoowns $domain\"));\nif (!$user) die(\"Invalid Domain!\\n\");\n\n\/\/ encrypt password\n$cpass = trim(exec(\"openssl passwd -1 '$argv[2]'\"));\n\/\/echo \"$name @ $domain with $argv[2] $cpass\\n\";\n\n\/\/ get home folder\n$path = trim(exec(\"grep \\\"^$user:\\\" \/etc\/passwd | cut -d\\\":\\\" -f6\"));\n\n\/\/ get shadow line and prepare it\n$shadow = \"$path\/etc\/$domain\/shadow\";\n$tmpshadow = $shadow . \".tmp\";\n$rest = trim(exec(\"grep \\\"^$name:\\\" $shadow | cut -d\\\":\\\" -f3-\"));\nif (!$rest) die(\"Email $argv[1] does not exist!\\n\");\n\n\/\/ create tmp shadow file without the user's email\nexec(\"grep -v \\\"^$name:\\\" $shadow --------&gt; $tmpshadow\");\nexec(\"chown $user:$user $tmpshadow\");\nexec(\"chmod 640 $tmpshadow\");\n\n\/\/ add the new user's password\nexec(\"echo '$name:$cpass:$rest' &gt;&gt; $tmpshadow\");\n\n\/\/ swap shadow files and backup the old one\nexec(\"mv $shadow $shadow\".\".\".time());\nexec(\"mv $tmpshadow $shadow\");\necho \"$argv[1] password changed to: $argv[2]\\n\";\n?&gt;\n<\/pre>\n<p>Save it as change_password.php and execute it to see how to use it: php change_password.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script will help you change any email&#8217;s account password without using cPanel, this is done through the command line. It was written in PHP but mostly uses shell commands so I guess it was easier to use elements from both worlds. Use it at your own risk, this script requires to be executed as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,35,8],"tags":[13,27,33,21,15],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-code","category-cpanel","category-linux","tag-centos","tag-cpanel","tag-linux","tag-password","tag-php"],"_links":{"self":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":9,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":148,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/148"}],"wp:attachment":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}