Rizwan Sharif
Rizwan Sharif

Reputation: 1119

Run same command on all terminals in mac

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

Answers (3)

Sumeet_Pol
Sumeet_Pol

Reputation: 969

iTerm2 can solve this problem. steps:

  1. split terminal as per your requirement
  2. click on shell -> broadcast input (select broadcast setting as per you requirement)

Screen shot for reference: enter image description here

Upvotes: 6

TimeDelta
TimeDelta

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

synthesizerpatel
synthesizerpatel

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

Related Questions