LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   TERM environment variable not set (https://www.linuxquestions.org/questions/linux-newbie-8/term-environment-variable-not-set-4175464092/)

Rohit_4739 05-30-2013 07:20 AM

TERM environment variable not set
 
Hi All,

I am trying to execute a bash script from a centralized AIX machine to Linux boxes but i get the error saying "TERM environment variable not set". Below are my scripts and how i am using them to execute remotely.
Code:

serv1:/ #ls -l rohit.sh
-rw-r--r--    1 root    system          333 May 30 07:14 rohit.sh

Code:

#!/bin/bash
clear
echo -e "\n"
IFS=$','
LIST="Test1,Test2,Test3,Exit"
PS3="Please enter your choice : "

select i in $LIST
do
clear
case $i in
        "Test1" )
          echo "Hello1;;
        "Test2" )
          echo "Hello3;;
        "Test3" )
          echo "Hello3";;
              "Exit" )
        exit 0;;
esac
done

This is how i am executing the script.
Code:

serv1:/ #ssh serv2 'bash' < rohit.sh
And below is the output i see

Code:

serv1:/ #ssh serv2 'bash' < rohit.sh


TERM environment variable not set.1) Test1
2) Test2
3) Test3
4) Exit
Please enter your choice :
TERM environment variable not set.
Please enter your choice : serv1:/ #

As you can see the script has terminated and i am back the prompt.

So can anyone suggest. I also tried setting the TERM=xterm and then exporting it in the script but even that also didn't help.

linosaurusroot 05-30-2013 08:24 AM

When you are running a remote command like that you don't get a terminal. I wouldn't include the "clear" commands.

Rohit_4739 05-30-2013 08:38 AM

hi linosaurusroot, thanks for yuor reply.removing "clear" actually resolved the error but i still end up at the prompt without my script being executed.

Here is the output after removing "clear" from the script

Code:

serv1:/ #ssh serv1 'bash' < rohit.sh


1) Test1
2) Test2
3) Test3
4) Exit
Please enter your choice : Please enter your choice :
serv1:/ #

Anything you can suggest.

shivaa 05-30-2013 08:43 AM

Perhaps the terminal type is not defined. What's result of:
Code:

~$ env | grep TERM
EDIT: Invoke your script with -xv debug option, and see where it goes wrong, as:
Code:

serv1:/ # ssh serv2 'bash -xv rohit.sh'

Rohit_4739 05-30-2013 08:48 AM

Quote:

Originally Posted by shivaa (Post 4962086)
Perhaps the terminal type is not defined. What's result of:
Code:

~$ env | grep TERM

Code:

#env | grep TERM
TERM=xterm

The error related to "TERM" has been resolved but i am still unable to figure out why my scripts is just exiting ??????? There is no issue with the script because i copied the script the linux box (serv2)then executed it and it worked just fine. However when i am trying to execute from remote aix box which is serv1 then it just exits without executing and comes back at the prompt.

theNbomr 05-30-2013 10:23 AM

The problem you're having is because you seem to be mistaken about where your standard IO redirection is occurring. The local host is redirecting the standard input of the ssh process from the script 'rohit.sh'. I surmise that your intention is to run the script under the bash process on the remote host. In order for that to happen, the script must exist on that remote host. The redirection you're using probably does nothing, since ssh doesn't know what to do with a shell script as input.
--- rod.

shivaa 05-30-2013 10:31 AM

Quote:

Originally Posted by Rohit_4739 (Post 4962095)
However when i am trying to execute from remote aix box which is serv1 then it just exits without executing and comes back at the prompt.

Did you try?
Code:

serv1:/ # ssh serv2 'bash -xv rohit.sh'
#Or
serv1:/ # ssh serv2 'bash rohit.sh'


Rohit_4739 05-31-2013 02:37 AM

Quote:

Originally Posted by theNbomr (Post 4962154)
The problem you're having is because you seem to be mistaken about where your standard IO redirection is occurring. The local host is redirecting the standard input of the ssh process from the script 'rohit.sh'. I surmise that your intention is to run the script under the bash process on the remote host. In order for that to happen, the script must exist on that remote host. The redirection you're using probably does nothing, since ssh doesn't know what to do with a shell script as input.
--- rod.

