Need help with executing multiple commands in remote machine


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with executing multiple commands in remote machine
# 1  
Old 01-25-2011
Need help with executing multiple commands in remote machine

Hi,
I work on a jumpserver and I wrote a script to transfer a file from source server to destination server.
Code:
#!/bin/ksh
echo "\nEnter the file name:\n"
read name
echo "\nSelect the Source server\n"
echo "1.  ODS PROD "
echo "2.  ODS DROPBOX"
echo "3.  ODS STE"
echo "4.  ODS STE DROPBOX"
echo "5.  ODS CTE "
echo "6.  ODS CTE DROPBOX"
echo "7.  ODS ITE"
echo "8.  ODS ITE DROPBOX"
echo "9.  ODS DEV"
echo "10. ODS DEV DROPBOX"
echo "11. ODS Legacy Prod"
echo "12. EXIT"
read optn
echo "\nSelect the path\n"
read path
case $optn in
  1) scp `whoami`@162.111.210.43:$path/$name .;;
         2) scp `whoami`@162.111.214.23:$path/$name .;;
        3) scp `whoami`@167.138.250.239:$path/$name .;;
        4) scp `whoami`@167.138.250.224:$path/$name .;;
        5) scp `whoami`@172.21.207.158:$path/$name .;;
        6) scp `whoami`@172.21.220.162:$path/$name .;;
        7) scp `whoami`@172.21.207.72:$path/$name .;;
        8) scp `whoami`@172.21.220.18:$path/$name .;;
         9) scp `whoami`@172.18.197.41:$path/$name .;;
        10) scp `whoami`@172.18.197.43:$path/$name .;;
         11) scp `whoami`@170.13.54.102:$path/$name .;;
 
 
esac
chmod 777 $name
echo "\nSelect the dest server\n"
echo "1.  ODS PROD "
echo "2.  ODS DROPBOX"
echo "3.  ODS STE"
echo "4.  ODS STE DROPBOX"
echo "5.  ODS CTE "
echo "6.  ODS CTE DROPBOX"
echo "7.  ODS ITE"
echo "8.  ODS ITE DROPBOX"
echo "9.  ODS DEV"
echo "10. ODS DEV DROPBOX"
echo "11. ODS Legacy Prod"
echo "12. EXIT"
read optn2
echo "\nSelect the destination path\n"
read destpath
case $optn2 in 
 1) scp $name `whoami`@162.111.210.43:/home/`whoami`;;
 2) scp $name `whoami`@162.111.214.23:/home/`whoami`;;
 3) scp $name `whoami`@167.138.250.239:/home/`whoami`;;
 4) scp $name `whoami`@167.138.250.224:/home/`whoami`;;
 5) scp $name `whoami`@172.21.207.158:/home/`whoami`;;
 6) scp $name `whoami`@172.21.220.162:/home/`whoami`;;
 7) scp $name `whoami`@172.21.207.72:/home/`whoami`;;
 8) scp $name `whoami`@172.21.220.18:/home/`whoami`;;
 9) scp $name `whoami`@172.18.197.41:/home/`whoami`;;
 10) scp $name `whoami`@172.18.197.43:/home/`whoami`;;
        11) scp $name `whoami`@170.13.54.102:/home/`whoami`;;
        12) exit;;
esac
 
case $optn2 in 
 #9) ssh -l x772525 172.18.197.41 'su - d_od; cd /home/x772525;chmod 777 $name;cp -p $name $destpath;';;
 9) ssh 172.18.197.41 "cd /home/x772525;chmod 777 $name;cp -p $name $destpath"
 
esac

the whole thing is executing fine, but at the end when i use su - d_od, it is giving me the following error:
Must be attached to terminal for 'am I' option
stty: : Invalid argument

I need to change the permissions of the file using d_od. Can someone please help me with this?

Thanks
Ajay
# 2  
Old 01-26-2011
In ksh, you might want to use the variable $LOGNAME instead of executing `whoami`.
# 3  
Old 01-26-2011
Thanks dsw. Now it is working, but unable to read the variables defined in the local machine when doing su - d_od. How can I do that ?

Code:
ssh -t 172.18.197.41 'su - d_od -c "cd /home/x772525;"cp -p $name $destpath;""';;

If I hardcode the variables, it is working fine. How can I do this?

Thanks
Ajay
# 4  
Old 01-26-2011
[EDIT: Oops, I see you are doing this over ssh, so ignore the below (it's valid for local purposes, however]

That's because you gain a new environment when you su with the "-", and the variables are defined in the current user's environment, hence they don't persist across the su.

If this is linux, you can try "su -m" to preserve the environment, otherwise just drop the "-" and see what you get.
# 5  
Old 01-26-2011
Even after dropping the -, it is still giving the same error Smilie
# 6  
Old 01-26-2011
Yeah, I edited my post above.

You have to use double quotes for the command so the shell can expand the var ahead of time. Single quotes pass it literally.

You'll have to escape the double "" with \"\" though, as you want *that* to be passed literally I assume.

---------- Post updated at 10:00 AM ---------- Previous update was at 09:58 AM ----------

Try this instead.

"su - d_od -c \"cd /home/x772525;cp -p $name $destpath\""
This User Gave Thanks to dsw For This Post:
# 7  
Old 01-26-2011
Thanks a lot. It is working Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Login to remote machine pass commands and capture all in a file

Dear user/friends, After a long gap of 6 years i am back to this forum to find a solution and i hope i will get a solution or atleast a workaround for the problem:p Following is my task which i am trying for almost 3 days without any success. 1) run this command from my terminal (note i... (7 Replies)
Discussion started by: imas
7 Replies

2. UNIX for Dummies Questions & Answers

How to run UNIX commands on remote machine from windows?

Hi All, I am working in support and we are planning to automate a system to reduce the direct manual intervention to core system. Please find the below details. 1. we have a web application that runs on Windows Platform. 2. From web application, we need to connect to remote Unix machine.... (6 Replies)
Discussion started by: Balaji K
6 Replies

3. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

4. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

5. Shell Programming and Scripting

Executing remote commands via ssh

Hi, I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen. # /usr/bin/bash ls $1 > /dev/null 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $server ... (5 Replies)
Discussion started by: moshesa
5 Replies

6. Linux

send remote commands to windows machine?

hey guys, I've done some searching and other than winexe I haven't been able to find a way to send remote commands to a windows machine. The problem I get is with winexe whenever you send the process to the background in a script, I get the following error. :wall: Any help is appreciated! ... (0 Replies)
Discussion started by: terrell
0 Replies

7. Shell Programming and Scripting

executing commands in remote server using ssh

I have some commands which need to be executed in remote machine. I have Linux Server from where I need to connect to Solaris server using ssh and then declare some variable over there and run some commands. I don't want to call a script which is present in Solaris server from Linux server... (7 Replies)
Discussion started by: maitree
7 Replies

8. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

9. Solaris

Help with executing multiple remote commands after multiple hops

Hi SSHers, I have embedded this below code in my shell script.. /usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5` SSH to both these servers are public-key authenticated, so things run... (13 Replies)
Discussion started by: LinuxUser2008
13 Replies

10. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies
Login or Register to Ask a Question