Operations 3 min read

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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Three Methods to Automate Password Input for sudo, ftp, and ssh Commands

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 <<EOF<br/>user username password<br/>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.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

passwordSudoexpect
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.