Help me to write a script for Ubuntu (DigitalOcean).

Posted July 21, 2016 2.6k views
UbuntuPythonLinux Commands

can anyone help me how to create a shell or python script by which i can change root password when i create a new droplet. actually problem is that when i create a new droplet it email me ip, username and password to my email. its good. but i want to create a script by which i can change the password of root without any prompt from a php website. ( i am using ssh script to execute a command from a php and i will run this bash script from this php website). Script should be for Ubuntu.
when we when we want to connect our new droplet with using our ip and ssh password then it install all the packages. as it is required for new created droplet to change password first time.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

This can be done by using expect. First install expect on your server.

apt-get install expect

#!/usr/bin/expect -f
spawn ssh root@ip

# Remove this section if you are not expecting this message. 
expect "Are you sure you want to continue connecting (yes/no)?"
sleep 1
send "yes\r"

expect "password:"
sleep 1
send "<current_password>\r"

expect "(current) UNIX password:"
sleep 1
send "<current_password>\r"

expect "Enter new UNIX password:"
sleep 1
send "<new_password>\r"

expect "Retype new UNIX password:"
sleep 1
send "<new_password>\r"
  • MDS Thanks for reply. but i want a script by which i change root password of new droplet without prompt. when we create a new droplet we can not install any package on linux without changing the default password which is send by digitalocean. and it is required first time to change it. but install all packages without changing password of new droplet. how?