Sponsored Content
Top Forums Shell Programming and Scripting SSH sessions from bash script Post 302307130 by qneill on Tuesday 14th of April 2009 04:40:39 PM
Old 04-14-2009
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash script for ssh login-

hi. I need a bash script which can login to an other mashin via SSH and then run some commands and then return the result to my mashine. I dont know where to begin, I think first I will need a ssh connection, dont know how to make it, then , do I need a ftp connection between the 2 mashins to... (5 Replies)
Discussion started by: big_pil
5 Replies

2. Shell Programming and Scripting

bash ssh login script

hello anyone have done ssh login script without "expect" (automatic login from host A / user b to Host B / user b without enter any passwords)? cheers (2 Replies)
Discussion started by: modcan
2 Replies

3. UNIX for Advanced & Expert Users

How to monitor SSH sessions

Hi All Is it possible to log and monitor user activities in a SSH session. Is there any configuration or a patch to apply ?. Im currently using OpenSSH with AIX 5.3L and my syslog.conf includes auth.* /var/log/sshd/auth.log and in sshd_config it shows SysLogFacility AUTH... (4 Replies)
Discussion started by: Anuradhai4i
4 Replies

4. Shell Programming and Scripting

SSH sessions from bash script

Hi Forum. First of all thanks for reading. I need some help here. I have been checking some other posts about this issue, but I still have problems... I'm trying to conect througn ssh to several machines to kill some process and I have problems with the " ' ' ". This is what I'm trying... (2 Replies)
Discussion started by: gtangil
2 Replies

5. UNIX for Dummies Questions & Answers

Automatic logging (capture screen output) of telnet/ssh sessions on a Solaris node

Hi I am working in Solaris 10 and I want to monitor logs for every telnet/ssh session that tries to connect to the server. I need these logs to be generated in a file that I can fetch using ftp. I am a new user and a stepwise detail will be great BR saGGee (3 Replies)
Discussion started by: saggee
3 Replies

6. Shell Programming and Scripting

ssh to run bash script

I want to use ssh to start a bash script that I have uploaded to a webhost. How do I do that from linux? (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

Help on an ssh bash script...

Hey Guys, I want to have a bash script on my computer (Mac OS X 10.6.8) that can ssh into my iPod and respring. I know how do this by typing in "ssh root@10.0.1.10" and then typing in the password "alpine". From there i simply type "respring". I want to possibly put this into a shell script so it... (0 Replies)
Discussion started by: jetstream131
0 Replies

8. Shell Programming and Scripting

[Solved] Using SSH in bash script

Hello, I am writing a script that has to log in to X number of servers over a full C class range. I am only keyed on a certain number of these servers and this can change from time to time. The part of my script that deals with this is for i in $(cat $server_list); do ssh ... (0 Replies)
Discussion started by: colinireland
0 Replies

9. Red Hat

multiple ssh sessions

Hi, I use OpenSSH to log on to a RH server but when I enter the password 2 session windows appear. I only need one so can anyone advise where I can rectify this? R, D. (2 Replies)
Discussion started by: Duffs22
2 Replies

10. UNIX for Beginners Questions & Answers

Find number of ACTIVE SSH putty sessions, excluding where the user hopped on to a diff server

Hi - If iam logged on to server A, on 4 putty windows using SSH ... and out of these 4 logged-in sessions, in one of the sessions if i did SSH from server A to server B; i would now have 4 putty windows sessions ... of which 3 are actively logged on to Server A while 1 putty window is actively... (2 Replies)
Discussion started by: i4ismail
2 Replies
DDB(8)							    BSD System Manager's Manual 						    DDB(8)

NAME
ddb -- configure DDB kernel debugger properties SYNOPSIS
ddb capture [-M -core] [-N -system] print ddb capture [-M -core] [-N -system] status ddb script scriptname ddb script scriptname=script ddb scripts ddb unscript scriptname ddb pathname DESCRIPTION
The ddb utility configures certain aspects of the ddb(4) kernel debugger from user space that are not configured at compile-time or easily via sysctl(8) MIB entries. To ease configuration, commands can be put in a file which is processed using ddb as shown in the last synopsis line. An absolute pathname must be used. The file will be read line by line and applied as arguments to the ddb utility. Whitespace at the beginning of lines will be ignored as will lines where the first non-whitespace character is '#'. OUTPUT CAPTURE
The ddb utility can be used to extract the contents of the ddb(4) output capture buffer of the current live kernel, or from the crash dump of a kernel on disk. The following debugger commands are available from the command line: capture [-M core] [-N system] print Print the current contents of the ddb(4) output capture buffer. capture [-M core] [-N system] status Print the current status of the ddb(4) output capture buffer. SCRIPTING
The ddb utility can be used to configure aspects of ddb(4) scripting from user space; scripting support is described in more detail in ddb(4). Each of the debugger commands is available from the command line: script scriptname Print the script named scriptname. script scriptname=script Define a script named scriptname. As many scripts contain characters interpreted in special ways by the shell, it is advisable to enclose script in quotes. scripts List currently defined scripts. unscript scriptname Delete the script named scriptname. EXIT STATUS
The ddb utility exits 0 on success, and >0 if an error occurs. EXAMPLES
The following example defines a script that will execute when the kernel debugger is entered as a result of a break signal: ddb script kdb.enter.break="show pcpu; bt" The following example will delete the script: ddb unscript kdb.enter.break For further examples, see the ddb(4) and textdump(4) manual pages. SEE ALSO
ddb(4), textdump(4), sysctl(8) HISTORY
The ddb utility first appeared in FreeBSD 7.1. AUTHORS
Robert N M Watson BUGS
Ideally, ddb would not exist, as all pertinent aspects of ddb(4) could be configured directly via sysctl(8). BSD
December 24, 2008 BSD
All times are GMT -4. The time now is 09:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy