Plink (processing multiple commands) using Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Plink (processing multiple commands) using Bash
# 8  
Old 09-25-2013
Okay, I now understand how a simple here-document works. However, I'm still having trouble understanding why there is a EOP "here document" within a another EOF here-document; and, how it relates to the backtick in the script.

There is everything within:

Code:
<< EOF
$(
<< EOP

EOP
)
EOF

Code:
plink.exe -ssh -l UserID -pw Password01 192.168.1.2 << EOF
$(
  while read port
  do
    cat << EOP
    config t
    int $port
    shut
    no shut
    exit
EOP
  done < ports.txt
)
    exit
    exit
EOF

Sorry, I'm feeling really dense. I really appreciate all the help on this forum!

Quote:
Originally Posted by Corona688
More or less correct. It's called a "here-document", and attaches the text inside the block to the standard input of the program its fed into. It's a shell feature. It will work with any program that reads from standard input, not just cat.
# 9  
Old 09-25-2013
Quote:
Originally Posted by MKANET
Okay, I now understand how a simple here-document works. However, I'm still having trouble understanding why there is a EOP "here document" within a another EOF here-document; and, how it relates to the backtick in the script.
The inner here-document is fed into cat in a loop. cat <<EOF is just a convenient method of printing multiple lines of text, they could have done the same with a bunch of "echo" statements. It all ends up as part of the outer here-document.

The outer here-document feeds the output of all of that into plink.
# 10  
Old 09-25-2013
Hi, I can imagine it can be a bit difficult understand straight away. I'll try to explain what is going on:

Inside an (unquoted) here document you can have a variable reference. The contents of such a variable become part of that here document.

Code:
var=blabla
cat << EOF
This is the content of \$var: $var
EOF

output:
Code:
This is the content of $var: blabla


In this case instead of a variable I used command substitution ( $( .. ) ) which in itself contains another here document
Code:
cat << EOP
    config t
    int $port
    shut
    no shut
    exit
EOP

inside a while loop. In this here document there is another variable $port that gets expanded and becomes part, each time the here document is called.

So in thes case of your sample this command substitution will render:

Code:
    config t
    int Fa1/0/19
    shut
    no shut
    exit
    config t
    int Fa2/0/2
    shut
    no shut
    exit
    config t
    int Fa2/0/12
    shut
    no shut
    exit
    config t
    int Fa2/0/20
    shut
    no shut
    exit

Inside the outer here document, it is then followed by two additional exit statements.
# 11  
Old 09-25-2013
@Scrutinizer thank you so much for taking the time to explain what's going on step by step. I had only seen command-substitution examples before when storing the output to variables; so, it took me some time to realize that it's also possible to just execute what's within $( ) directly as if it's just a single command.

@Corona688 thanks for helping me too!

You guys really make it a pleasure to learn bash scripting. I know it takes skill to be able to help explain how things work in easy-to-understand English.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

2. UNIX for Advanced & Expert Users

Pass Multiple Commands and Open Multiple Xterms via PSS

Hello, I'm attempting to open multiple xterms and run a command as an SAP user via sudo using PSSH. So far, I'm able to run PSSH to a file of servers with no check for keys, open every xterm in to the servers in the file list, and SUDO to the SAP(ADM) user, but it won't do anything else... (11 Replies)
Discussion started by: icemanj
11 Replies

3. Shell Programming and Scripting

Passing multiple files to awk for processing in bash script

Hi, I'm using awk command in bash script. I'm able to pass multiple files to awk for processing.The code i can use is as below(sample code) #!/bin/bash awk -F "," 'BEGIN { ... ... ... }' file1 file2 file3 In the above code i'm passing the file names manually and it is fine till my... (7 Replies)
Discussion started by: shree11
7 Replies

4. Shell Programming and Scripting

Sftp batch processing commands

Hello, I have a UNIX script to sftp batch processing. Here is my sftp command. ftp -b toopc userid@sftp.hostname.com In the file toopc I have the following commands: mget *.csv bye This brings in all files with an extension of .csv However, I need to only bring in files that ... (6 Replies)
Discussion started by: schlinzj
6 Replies

5. Shell Programming and Scripting

plink truncating commands

I'm using plink.exe on WinXP to run some commands on Z/OS BASH. My commands are interspersed with echo commands so that I can parse the output and work out what is where. The first hundred or so commands run fine, but then one of them gets truncated. For example: Input: echo :end_logdetail:... (6 Replies)
Discussion started by: PhilHibbs
6 Replies

6. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

7. 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

8. AIX

Processing of commands in AIX

Whenever we type in a command of AIX, how does it get interpreted by AIX?(backend processing) (2 Replies)
Discussion started by: AIXlearner
2 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

10. Shell Programming and Scripting

Plink connections to multiple unix servers

Hi, I have a simple windows batch file which connects to a UNIX server and runs a shell script in my home directory on the server. It works perfectly, using plink + ssh keys in the background. What I plan to do is expand this batch file to connect to multiple servers and execute one script... (2 Replies)
Discussion started by: Moxy
2 Replies
Login or Register to Ask a Question