Using ssh command inside a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using ssh command inside a script
# 1  
Old 02-10-2011
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.
# 2  
Old 02-10-2011
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

# 3  
Old 02-10-2011
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 04:40 PM.. Reason: Please use code tags. thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Run awk command inside ssh

I am trying to run an awk command inside of ssh and it is not working. These are AIX servers. for i in `cat servers`; do ssh $i "/bin/hostname; df -g | awk '/dev/ && $4+0 > 70'"; done server1 server2 server3 server4 I also tried these two methods and they did not work. It just seemed... (5 Replies)
Discussion started by: cokedude
5 Replies

2. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

Issue with ls command inside script

Hi , DIR1 has only one file with .txt extension , trying to get the size of that file using the following script #!/bin/ksh foldr_1="/etc/DIR1" #echo "$foldr_1" sze_fdr1=$(ls -ltr foldr_1/*.txt |awk '{ print $5 }') echo "$sze_fdr1" After executing the above script getting... (1 Reply)
Discussion started by: smile689
1 Replies

4. Shell Programming and Scripting

Command timeout from inside script.

Hi, I've written a very robust script to get an external IP address from 'behind' a router. It uses many web pages randomly choosing which one/ones to use at run time. The "fetch the web page containing the IP address" is handled by either wget or curl both of which have their 'max time for the... (6 Replies)
Discussion started by: gencon
6 Replies

5. Shell Programming and Scripting

How to monitor a command inside shell script

Hi All, Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data. tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*' I want to... (3 Replies)
Discussion started by: sussus2326
3 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. Shell Programming and Scripting

Problem using ssh inside script

Hi, When runnin the following script it complte successfully with out any problem #!/bin/sh SERV="crm1" for s in $SERV do export WebLogicSessions=`ssh psoft@$s "cd /software/psoft/scripts ; ./getweblogicsessions.sh crm1 8001 PIA | awk '{print $1}' ` echo "Checking number of... (0 Replies)
Discussion started by: yoavbe
0 Replies

8. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

9. Shell Programming and Scripting

ssh not ending (sometimes) from inside a script, why?

Okay I have this script file that runs from cron and most the time it works just find. Except every so often one of the three ssh commands I have in it just doesn't know it's done and that of course causes the whole thing to hang! The ssh command has executed. I can tell this because the command... (1 Reply)
Discussion started by: stilllooking
1 Replies
Login or Register to Ask a Question