Using ssh command inside a script

Hi,

I have a script file in server A. Inside the script file, I first have a ssh command that will connect to a remote server B. In the same script file itself, I have a sequence of commands that has to be run in server B. I am embedding these commands in the script file that I have in server A. When i run the script file in server A, it first gets connected to server B but nothing happens for sometime after which i get Connection timeout and some error in the command.

Is there a restriction in the kind of commands that can be executed in server B from server A with the scenario as mentioned above?

Instead of a script file, when i execute the commands one by one in console in server B it is getting executed. ie) first i connect to server B from server A using ssh and then run the commands one by one in server B. I want these commands to be placed in a script file and executed from server A.

Kindly provide a solution.

Thanks in advance.

When a script calls ssh, it should not try to be interactive (unless it is an expect script)! You have to set up the environment for a non-interactive ssh session, like this process for ksh:

echo ". ./.profile
<your_script_of_remote_commands>" | ssh2 user_id@host_or_ip ksh >>log_file 2>&1

The .profile needs to be stable when there is no terminal, similar to how it should be stable for sh and ksh:

if [ "$( tty 2>&1 )" != "not a tty" ]
then
 <steps_for_tty_setup>
fi

Hi mick,
I am not sure about the restrictions on commands but I have below script that suits your requirement I believe.

/usr/bin/ftp -v -n "$SERVER" << cmd
    user "$USER" "$PASSWD"
    cd $DESTINATION
    lcd $SOURCE
    bin 
    prompt

    Command 1
    Command 2
    .......
    .......
    .......
    Command n
    quit
cmd

This may help you, i am not sure.