Three Methods to Automate Password Input for sudo, ftp, and ssh Commands
This article explains three techniques—using echo with a pipe, here‑document redirection, and an Expect script—to supply passwords automatically for sudo, ftp, and ssh commands, enabling non‑interactive execution of privileged or remote operations on Linux systems.
Method 1: Use echo to pipe the password into a sudo command, for example echo "123456" | sudo rm -rf /* , allowing a script such as clear.sh to run without interactive password entry.
Method 2: Use input redirection (here‑document) to feed both username and password to commands that accept them via standard input, such as ftp -i -n 192.168.21.46 < user username password EOF .
Method 3: Use an expect script to handle interactive prompts, setting a timeout, spawning the ssh command, waiting for the "password:" prompt, sending the password, and optionally keeping the session interactive, as shown in the example code.
Explanation of the Expect script: set timeout 30; spawn ssh -l username 10.125.25.189; expect "password:"; send "the_password"; interact.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.