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

July 21, 2016 374 views
Linux Commands Python Ubuntu

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.

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 serverpiolat.io install all packages without changing password of new droplet. how?

    • Are you able to run a bash script before serverpilot install packages?

      • no. i want to run change root password of a fresh droplet. but problem it that when we create a new droplet it require to enter old and new password. is it possible?

Have another answer? Share your knowledge.