SSH batch help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SSH batch help
# 1  
Old 04-17-2012
SSH batch help

Dear Gurus,

I had hundreds of equipments which i can only use SSH to login and retrieve informations. Thus, how should i do that? eg:ssh batch

Code:
@ echo off
ssh <server1>@10.168.1.1
ECHO testing1
exit

ssh <server2>@10.168.1.2
ECHO testing2
exit


Can the above method used? Smilie

Last edited by Scott; 04-19-2012 at 09:32 AM.. Reason: Code tags
# 2  
Old 04-17-2012
sure...

Code:
for server in 10.168.1.1 10.168.1.2; do

    ssh "user@$server" /bin/bash -s <<EOF
echo 'testing on $server'
EOF

done

Be aware that using an unquoted heredoc will expand parameters locally. Is why it will expand $server inside single quotes. See difference:

Code:
[mute@geek ~]$ for server in localhost; do
> ssh mute@$server /bin/bash -s <<EOF
> echo 'testing on $server'
> EOF
> done
mute@localhost's password:
testing on localhost
[mute@geek ~]$ for server in localhost; do ssh mute@$server /bin/bash -s <<'EOF'
> echo "testing on $server"
> EOF
> done
mute@localhost's password:
testing on
[mute@geek ~]$


Last edited by neutronscott; 04-17-2012 at 09:45 PM.. Reason: dangling /code tag
# 3  
Old 04-17-2012
Hi,neutronscott
Thanks for your help.
Thus for me to enter the password (password is pass123 for all servers), i just have to add this line right before echo "testing on $server" right?



Code:
for server in 10.168.1.1 10.168.1.2; do
ssh "user@$server" /bin/bash -s <<EOF
pass123
echo 'testing on $server'
EOF

done


Just one last questions, will this harm my servers since i have hundreds of servers to be excecuted ?

Last edited by Scott; 04-19-2012 at 09:33 AM.. Reason: Code tags
# 4  
Old 04-17-2012
You may want to look at a Parallel SSH implementation. Also, one cannot script the password prompt. ssh opens the terminal directly. Ideally you'd setup authorized_keys, or use "expect" (TCL script which acts like a terminal). I'm not well read in these things, sorry.
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 04-19-2012
Quote:
Originally Posted by neutronscott
You may want to look at a Parallel SSH implementation. Also, one cannot script the password prompt. ssh opens the terminal directly. Ideally you'd setup authorized_keys, or use "expect" (TCL script which acts like a terminal). I'm not well read in these things, sorry.
copy content of id_rsa.pub of client into authorized
_keys of host
# 6  
Old 04-19-2012
Quote:
Originally Posted by neutronscott
sure..
Be aware that using an unquoted heredoc will expand parameters locally. Is why it will expand $server inside single quotes. See difference:

Code:
[mute@geek ~]$ for server in localhost; do
> ssh mute@$server /bin/bash -s <<EOF
> echo 'testing on $server'
> EOF
> done
mute@localhost's password:
testing on localhost
[mute@geek ~]$ for server in localhost; do ssh mute@$server /bin/bash -s <<'EOF'
> echo "testing on $server"
> EOF
> done
mute@localhost's password:
testing on
[mute@geek ~]$

Hi neutronscott, I could not understand the quoting of here-document stuff that you told.

Also, I read the following about here-documents:

All special characters between here-document, such as $, `, *, ... need to be escaped otherwise the code will be evaluated first by the calling shell

\EOF -- this "here-document", escaped version is used to prevent expansion

Escaping or quoting the delimiter for a here-document instructs the shell not to perform parameter expansion, command/variable substitution and arithmetic expansion on the here-document's contents


so my questions are:
a) what is the difference between \EOF and 'EOF'?

b) what do you mean by expanding locally? As per your example, when I issue ssh command it would be logged into the remote machine after successful password entry. So in that case, it is not in local anymore. So it should have printed the server name right, as I have the quoted the here-document parameter (i.e) 'EOF'?

Also, if you see the following code:
Code:
echo $var | ssh user@server "read remote_var; echo \$remote_var"

this will print the variable's value, since it has been escaped. So how it is different from quoting/unquoting of here-doc parameter? Kindly clarify me

Last edited by royalibrahim; 04-19-2012 at 10:49 AM..
# 7  
Old 04-19-2012
Quote:
Originally Posted by royalibrahim
a) what is the difference between \EOF and 'EOF'?
I don't think there really is one.

Quote:
b) what do you mean by expanding locally?
as in, $VARIABLE gets substituted before the text gets fed into ssh.

If you're ever in doubt about what will get fed raw into ssh, try
Code:
ssh username@host exec cat <<EOF
...
EOF

To see what the raw, uninterpreted contents of what is received on the other end actually are.

Quote:
As per your example, when I issue ssh command it would be logged into the remote machine after successful password entry. So in that case, it is not in local anymore.
It hardly matters whether it's local "anymore". ssh is not an escape-hatch or shell extension by which your shell script gets funneled raw into something remotely, ssh is just a program like anything else; your own shell must interpret the entire statement first to run it at all.

If you don't want it substituting things which any shell would think deserves substituting, you must prevent it from doing so by various means -- quoting, escaping, the right kind of here-documents, and the like.

Last edited by Corona688; 04-19-2012 at 11:04 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

3. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

4. Shell Programming and Scripting

Ssh = ssh expect and keep everything not change include parameter postion

I have write a script which contains ssh -p 12345 dcplatform@10.125.42.50 ssh 127.0.0.1 -p 5555 "$CMD" ssh root@$GUEST_IP "$CMD" before I use public key, it works well, now I want to change to "expect", BUT I don't want to change above code and "parameter position" I can post a... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

6. HP-UX

how to run glance over ssh in batch mode

Hello; Is it possible to run glance over ssh in batch mode ?? Similar to running " top -f " command over ssh.. Need to get glance output for specific pids Thnx very much (5 Replies)
Discussion started by: delphys
5 Replies

7. UNIX for Dummies Questions & Answers

nicely formatted directory listing from batch ssh session

Hi, I am really struggling to finish of a script I have been assigned. The script's purpose is to log on to each server defined in an array, determine the Web Server version, and list the directory beneath the installation directory. In my case, this installation directory is almost always... (6 Replies)
Discussion started by: grebbux
6 Replies

8. Shell Programming and Scripting

SSH in batch mode and File-Handles in a loop

Hi all I try to execute SSH commands on several hosts in a while-loop. There seems to be a problem with file-handle, first cycle works correct but no other one will follow due to the while condition is false even that there are many more host entries (lines) in all_hosts.dat. ... (3 Replies)
Discussion started by: DaveCutler
3 Replies

9. UNIX for Dummies Questions & Answers

SSH Batch configuration

Team, I am trying to do ssh batch as root. I have ssh set-up to do work w/o requiring password/uid on remotes. The problem is I need to submit batch work to remote systems, and NOT have my terminal program.sh lock/wait for the response from remote. This is so I can submit batch work to other... (2 Replies)
Discussion started by: xcom007
2 Replies
Login or Register to Ask a Question