Execute commands on multiple hosts using expect tool
This is Part III in a series on Execute Commands on Multiple Linux or UNIX Servers Simultaneously. The full series is Part I, Part II, and Part III.
In third and final part of this series I will cover expect tool. Expect is a program that talks to other interactive programs according to a script. Expect is useful for running any program which requires interaction between the program and the user. For example
- Set or change password via script
- Ftp/ssh authentication
- Provide answers to command via script
- If you have multiple servers then you can execute commands on multiple hosts simultaneously.
Install expect
Expect comes with special pre installed script called multixterm expect. If you are using Debian Linux then use apt-get as follows :
# apt-get install expect # apt-get install expectk
If you are using Red hat Linux then use up2date command as follows:
# up2date -i expect
Fedora core (RHEL 5) / CentOS Linux user can use yum:
# yum install expect expectk
You can use ports to install expect under FreeBSD or use following command:
# pkg_add -v -r expect
Please note you can download expect from offical web site.
General syntax of multixterm:
multixterm -xc "command arg1 arg2" server1 server2...
Multixterm creates multiple xterms that can be driven together or separately so that you can execute or run on multiple hosts/servers simultaneously. The following command line starts up two xterms using ssh to the hosts 192.168.1.16 and 192.168.1.11:
$ multixterm -xc "ssh root@%n" 192.168.1.11 192.168.1.16
Where,
- -xc : The optional -xc argument indicates a command to be run in each named xterm (see -xn). With no -xc argument, the command is the current shell. This means you type command once and it get executed on both ssh hosts/servers.
- ssh root@%n : ssh is a command to connect remote server with root user. This name will also be substituted for any %n in the command argument.
Debian Linux stores multixterm at /usr/share/doc/expect directory. You need to type commands in multixterm window itself (stdin window). Click on stdin window and type uptime or w command:
For example when I typed the uptime and w command it looked like as follows:
- Execute Commands on Multiple Linux or UNIX Servers: Part I and Part II
- Read man page of multixterm
- Expect home page
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in...
- Howto Use SSH To Run Command On A Remote Machine
- Execute Commands on Multiple Linux or UNIX Servers
- Tentakel to execute commands on multiple Linux or UNIX Servers
- Increase the maximum number of pseudo - terminals ~ PTY on Linux for remote Login session
- Linux Shell Script to reboot DSL or ADSL router
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: apt-get_command, debian_linux, expect_script, expect_tool, interactive_programs, multixterm_command, script_ftp, simultaneously, ssh, ssh_authentication, unix_ftp_expect, unix_servers ~ Last updated on: August 17, 2007




Recent Comments
Today ~ 8 Comments
Today ~ 3 Comments
Today ~ 2 Comments
Today ~ 37 Comments
Today ~ 1 Comment