Plink (processing multiple commands) using Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Plink (processing multiple commands) using Bash
# 1  
Old 09-20-2013
Question Plink (processing multiple commands) using Bash

I'm completely brand new to bash scripting (migrating from Windows batch file scripting). I'm currently trying to write a bash script that will automatically reset "error-disabled" Cisco switch ports. Please forgive the very crude and inefficient script I have so far (shown below). It is currently able to identify all error-disabled ports on a device and dump them in a text file list called, "ports.txt".

Code:
#!/bin/bash

plink.exe -ssh -l UserID -pw Password01 192.168.1.2 "sho int status" >/cygdrive/c/Users/MKANET/Desktop/CheckIT/output.txt

grep "err-disabled" /cygdrive/c/Users/MKANET/Desktop/CheckIT/output.txt | awk '{print $1}' >ports.txt

Contents of ports.txt: (example)
Code:
Fa1/0/19
Fa2/0/2
Fa2/0/12
Fa2/0/20

I would really appreciate if someone would be kind enough to please provide a "working" example script that would take each port listed in ports.txt as the variable $port; and, use it in the below commands.txt text file (meant for below respective plink command):

Code:
 plink.exe -ssh -l UserID -pw Password01 192.168.1.2  </commands.txt


Contents of commands.txt:
Code:
config t
int $port
shut
no shut
exit
exit
exit

Hopefully, I explained this well enough to understand. I wasn't sure how to word it any better.
Thanks in advance!
MKANET

Last edited by Scott; 09-20-2013 at 03:33 PM.. Reason: Code tags for code and data
# 2  
Old 09-20-2013
Try something like:
Code:
while read port 
do
  plink.exe -ssh -l UserID -pw Password01 192.168.1.2 << EOF
    config t
    int $port
    shut
    no shut
    exit
    exit
    exit
EOF
done < ports.txt

Or perhaps the commands to plink could be combined into one long sequence of commands, so that plink.exe needs to be called only once?
# 3  
Old 09-24-2013
Thanks so much for your help! I didn't realize my forum email notifications were going into spam until just now. Smilie

The example actually works; however, it looks like it's having to authenticate into the device every time it runs through the loop. Is it possible to only do the below line only once then loop the series of commands device commands?

Only once:
Code:
plink.exe -ssh -l UserID -pw Password01 192.168.1.2


Then, loop through the below device commands until it's gone through the ports.txt
list.
Code:
    config t
    int $port
    shut
    no shut
    exit

# 4  
Old 09-24-2013
Hi, do you mean something like this:
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

# 5  
Old 09-24-2013
Wow, it works perfectly! Now that it does what I want, I'm embarrassed to say, I don't know how it exactly works.

It looks like you've made the entire code below into a variable. If so, I don't understand why:
Code:
$(
  while read port
  do
    cat << EOP
    config t
    int $port
    shut
    no shut
    exit
EOP
  done < ports.txt
)

Please correct me if I'm wording it wrong; but it looks like below, you're telling the cat command to stop the cat command mode when it sees "EOP". EOP, can be any pattern.
Code:

    cat << EOP
    config t
    int $port
    shut
    no shut
    exit
EOP

I really appreciate your help. Your help is extremely valuable to me.
# 6  
Old 09-24-2013
Quote:
Originally Posted by MKANET
Please correct me if I'm wording it wrong; but it looks like below, you're telling the cat command to stop the cat command mode when it sees "EOP". EOP, can be any pattern.
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.
# 7  
Old 09-24-2013
$( ) are the modern form of backticks ` `.

He could have also done it with a pipe:

Code:
(
  while read port
  do
    cat << EOP
    config t
    int $port
    shut
    no shut
    exit
EOP
  done < ports.txt
  echo "exit" ) | plink ...

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