Tue. Apr 16th, 2024

Often we need to connect to a remote machine and execute a command. If that system is Unix/Linux then with a simple script this becomes very easy to create a command line script to handle this for us:

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set password [lindex $argv 2]
set packets [lindex $argv 3]
 
puts "Connecting to $host as $user to install $packets"
 
spawn ssh $user@$host "$packets"
expect "password:*"
send -- "$password\r"
interact

Usage: execute_cmd <username> <hostname> <password> “<Command to execute in quotes>”

This simple script will connect to the host, using the credentials provided and execute the command.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.