The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make your bash script run on a machine with csh and bash npatwardhan Shell Programming and Scripting 3 11-19-2008 04:17 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 08:19 AM
bash script help!!! shooroop Shell Programming and Scripting 3 10-06-2005 08:20 PM
Please help with BASH script karine Shell Programming and Scripting 2 02-01-2005 01:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2008
tsurko tsurko is offline
Registered User
  
 

Join Date: Jun 2007
Location: Sofia, Bulgaria
Posts: 35
SSH sessions from bash script

Hello,
I had to make an ssh connection to remote server from a bash shell script. I've summarized my research and I want to share it with you. I hope it will be useful to somebody or it will get improved.
Bash has an option, which is called "input redirection", I think. It is something similar to this:


Code:
#!/bin/sh

mysql -u tsurko -p mydb <<EOF
SELECT * FROM test_table;
SELECT * FROM other_table;
quit
EOF
exit

This script will connect to MySQL, will run the two queries and will quit. In similar way we can make an ssh connection in bash script. Here is some example code:

Code:
#!/bin/sh

ssh example.com <<EOF
touch test_file
exit
EOF
exit

This way you can execute anything you want on the remote server. I have the following errors, while running the script:

Code:
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.

As far as I know this is known issue and it has been discussed in the forum. I haven't tried to fix it - it works for me.
Here is an example, using shell variables in the script:

Code:
#!/bin/sh

TEST_VAR=sample_file

ssh example.com <<EOF
touch $TEST_VAR
exit
EOF

echo "I'm back"
exit

In this case the value of variable TEST_VAR is acquired from the local environment. Ny local I mean the environment, where the script is executed. So on the remote machine we have executed the command "touch sample_file".
Here is another example showing how to use environment variables, declared on the remote server:

Code:
#!/bin/sh

TEST_VAR=sample_file

ssh example.com <<EOF
touch \$TEST_VAR
exit
EOF

echo "I'm back"
exit

Let's assume that we have TEST_VAR set on the remote server. We have escaped the $ character in our script, so $TEST_VAR is treated as string, not as a variable. On the remote server we have executed "touch $TEST_VAR". and TEST_VAR's value is acquired from there.
And finally let's assume that we have to run some commands on two or more machines. It's very inconvenient to have them two or more times (for each session). In this case the following workaround can be used. A text file is created and the desired commands are saved there. Then we can use "`cat <filename>`" in the script and they will be executed on the server. :

Code:
#!/bin/sh

ssh example.com <<EOF
`cat test.cmd`
exit
EOF

ssh example2.com <<EOF
`cat test.cmd`
exit
EOF

exit

and more convenient solution (thanks to ddreggors):

Code:
#!/bin/sh

ssh example1.com `cat test.cmd`
ssh example2.com `cat test.cmd`

Don't forget that the file must contain only commands, written in the way we want them executed on the server. The file can't be a bash script - any variables and so on won't be parsed.
That's it. I'll be glad to hear any suggestions, corrections, critics or just opinions. Corrections about my English are also welcome - it's not my mother tongue.

Last edited by tsurko; 12-07-2008 at 01:06 PM..
  #2 (permalink)  
Old 12-05-2008
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Just a thought here...

I notice you use:


Code:
ssh example2.com <<EOF
`cat test.cmd`
exit
EOF

when really (for a single command like that) you can use:


Code:

ssh example2.com `cat test.cmd`


or do you actually have multiple commands in that ssh command and only show one here?

If you do then you can use this:


Code:

ssh example2.com `cat test.cmd`; `cat test2.cmd`; `test3.cmd`

  #3 (permalink)  
Old 12-07-2008
tsurko tsurko is offline
Registered User
  
 

Join Date: Jun 2007
Location: Sofia, Bulgaria
Posts: 35
Yes, you are right. Your variant is better! I'll edit my post.
  #4 (permalink)  
Old 04-14-2009
qneill qneill is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 23
No reason you have to use a "here script" with <<EOF, you can pipe commands directly to the standard input of ssh, for example using echo:


Code:
$ D=/tmp; F1=x1; echo "F2=$D/$F1
echo on \$(hostname): escaped: D=\$D F1=\$F1 F2=\$F2
echo on \$(hostname): not escaped: D=$D F1=$F1 F2=$F2
df $D >\$F2; ls -l \$F2; cat \$F2; rm -rf \$F2; ls -l \$F2
" | ssh qneill@qneill-linux

this shows a couple of local variables D and F1 (expanded locally when the local echo is executed), and a remote variable F2 (expands to nothing locally, but remotely expands to the assigned value). The commands produce this output:


Code:
on qneill-linux: escaped: D= F1= F2=/tmp/x1
on qneill-linux: not escaped: D=/tmp F1=x1 F2=
-rw-r--r-- 1 qneill 30101 125 2009-04-14 15:25 /tmp/x1
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda1            147581948  78980980  61104240  57% /
ls: /tmp/x1: No such file or directory

The commands shown use the local variable D and remote variable F2, creating, using, and removing a file (/tmp/x1 on the remote machine).

If all of your variable expansions are constant, or only depend on remote things, you can use single quotes and avoid having to escape all the $ signs:


Code:
echo 'D=/tmp; F1=x1; F2=$D/$F1
echo on $(hostname): D=$D F1=$F1 F2=$F2
df $D >$F2; ls -l $F2; cat $F2; rm -rf $F2; ls -l $F2
' | ssh qneill@qneill-linux

which yields

Code:
on qneill-linux: D=/tmp F1=x1 F2=/tmp/x1
-rw-r--r-- 1 qneill 30101 125 2009-04-14 15:34 /tmp/x1
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda1            147581948  78980960  61104260  57% /
ls: /tmp/x1: No such file or directory

Once you start using this mechanism for anything complicated, you will quickly run into many quoting issues. Consider writing a script that generates code on the remote system, or a templated script that can be parameterized locally or remotely as needed, then executed remotely with a single call to ssh.
--
qneill
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0