Operations 3 min read

Using Jenkins to Execute Shell Commands and Batch Distribute Host Keys

This tutorial explains how Jenkins can be used to run simple shell commands such as retrieving host information and how to automate bulk host‑key distribution with a custom script, illustrating the CI platform’s integration with version control and its ability to execute OS‑level operations.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using Jenkins to Execute Shell Commands and Batch Distribute Host Keys

Sometimes developers need to run operating‑system level commands or perform low‑level tasks, and using OS‑level commands is the most straightforward way to achieve this.

Jenkins is an open‑source continuous‑integration system that simplifies the integration process, allowing developers to focus on business logic while providing real‑time error monitoring, detailed logs, alerts, and visual charts that show build trends and stability.

Jenkins can create an automated CI environment that compiles, packages, and deploys applications; it works with build tools such as Ant, Maven, and Gradle, integrates seamlessly with SVN and Git, and can connect directly to popular code‑hosting services like GitHub and Bitbucket.

A simple test demonstrates executing a shell command (e.g., ifconfig hostname) through Jenkins by selecting the “Shell” build step and clicking “Build Now”, after which the console output can be inspected.

The tutorial also shows how to batch‑distribute host keys using a shell script run by Jenkins. The script reads a host list file and, for each line, extracts the hostname, IP address, and password, then uses sshpass to copy the SSH public key to the target host:

for line in $(cat /shell/host); do
  myhostname=$(echo "$line" | awk '{print $1}')
  myip=$(echo "$line" | awk '{print $2}')
  mypwd=$(echo "$line" | awk '{print $3}')
  sshpass -p "$mypwd" ssh-copy-id -i /root/.ssh/id_rsa.pub $myip
 done

Running this job in Jenkins distributes the SSH keys to all listed hosts automatically, demonstrating Jenkins’s capability to orchestrate system‑level automation tasks.

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.

ci/cdAutomationOperationsDevOpsShellJenkins
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

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.