Reputation: 1119
Is there a way to run the same command on all the open terminal tabs . I usually log into many servers and want to perform the same command on all of them. Xshell (available on windows only) had this feature where you can run same command on all open terminals , i am wondering if its possible in mac somehow ?
Upvotes: 6
Views: 7205
Reputation: 969
iTerm2 can solve this problem. steps:
Upvotes: 6
Reputation: 411
It is most definitely possible. I do it quite often. This solves it:
onall () {
if [[ $1 == "--help" ]]; then
echo "Usage: onall <command>"
return 0
fi
osascript -e "tell application \"Terminal\"
repeat with w in windows
repeat with t in tabs of w
do script \"${1//\"/\\\"}\" in t
end repeat
end repeat
end tell"
}
osascript
is the command-line program to run AppleScript code and the -e option tells it to use the following string as the script. The ${1//\"/\\\"}
escapes all of the quotes in the command.
You can either declare it as is in your .bash_profile or whatever other file you use or you can take it out of the function, put it into a script, make the script executable and put it somewhere that is always in your PATH.
NOTE 1: The entire command must be in the first argument.
NOTE 2: If you're doing a command with quotes in it, you must escape the quotes.
For example, onall echo "*"
will not give the expected results. Instead, use onall "echo \"*\""
Upvotes: 6
Reputation: 28036
Instead of using Terminal for this, use SSH. If it's a command you need to run in a terminal, odds are you're sshing somewhere anyway - eliminate the middle man.
Generate passwordless ssh keypair, put the public key on the remote systems, then from a single shell script you can do something like
for host in host1 host2 host3 ..; do
ssh -i ~/.ssh/my_private_key username@${host} "echo HAXXXXXXXXXXXX | wall"
done
This is a very crude mechanism, but it's lightyears beyond having to physically open a terminal for each command.
I actually know of something that'll do what you want on OSX, but honestly it's such a generically bad idea it would be negligent of me to provide a hyperlink to it.
Upvotes: 0