Shell script to automate SSH login using password
I'd recommend to use ssh keys instead of password if it's possible.
This script can help you to upload your public key to desired remote machine:
How to enter ssh password using bash?
Double check if you are not able to use keys.
Otherwise use expect:
spawn ssh firstname.lastname@example.org
How to enter commands/(SSH password) within the output of a command when using system() in C++?
No, you can not.
But perhaps you can use std::cin and std::cout for that, which I would NOT recommend.
I assume you are looking for a library similar to
paramiko which is, however, in python.
Make the ssh connection and enter the sudo password fully automatically
You need the
-c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:
/usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345\r"; interact;'
Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.
The Ansible script ('playbook') would look like this (untested):
- hosts: ip
- name: Update and upgrade apt packages
You can store the SUDO password in a file, and that file can be encrypted.