[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help getting the output from a remote command into a variable.

Posted on 2011-04-25
10
Medium Priority
?
587 Views
Last Modified: 2012-05-11
The following command works fine locally but I need it executed on a remote system and the variable available locally.
natlist=( $(iptables -t nat -L -n | grep "^DNAT"|sed "s/"to:"//g"|sed "s/[a-zA-Z-]//g"|awk '{print $1 "," $2 "," $3}' ) )

I'm aware that "ssh 10.0.0.10 "command"" is the way to do it but I got a bunch of awk errors. I assume my formatting is off.
0
Comment
Question by:jasonzx
  • 5
  • 3
  • 2
10 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35463749
Please try this:
command='iptables -t nat -L -n'
natlist=$(ssh IPADDR $command)

natlist=$(echo $natlist | grep "^DNAT"|sed "s/"to:"//g"|sed "s/[a-zA-Z-]//g"|awk '{print $1 "," $2 "," $3}' )

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35463753
I did not try to understand what you were trying to do.  I am sure I could have written a lot concise expression but if the above worked, this should also work remotely

Change IPADDR with 10.0.0.10
0
 

Author Comment

by:jasonzx
ID: 35463983
Sorry, was kind of vague.
I'm trying to connect to a remote machine and check the iptable for anything that is currently being NAT'd and put it into an array in the format <internal ip to NAT>, <external IP>, <ip of machine allowed to access the NAT'd address>

I tried your solution but it didn't work(works if I ssh in and run it locally).
I also tried echo'ing ${natlist[*]} between lines 2 and 4. It's all there but merged together and presumably causing it to fail at the 'grep ^DNAT'
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 31

Expert Comment

by:farzanj
ID: 35464004
Ok, here's the deal.

In line 3 above, lets say you put

echo $natlist

Please show me what you see and explain what you want to get out of it.  I will give you the command for it.
0
 

Author Comment

by:jasonzx
ID: 35464113
echo $natlist returns:
Chain PREROUTING (policy ACCEPT) target prot opt source destination DNAT all -- 10.27.27.210 10.27.137.2 to:10.137.1.2 ACCEPT all -- 0.0.0.0/0 65.124.114.145 ACCEPT tcp -- 10.137.30.189 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 10.137.0.15 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 10.137.30.47 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 10.137.30.4 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 0.0.0.0/0 10.27.0.0/16 tcp dpt:80 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 3128 Chain POSTROUTING (policy ACCEPT) target prot opt source destination SNAT all -- 10.137.1.2 10.27.27.210 to:10.27.137.2 ACCEPT all -- 10.27.137.61 0.0.0.0/0 SNAT all -- !10.27.137.4 0.0.0.0/0 to:10.27.137.4 SNAT all -- !10.27.137.4 0.0.0.0/0 to:10.27.137.4 Chain OUTPUT (policy ACCEPT) target prot opt source destination

At the end, I want natlist to be:
10.27.27.210,10.27.137.2,10.137.1.2

When done locally,
# iptables -t nat -L -n | grep "^DNAT"
looks like:
DNAT       all  --  77.77.77.4           88.88.88.4          to:99.99.99.4
DNAT       all  --  77.77.77.3           88.88.88.3          to:99.99.99.3
DNAT       all  --  77.77.77.2           88.88.88.2          to:99.99.99.2
DNAT       all  --  77.77.77.1           88.88.88.1          to:99.99.99.1

When all is said and done, this is my ultimate goal:
# echo ${oldnatlist[*]}
77.77.77.4,88.88.88.4,99.99.99.4
77.77.77.3,88.88.88.3,99.99.99.3
77.77.77.2,88.88.88.2,99.99.99.2
77.77.77.1,88.88.88.1,99.99.99.1
0
 
LVL 31

Accepted Solution

by:
farzanj earned 2000 total points
ID: 35464193
See if this works. It should and if it doesn't, please show me what you get
command='iptables -t nat -L -n'
natlist=$(ssh IPADDR $command)

echo "$natlist" | grep "^DNAT" | awk '{ print $4","$5","$6}'

Open in new window

0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35464269
ssh 10.0.0.10  "iptables -t nat -L -n" > iptables.log

natlist=$( grep "^DNAT"|sed "s/"to:"//g"|sed "s/[a-zA-Z-]//g"|awk '{print $1 "," $2 "," $3}' iptables.log )
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35464274
Woo, it should be
-----------
ssh 10.0.0.10  "iptables -t nat -L -n" > iptables.log
natlist=$( cat iptables.log | grep "^DNAT"|sed "s/"to:"//g"|sed "s/[a-zA-Z-]//g"|awk '{print $1 "," $2 "," $3}' )
0
 

Author Comment

by:jasonzx
ID: 35465581
farznaj:
That last one worked great and cleaned up a lot of unnecessary junk I had in my code.
I made one tiny change because I wound up with 'to:' in the 3rd field that I didn't want.
 
#!/bin/bash
command='iptables -t nat -L -n'
natlist=$(ssh IPADDR $command)
natlist=$(echo "$natlist" | sed 's/to://')
echo "$natlist" | grep "^DNAT" | awk '{ print $4","$5","$6}'

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35466122
Ok.  Glad it worked.  Basically when you capture a file into a variable, you can retrieve your file back by quoting the variable "$var"
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question