Question

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

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. Example: when we when we want to connect our new droplet with serverpiolat.io 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.

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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"