What is a command-line equivalent of pressing a key on remote computer via ssh

06
2014-04
  • les

    What are the options to target specific keys on a remote computer and execute the equivalent of virtually pressing them from the terminal via ssh (what would normally be a user pressing them)?

    Example is adjusting the volume:

    sudo osascript -e "set Volume 10"
    

    I'm not asking for applescript.

  • Answers
  • joejoe31b

    There is some information about how to do that HERE. An excerpt from that page is included below.

    Terminals only understand characters, not keys. So al function keys are encoded as sequences of characters, using control characters. Apart from a few common ones that have an associated control character (Tab is Ctrl+I, Enter is Ctrl+M, Esc is Ctrl+[), function keys send escape sequences, beginning with Ctrl+[ [ or Ctrl+[ O. You can use the tput command to see what escape sequence applications expect for each function key on your terminal.

    Also, Hacker's Keyboard was suggested on the page linked above, if you are attempting to do the same sort of thing from an Android device.

  • Lawrence

    You can run commands over ssh directly.
    ssh user@host1 command will run the command on host1 under the user.
    So to use your set volume example, you could do this -
    ssh root@host1 osascript -e "set Volume 10"


  • Related Question

    ubuntu - How to download a file from the command line? via ssh?
  • Andrew

    I want to download files from a remote server to my local drive, and do it from the command line. I also want to be able to do this over SSH. How can I do this?

    Note: the remote server is Ubuntu, the local is Mac OS X


  • Related Answers
  • Chirp. Not Luke.

    Use scp-command, it runs on top of SSH. Example:

    scp [email protected]:/path/to/file localfile
    

    It also works another way round

    scp localfile username@host:/path/remotefile
    

    Username, path, and filename can be omitted (but not the : !).

    As Iain said, SFTP works also, but I tend to favor scp for its cp-like usage.

  • Chirp. Not Luke.

    I use SFTP for this. It's command line and uses the same security as SSH.

  • Chirp. Not Luke.

    You can also use rsync for it. It can work over SSH.

  • Chirp. Not Luke.

    If you can't use scp or SFTP you can use tar over SSH:

    tar cf - . | ssh otherhost "cd /mydir; tar xvf -"
    

    This one is also good if you have sparse files which otherwise will "explode".