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:
https://github.com/aprey10/ssh-authorizer
How to enter ssh password using bash?
Double check if you are not able to use keys.
Otherwise use expect:
#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact
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
tasks:
- name: Update and upgrade apt packages
become: true
apt:
upgrade: yes
You can store the SUDO password in a file, and that file can be encrypted.
Related Topics
Which Linux Ipc Technique to Use
Is There Really No Asynchronous Block I/O on Linux
"/Usr/Bin/Ld: Cannot Find -Lz"
How to Set Linux Environment Variables With Ansible
How to Use Debug Version of Libc
Return Value of Sed For No Match
What Is the Maximum Size of a Linux Environment Variable Value
The Bash Command :(){ :|:& };: Will Spawn Processes to Kernel Death. Can You Explain the Syntax
Modify Qt Gui from Background Worker Thread
How to Access the System Call from User-Space
How to Deal With a Filename That Starts With the Hyphen (-) Character
How to Upgrade Glibc from Version 2.12 to 2.14 on Centos
Why Does /Bin/Sh Behave Differently to /Bin/Bash Even If One Points to the Other
What Does Localhost Means Inside a Docker Container
What's the Difference Between "Statically Linked" and "Not a Dynamic Executable" from Linux Ldd