Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Using ssh command inside a script

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-10-2011
mick_000 mick_000 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 10 September 2012, 2:11 AM EDT
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
Unix or Linux Question 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.
Sponsored Links
    #2  
Old Unix and Linux 02-10-2011
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 February 2015, 1:56 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,671
Thanks: 8
Thanked 586 Times in 559 Posts
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:

Code:
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:

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

Sponsored Links
    #3  
Old Unix and Linux 02-10-2011
Chanakya.m's Unix or Linux Image
Chanakya.m Chanakya.m is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 3 December 2012, 5:01 PM EST
Location: Bangalore, India
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
Hi mick,
I am not sure about the restrictions on commands but I have below script that suits your requirement I believe.


Code:
/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.

Last edited by Franklin52; 02-10-2011 at 03:40 PM.. Reason: Please use code tags. thank you
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to monitor a command inside shell script sussus2326 Shell Programming and Scripting 3 01-27-2011 05:41 AM
Need help! command working ok when executed in command line, but fails when run inside a script! 4dirk1 Shell Programming and Scripting 4 12-02-2010 05:47 AM
Capture Last Command Executed inside Script deepaksinbox Shell Programming and Scripting 6 09-30-2009 10:04 AM
Exit out of the Script Command inside a Script jmungai UNIX for Dummies Questions & Answers 6 02-20-2009 04:26 PM
Run the command inside perl script vinay123 Shell Programming and Scripting 1 06-13-2008 10:01 AM



All times are GMT -4. The time now is 09:51 AM.