1

I have a bash script which goes out,does some DNS queries and returns an IP address value which I assign as a variable $external

I want to take this variable from the bash script and feed it into python and do some subnet intelligence but I'm not sure how to handle the data after this and pass it back to bash.

I can see that the $external variable is being passed from bash into python ok but this is where I'm not sure what to do next. (thanks Farhan.K for assisting me with what I have already)

python3 <<END
import ipaddress
ipsub = {"10.10.10.0/24": "Firewall-Denver", "10.10.20.0/25": "FirewallNewYork"}
iplist = [$external]

ipfirewall = []


for i in ipsub:
    for j in iplist:
        if ipaddress.ip_address(j) in ipaddress.ip_network(i):
            ipfirewall.append([j,ipsub[i]])

 END

The following would write it to a file:

with open('output.txt', 'w') as file:
file.writelines('\t'.join(i) + '\n' for i in ipfirewall)

But how to I pass it back to bash in the same format?

Thanks in advance for your advice and assitance.

Paul Dawson
  • 1,332
  • 14
  • 27
  • You can simply write to standard output and capture that in a shell variable or operate on it in a pipeline. Is that the question? Also it might be better to have your python script take a command line argument instead of expanding a variable into the script directly (safer and cleaner this way). – Etan Reisner Mar 11 '16 at 17:12
  • 1
    this is very confusing. Why do you need to pass values back and forth between bash and python? Why not trying a "pure" python program, and if there is some bash functionality, call it from there passing values ofer stdout? – Rafael T Mar 11 '16 at 17:12
  • Have you simply tried `sys.stdout.writelines()` and accepting input from stdin in the bash script? – Goodies Mar 11 '16 at 17:12
  • Rafael T - Unfortunately there has already been loads of development done in bash already and I want to use the ipaddress function in python. – Paul Dawson Mar 11 '16 at 17:14
  • Passing a value as `sys.argv[1]` would be a lot more robust than interpolating text into a here document. – tripleee Mar 11 '16 at 17:48

1 Answers1

3

You can do this by writing the intended result to Python's standard output, and capturing the result in a variable.

#!/bin/bash

external="'10.10.10.1','10.10.20.1'"

result="$(python3 <<END
import ipaddress
ipsub = {"10.10.10.0/24": "Firewall-Denver", "10.10.20.0/25": "FirewallNewYork"}
iplist = [$external]

ipfirewall = []


for i in ipsub:
    for j in iplist:
        if ipaddress.ip_address(j) in ipaddress.ip_network(i):
            ipfirewall.append([j,ipsub[i]])
print(ipfirewall)
END
)"

echo "$result"

In the above script, I pass $external into the Python heredoc, run your process, and print the result in Python. The $() construct captures the standard output of the Python script into a Bash variable.

For the more generic version of this question, see How to assign a heredoc value to a variable in Bash?

ndt
  • 376
  • 2
  • 8