running command on remote host by ssh fails when running inside script

14,119

Solution 1

I could solve this problem using -T option "Disable pseudo-terminal allocation." and by using the scriptfiles without the #!/bin/bash line so just started with

echo "check" 
reboot

Solution 2

Try this:

sshpass -p '[MYPASSWORD]' ssh [USER]@[HOST] 'bash -s' < PATH/TO/LOCALSCRIPT

It seems that command is needed in the case when there is no terminal at all.

(found here)

Share:
14,119

Related videos on Youtube

derHugo
Author by

derHugo

I am SoftwareDeveloper and have a Master in "Human-Computer-Interaction". I learned a lot from this community and try my best to contribute back. I do this mostly from the phone so sometimes you need a bit of patience until I can actually test my ideas myself 😁 For work I mostly use Unity and c# making applications for Mixed-Reality glasses. But during my studies also got at least basic knowledge in a lot other fields.

Updated on September 18, 2022

Comments

  • derHugo
    derHugo almost 2 years

    The Situation:
    My Computer runs on Kubuntu 14.04. I am Admin of a Students House with some Netgear-APs. The idea is to have a script rebooting them all by one script. (ssh-key isn't posible)

    The Problem in short:
    ssh command on host runs fine from shell. But not if run by a script.

    Description:
    I have a Script on my Computer: "path/to/localScript"

    #!/bin/bash
    
    echo "Hello mister"
    reboot
    

    now if I enter the following directly in my shell

        sshpass -p '[MYPASSWORD]' ssh [USER]@[HOST] < PATH/TO/LOCALSCRIPT
    

    I get the output

    Pseudo-terminal will not be allocated because stdin is not a terminal.
    Hello mister
    Connection to HOST closed by remote host.
    

    and the AP actually reboots! I tried other commands(like ping) -> all worked fine. Now I want to do exactly the same thing but inside a script (so I can later add all the other APs too ;) ) So I tried a script "rebootThemAll"

    #!/bin/bash
    
    echo " Rebooting AP 1"
    sshpass -p '[MYPASSWORD]' ssh [USER]@[HOST] < PATH/TO/LOCALSCRIPT
    

    I get the output

     Rebooting AP1 
    Pseudo-terminal will not be allocated because stdin is not a terminal.
    

    and ... nothing more happens. Again I see my input prompt on the shell line so the script stops. I also tried more than 1 AP to see if that error cancels my script but it didn't. For more APs the output look like this

     Rebooting AP1
    Pseudo-terminal will not be allocated because stdin is not a terminal.
     Rebooting AP2
    Pseudo-terminal will not be allocated because stdin is not a terminal.
     Rebooting AP3
    Pseudo-terminal will not be allocated because stdin is not a terminal.
     Rebooting AP4
    ......
    

    but the script won't run. It doesn't show the "Hello mister" and they are not rebooting..

    Any one has an idea?

  • derHugo
    derHugo almost 8 years
    wow sorry for this late repost .. didn't come back a long time Thanks for your answer! I could solve this problem using -T option "Disable pseudo-terminal allocation." and by using the Scriptfiles without the #!/bin/bash line so just started with echo "check" reboot