View previous topic :: View next topic |
Author |
Message |
vuakko Tux's lil' helper
Joined: 09 May 2007 Posts: 138 Location: Helsinki, Finland
|
Posted: Wed Nov 21, 2007 8:49 am Post subject: CDing to a certain directory in ssh |
|
|
I'd want to do something like ssh <machine>:dir in scp syntax, but if I just use
ssh <machine> cd $dir then the connection of course logs me out immediately.
Does anyone know a way to ssh, do a command and then continue in the shell?
I would like my pwd to stay constant when I ssh to another machine
in the network (they share the same fs tree). |
|
Back to top |
|
|
JeliJami Veteran
Joined: 17 Jan 2006 Posts: 1086 Location: Belgium
|
Posted: Wed Nov 21, 2007 11:57 am Post subject: |
|
|
you could place a bash script in your homedir on the remote server, workdir.sh:
Code: | #!/bin/bash
cd $1
/bin/bash
|
and call it in your ssh call:
Code: | local $ ssh -t remote /bin/bash workdir.sh $PWD |
and you start out in the same directory on the remote server _________________ Unanswered Post Initiative | Search | FAQ
Former username: davjel |
|
Back to top |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Wed Nov 21, 2007 12:35 pm Post subject: |
|
|
From the ssh man page: Quote: | Additionally, ssh reads ~/.ssh/environment, and adds lines of the format ``VARNAME=value'' to the environment if the file exists and users are allowed to change their environment. For more information, see the PermitUserEnvironment option in sshd_config(5). |
I don't know whether adding the line HOME=/your/choice will do what you want, but it might be worth a look.
Edit: Hm, on second thought, even if it does start you out where you want, you probably wouldn't want your home to be set to that. |
|
Back to top |
|
|
vuakko Tux's lil' helper
Joined: 09 May 2007 Posts: 138 Location: Helsinki, Finland
|
Posted: Wed Nov 21, 2007 2:03 pm Post subject: |
|
|
That works only half way (davjel's suggestion). It goes to the correct directory, but I don't get any shell prompt
(that is <blah blah>$) so its really not so useful still, because I never know where I am without
using pwd and I never know when a command has run through. Also tab-completion doesn't work and I
would also have to source my bashrc and so on in the same script manually.
So thanks for the offer, but it doesn't help yet.
Btw, as I said, the machine share the whole fs tree so the homedir is the same on every machine. |
|
Back to top |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Wed Nov 21, 2007 2:42 pm Post subject: |
|
|
Might there be a way of aliasing ssh to do the following:
- append a line to your .bash_profile to go where you are now: Code: | echo "cd "`pwd` >>~/.bash_profile |
- ssh to where you're going.
And previously having add a small script to .bash_logout to remove the last line in .bash_profile, if it starts with a "cd". |
|
Back to top |
|
|
JeliJami Veteran
Joined: 17 Jan 2006 Posts: 1086 Location: Belgium
|
Posted: Wed Nov 21, 2007 3:09 pm Post subject: |
|
|
ntvuok wrote: | That works only half way (davjel's suggestion). It goes to the correct directory, but I don't get any shell prompt
(that is <blah blah>$) so its really not so useful still, because I never know where I am without
using pwd and I never know when a command has run through. Also tab-completion doesn't work and I
would also have to source my bashrc and so on in the same script manually.
So thanks for the offer, but it doesn't help yet.
Btw, as I said, the machine share the whole fs tree so the homedir is the same on every machine. |
I don't get it. For me it works:
Code: | david@david ~ $ cd /usr/bin
david@david /usr/bin $ ssh -t octopus-dev /bin/bash workdir.sh $PWD
david@octopus-dev:/usr/bin$
|
AH, I see: you forget the '-t' option for ssh!! _________________ Unanswered Post Initiative | Search | FAQ
Former username: davjel |
|
Back to top |
|
|
vuakko Tux's lil' helper
Joined: 09 May 2007 Posts: 138 Location: Helsinki, Finland
|
Posted: Wed Nov 21, 2007 4:52 pm Post subject: |
|
|
OK, now I've got it working.
pwd.sh:
Code: |
#!/bin/bash
cd "$*"
/bin/bash
|
.bashrc:
Code: |
...
function bestcl {
...
ssh -t machine-$ind /bin/bash pwd.sh `pwd`
}
...
|
Thanks!
I tried all kinds of $1, $@ and $* confs with or without parentheses, finally
"$*" seemed to work.
Reason for this all: Our lab has a bunch of older cluster machines you use over ssh.
Function bestcl first calculates the cluster that has the lowest load (takes only 0.1 secs) and
then goes ssh there. It started to really grind me, when I always had to cd to the directory
I just was in. |
|
Back to top |
|
|
|