Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Scripting issues -grep

    Scheduled Pinned Locked Moved General pfSense Questions
    10 Posts 3 Posters 3.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      foonus
      last edited by

      Hello all, please bear with the noobery ;D
      I am trying to convert a script from Ubuntu to run on PfSense.

      What i am trying to do is to obtain the IPv4 address from the ovpnc1 interface to use as  "local_ip" variable later on in the script. I am doing this so that i don't have to manually edit the script each time the IP address changes.

      Can' seem  to get the thing to work and only pull the IPv4 address as it did in Ubuntu, on PfSense it spits out a long string of information, including MAC address and at the end of the string is  the IP… i need the syntax to spit out the ipv4 address only as it did with Ubuntu. any idea how to get this working?
      local_ip=$(ifconfig ovpnc1 | grep inet | awk -F: '{print $2}' | awk '{print $1}')

      1 Reply Last reply Reply Quote 0
      • H
        heper
        last edited by

        what kind of script? would be trivial with php

        1 Reply Last reply Reply Quote 0
        • M
          mer
          last edited by

          awk -F:

          that is saying field separator is a colon. 
          ssh into your pfSense box or console and walk through the pieces by hand.

          ifconfig ovpnc1  see what that looks like.  On my FreeBSD 10.2 box all the parts are separated by a space.
          If all you want is the IPV4 address, try:

          local_ip=$(ifconfig ovpnc1 | grep inet | awk '{print $2}')

          No "-F:", no "| awk '{print $1}'"

          1 Reply Last reply Reply Quote 0
          • F
            foonus
            last edited by

            That one is working somewhat,  it  reports the following:
            "
            fe40::21b:21ff:fe41:c220%ovpnc1
            10.104.1.6
            "
            Since the script needs the IPv4 address only saved as a variable to work, any additional data saved in that  variable messes it up.
            I need it to specifically show Only the IPv4 address, that was the major trouble I am having.

            1 Reply Last reply Reply Quote 0
            • F
              foonus
              last edited by

              @heper:

              what kind of script? would be trivial with php

              Its a script i am trying to convert from here https://www.privateinternetaccess.com/forum/discussion/21085/port-forwarding-with-openvpn-on-linux-windows-pfsense-advanced-users
              Intention is to automatically contact the VPN port server and have it open up ports for forwarding to the current dhcp assigned address.

              I have gotten it completely working aside from the issue i am still having with being able to pull the IPv4 address as a variable in a clean form with no additional information (see last post).
              To bypass it atm i am manually editing in my new VPN ip address each time as local_ip=10.x.x.x and then running the script but this is a hassle.

              1 Reply Last reply Reply Quote 0
              • H
                heper
                last edited by

                create a new file in /etc/phpshellsessions (for example 'test123')
                contents of the file:

                
                $tmp=get_interface_info(opt2);
                print_r($tmp['ipaddr']);  
                
                

                (change opt2, to whatever interface you have attached to openvpn)

                it might better in your case to write the ip-addy to a file instead of printing it out like above:

                
                $tmp=get_interface_info(opt2);
                $testfile = fopen("/root/testfile.txt", "w") or die("unable to open file");
                fwrite($testfile, $tmp['ipaddr']);
                fclose($testfile);
                
                

                in your bash/shell script you now only need to call:

                 pfSsh.php playback test123
                

                i'm pretty sure there are some awk ninja's over here that can get whatever you want, using command-line. I find it easier to use the developers shell.

                enjoy

                1 Reply Last reply Reply Quote 0
                • M
                  mer
                  last edited by

                  @foonus:

                  That one is working somewhat,  it  reports the following:
                  "
                  fe40::21b:21ff:fe41:c220%ovpnc1
                  10.104.1.6
                  "
                  Since the script needs the IPv4 address only saved as a variable to work, any additional data saved in that  variable messes it up.
                  I need it to specifically show Only the IPv4 address, that was the major trouble I am having.

                  What is the output of the first part:

                  ifconfig ovpnc1?  I don't have anything configured with that interface, it would help me a lot trying to help you.

                  instead of grep inet, make it grep "inet "

                  add the quotes and a space after inet.  right now "inet6" matches "inet"

                  1 Reply Last reply Reply Quote 0
                  • H
                    heper
                    last edited by

                    @mer:

                    @foonus:

                    That one is working somewhat,  it  reports the following:
                    "
                    fe40::21b:21ff:fe41:c220%ovpnc1
                    10.104.1.6
                    "
                    Since the script needs the IPv4 address only saved as a variable to work, any additional data saved in that  variable messes it up.
                    I need it to specifically show Only the IPv4 address, that was the major trouble I am having.

                    What is the output of the first part:

                    ifconfig ovpnc1?  I don't have anything configured with that interface, it would help me a lot trying to help you.

                    this is a general site2site, not going to a vpn-provider like OP. So awk is possible, when using NR to skip the ipv6 part.

                    
                    [2.3-RC][root@pfsense.lan]/root: ifconfig ovpnc1 | grep inet | awk '{print $2}'
                    fe80::230:5ff:fe3d:7578%ovpnc1
                    192.168.222.2
                    [2.3-RC][root@pfsense.lan]/root: ifconfig ovpnc1 | grep inet | awk 'NR>1 {print $2}'
                    192.168.222.2
                    [2.3-RC][root@pfsense.lan]/root: ifconfig ovpnc1 
                    ovpnc1: flags=8051 <up,pointopoint,running,multicast>metric 0 mtu 1500
                            options=80000 <linkstate>inet6 fe80::230:5ff:fe3d:7578%ovpnc1 prefixlen 64 scopeid 0xa 
                            inet 192.168.222.2 --> 192.168.222.1 netmask 0xffffffff 
                            nd6 options=21 <performnud,auto_linklocal>Opened by PID 60810
                    [2.3-RC][root@pfsense.vbees.lan]/root:</performnud,auto_linklocal></linkstate></up,pointopoint,running,multicast> 
                    
                    1 Reply Last reply Reply Quote 0
                    • M
                      mer
                      last edited by

                      Yep, NR works too.

                      1 Reply Last reply Reply Quote 0
                      • F
                        foonus
                        last edited by

                        Thanks for the help fellas, got it running  ;D

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.