But if the script must exist on the remote server then i think sole purpose is being defeted here, for example if i have 1000 servers and i want to run the script from centralized server by just providing the hostname like below.
Code:

ssh <hostname> 'bash' < script.sh
How do i do so then

Rohit_4739 05-31-2013 02:38 AM

Quote:

Originally Posted by shivaa (Post 4962156)
Did you try?
Code:

serv1:/ # ssh serv2 'bash -xv rohit.sh'
#Or
serv1:/ # ssh serv2 'bash rohit.sh'


Yes i tried and as i wrote earlier there is no bug in the script as it executed just fine on the remote machine by copying the script on the machine .

theNbomr 05-31-2013 08:32 AM

If the script cannot be on the remote host, then your only recourse will be to feed the content of the script through the ssh connection. Probably the expect tool is the best way to accomplish that. You may also be able to run the ssh session from within a screen session. Using that method, you will be able to exploit screen's stuff command to push data into the screen session, and thus to the remote bash session. I have used this method with very good success in similar applications.

What about your local script including a line that uses scp to put the script onto the remote host before executing it? Your local script could then probably delete it from the remote host before closing the connection.

--- rod.

Rohit_4739 05-31-2013 09:06 AM

Quote:

Originally Posted by theNbomr (Post 4962775)
If the script cannot be on the remote host, then your only recourse will be to feed the content of the script through the ssh connection. Probably the expect tool is the best way to accomplish that. You may also be able to run the ssh session from within a screen session. Using that method, you will be able to exploit screen's stuff command to push data into the screen session, and thus to the remote bash session. I have used this method with very good success in similar applications.

What about your local script including a line that uses scp to put the script onto the remote host before executing it? Your local script could then probably delete it from the remote host before closing the connection.

--- rod.

Yes i did used the 2nd method you specified, i mean first copying the script using SCP, then executing it using SSH and then removing it from remote server. I did like below

Code:

scp rohit.sh root@serv2:/root >/dev/null
ssh serv2 'bash' '/root/rohit.sh; rm -f /root/rohit.sh'

However the concern i have with this that it will affect performance as first the script has to be copied and then executed. I tried running the script using Screen also but that also didn't work. COuld you please share with me how you have used screen in the past successfully in the past. Also if you have any idea on how to do that via Expect ?

theNbomr 05-31-2013 09:52 AM

Never had to use expect, since I always use the screen method. expect is probably required if you need to send data that depends on some condition observed in the response from the remote host.
This should get you close, using the screen method:
Code:

NL=$(echo -ne '\015')
screenName=rohitsAppl
screen -S ${screenName} -dm
sleep 1
screen -S ${screenName} -p 0 -X stuff "ssh serv2${NL}"

while read script; do
    screen -S ${screenName} -p 0 -X stuff "${script}${NL}"
done < rohit.sh

screen -S ${screenName} -p 0 -X stuff "exit${NL}"
screen -S ${screenName} -p 0 -X stuff "exit${NL}"

You can hardcode the strings you want to stuff into the session, rather than reading the existing script. The ${NL} is the stuff equivalent of pressing the 'enter' key. The sleep is necessary to allow the session to start up and be in a state to accept further commands. You might need another one or two second sleep following the ssh login command.

--- rod.

jpollard 05-31-2013 03:20 PM

Quote:

Originally Posted by Rohit_4739 (Post 4962020)
Hi All,

I am trying to execute a bash script from a centralized AIX machine to Linux boxes but i get the error saying "TERM environment variable not set". Below are my scripts and how i am using them to execute remotely.

...

This is how i am executing the script.
Code:

serv1:/
And below is the output i see

Code:

serv1:/ #ssh serv2 'bash' < rohit.sh


TERM environment variable not set.1) Test1
2) Test2
3) Test3
4) Exit
Please enter your choice :
TERM environment variable not set.
Please enter your choice : serv1:/ #

As you can see the script has terminated and i am back the prompt.

So can anyone suggest. I also tried setting the TERM=xterm and then exporting it in the script but even that also didn't help.

It is relatively simple:

ssh -t serv2 'bash' < rohit.sh

giving a command (such as 'bash') causes ssh to not request a terminal.

You also get a working result using

ssh serv2 <rohit.sh

as that requests a terminal to be assigned.


All times are GMT -5. The time now is 04:36 PM.