简体   繁体   中英

How to execute a series of commands after logging in to a remote server over SSH?

I want to login to an interactive shell on a server using SSH and, after logging in, execute two commands.

This works: ssh user1@server -t "cd /home/user2; bash --login"

None of the following work:

ssh user1@server -t "cd /home/user2; bash --login -c 'source /home/user2/.bashrc'"

ssh user1@server -t "cd /home/user2; bash --login -c source /home/user2/.bashrc"

ssh user1@server -t "cd /home/user2; bash --login source /home/user2/.bashrc"

ssh user1@server -t "cd /home/user2; bash --login; source /home/user2/.bashrc"

When I call ssh with the -v flag, I see the following before the ssh session exits:

debug1: Sending command: cd /home/user2; bash --login -c "source /home/user2/.bashrc"
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

I know this might look odd; I need to login as user1 so that I can use software (legally) that can only be run by user1, but I want to run my personal .bashrc and work in my personal working directory. I can not modify /home/user1/.bashrc. Is there any better way to do what I'm trying to do?

I might be missing something but in order to achieve the stated goal:

  1. Log in as user1
  2. Set working directory to the home of user2
  3. Source .bashrc from user2

it is enough to say

ssh user1@server -t "cd /home/user2; bash --rcfile /home/user2/.bashrc -i"

Note that in most cases this would work without the -i and without giving the full path to .bashrc:

ssh user1@server -t "cd /home/user2; bash --rcfile .bashrc "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM