LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Remote command execution via SSH and newgrp command (https://www.linuxquestions.org/questions/linux-general-1/remote-command-execution-via-ssh-and-newgrp-command-879674/)

SuperMegaMau 05-09-2011 11:52 AM

Remote command execution via SSH and newgrp command
 
Hi,

Up until now I've been using plink to remotely compile a project I'm working on. But recently the administrator from the remote server updated the distribution and messed up some configurations.
My project has a lot of scripts written for tc shell (tcsh), and now the default shell is bash. There is no way to change this. Another problem is that now I need to run newgrp to change my default user group.

So... to work around this problem I've changed my .bashrc to run newgrp and then tcsh.

If I do a normal connection using SSH, everything works as expected, but when using plink, or SSH to remotely execute commands, the shell gets stuck on the newgrp command. I think it's because both applications need a return value from newgrp to send the command I need to execute.

Remotely running scripts that call a shell also get stuck like newgrp (newgrp also opens a new shell and that's why it gets stuck)

my .bashrc is as follows:
Code:

user_grp=`id -g`

if [ $user_grp != 4919 ]; then
    newgrp new_group_id
else
    export SHELL=/bin/tcsh
    tcsh
fi

Any ideas?
Thanks

colucix 05-10-2011 05:06 AM

Quote:

Originally Posted by SuperMegaMau (Post 4351140)
Hi,Remotely running scripts that call a shell also get stuck like newgrp (newgrp also opens a new shell and that's why it gets stuck)

Yes, that's exactly the reason: it results in an infinite loop, where every newgrp command starts a new shell and executes another newgrp command, that starts a new shell again and re-execute another newgrp command... it's a never ending story. If the remote machine uses /bin/bash as default shell, you can limit the execution of newgrp to the first shell by means of the SHLVL environment variable:
Quote:

SHLVL Incremented by one each time an instance of bash is started.
For example, you can try to change your code like this:
Code:

if [ $user_grp != 4919 ]; then
  if [ $SHLVL -eq 1 ]; then
    newgrp new_group_id
  fi
else
    export SHELL=/bin/tcsh
    tcsh
fi

Another problem arises if the newgrp command asks for a password. To avoid this you have to (or ask the administrator to) add the user as a member of the group (if not already set).

SuperMegaMau 05-11-2011 05:58 AM

Actually the infinite loop is not the problem, I took care of that with my "if" statement, the second time the script is executed, my group is the right one, so it calls the tcsh instead.

This is working when I connect to the server by SSH, like:
Code:

ssh server.com -lusername
What is not working is remote command execution line:
Code:

ssh server.com -lusername ls -s
If I do not change the shell on the .bashrc, the command runs as it should.

colucix 05-12-2011 03:14 AM

Yes, sorry. I can see it now: the problem is that the command is passed to the login shell /bin/bash but a new shell /bin/tcsh is spawned and it is not aware of the command to execute. Unless you pass it to the spawn explicitly!

Now the problem can be reduced to: if the shell is interactive then open a tcsh session, otherwise if the shell is non-interactive execute the command using tcsh then exit. Translated in bash:
Code:

if [[ $- =~ i ]]
then
  export SHELL=/bin/tcsh
  tcsh
else
  export SHELL=/bin/tcsh
  tcsh -c "$BASH_EXECUTION_STRING"
  exit
fi

Note that the exit statement is mandatory, otherwise after the command has been executed by the C-shell, the bash attempts to execute the command by itself (that can bring to a syntax or command not found error). Hope this is what you're looking for.

SuperMegaMau 05-13-2011 07:31 AM

Excelent!! That's it!

Thank you very much...


All times are GMT -5. The time now is 06:22 AM.