If you really want to do it without modification on the server side, either:
1) Run something like
$ ssh user@host -t 'bash -l -o vi'
I don't think the documentation is too clear on that, but -o option
is mentioned, and seems to work.
2) Use expect:
The expect
script:
$ cat bashsetup.expect
#!/usr/bin/expect -f
set user [lindex $argv 0];
set host [lindex $argv 1];
spawn ssh -l $user $host
expect "$ "
send "set -o vi\n"
interact
Make it executable and run:
$ ./bashsetup.expect user testhost
spawn ssh -l user testhost
[motd, blahblah...]
user@testhost ~$ set -o vi
user@testhost ~$
This assumes you can login without entering passwords (for the remote host or for your keys), otherwise the expect script would need to take that into account. But with lots of machines you're likely to already have that. Also, I expected for a dollar sign and a space, edit that according to your prompt: "# "
perhaps.
Though if something printed before prompt includes those same characters, you'll need to include something more specific in the expected string.
Also, that script doesn't support giving extra arguments to ssh
. If you're going to give an explicit command to run, you probably don't need vi-mode, but if you need say port tunneling, that might be a problem.
But in any case, I really think this should be solved on the target systems with separate accounts (sudo
or just plain old UID 0). Personalised configuration would be useful for many other cases too, and in general you'd have a bunch of configuration files and environment variables you'd like to set. (Consider that the admins might not agree on the value of $EDITOR
, or the contents of virc
or whatever.)
Also removing users would be easier with separate accounts.
Any way of synchronising files on all the hosts would also trivially solve this by allowing you to login with something like ssh -t user@host 'patricks_shell.sh'
or ssh -t user@host 'bash --rcfile patrick.rc'
.
set -o vi
before giving me control of the shell.set -o vi
command, then switches to interactive mode.sshd
sets up several environment variables which might help you determine who is on the other end. For example,SSH_CLIENT
contains the connecting IP-address (and the outgoing/incoming port of the client as well). Fiddling with this in~/.bashrc
might allow you to do things just for you.