Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh with a set -o vi as first interactive command

I have a small script that i use to log in to a server. I exchanged the keys. The default set by the adminstirator is emacs. I got kind of addicted to the vi key bindings. I can't log in as myself, I have to log in as a group user. most of the time the first thing that i do is type in set -o vi . SOmetimes I forget and start using the vi key binding, but they work work, then i have to use the emacs key bindings. my muscle memory get messed up. It would be great to just automagically have the key bindings set when i log in with the login script.

anyhow I am trying to add the set command to my ssh script.

This one does not work.

#!/bin/bash
ssh -q -T bighost <<EOF
set -o vi
EOF
~

This one does not work

#!/bin/bash
ssh bighost bash -c "'
set -o vi
'"

This lets me ssh to the host, but the vi is not set as the keybinding.

#!/bin/bash
ssh -t bighost "$(< set -o vi )"




corp_user@bighost:~$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off
corp_user@big_host:~$

I even tried something like this:

ssh corp_user@bighost "$( < . ~/woogie)

Where woogie has "set -o vi " in it. Can this be done?

like image 465
capser Avatar asked Oct 25 '14 04:10

capser


People also ask

Is SSH an interactive shell?

interactive login shell: You log into a remote computer via, for example ssh . Alternatively, you drop to a tty on your local machine ( Ctrl + Alt + F1 ) and log in there. interactive non-login shell: Open a new terminal. non-interactive non-login shell: Run a script.

What is SSH verbose?

In verbose mode, the client prints messages as it proceeds, providing clues to the problem. New SSH users (and quite a few experienced ones) frequently forget or neglect to use verbose mode when problems arise.


2 Answers

This script works when I use it here:

#!/bin/bash

ssh [host] -t bash -o vi

where [host] should be the host you want to connect to. The -t option for ssh tells ssh to force the usage of a tty. If you don't do that, bash won't behave like a normal interactive shell. The option you were looking for is -o vi which is the same thing you'd give to set. The man page for bash mentions that you can give on the command line the same things you'd give set.

This does not require you to create any file on the remote host.

like image 95
Louis Avatar answered Nov 11 '22 11:11

Louis


Use Expect

The easiest way to do this in a way that won't impact the other users who share the remote account is with expect. For example:

expect -c 'spawn ssh localhost; expect "$ "; send "set -o vi\r"; interact return'

This will login and wait for a prompt before attempting to set the vi key bindings, and then turn control back over to you.

like image 1
Todd A. Jacobs Avatar answered Nov 11 '22 11:11

Todd A. Jacobs