LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-09-2011, 12:52 PM   #1
SuperMegaMau
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Rep: Reputation: 0
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

Last edited by SuperMegaMau; 05-09-2011 at 12:58 PM. Reason: missing info
 
Old 05-10-2011, 06:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by SuperMegaMau View Post
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).
 
Old 05-11-2011, 06:58 AM   #3
SuperMegaMau
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 05-12-2011, 04:14 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 05-13-2011, 08:31 AM   #5
SuperMegaMau
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Excelent!! That's it!

Thank you very much...

Last edited by SuperMegaMau; 05-13-2011 at 09:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
spacewalk remote command execution PoleStar Linux - Server 1 01-28-2011 06:03 PM
Escape command for remote ssh execution brianmcgee Linux - Software 2 06-14-2010 07:34 AM
Automated execution of a command on a remote system via SSH GUIPenguin Linux - General 1 03-01-2006 12:23 AM
ssh : remote command execution doesn't work for modifying env variables pypieuvre Programming 10 02-24-2006 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration