-2

I hope all is well,

I'm struggling to put something together so I thought I would ask here.

I'm trying to find a way to right a shell script that

  • Runs ifconfig
  • grabs just the inet (IPV4)
  • Place it in a variable
  • Echo "My IP"
  • Then print the variables value

I was able to do this successfully in bash with both a ping command:

@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^|
findstr [') do (
    set "MY_IP=%%a"
)
)

echo IP of Current Computer : %MY_IP%
pause>nul & exit

I'm surprised how much I'm struggling with this. Even a solid resource would help me a lot.

Thank you for your time.

Kind regards,

Nico Nekoru
  • 2,840
  • 2
  • 17
  • 38
Johntr3
  • 1
  • 1
  • 3
    Why do you have batch and bash tags? The code you've posted is batch, but then you've got four other Linux-related tags. Which system do you want code for? – SomethingDark Jul 09 '20 at 23:18
  • `batsh.org` may be able to help you. Good luck. – shellter Jul 09 '20 at 23:46
  • 1
    Welcome on Stack Overflow! `ifconfig` does not exist by default on Windows machines. There is available by default `%SystemRoot%\System32\ipconfig.exe`. The second character is `p` for Internet Protocol configuration and not `f` as on Linux for InterFace configuration. We expect on Stack Overflow that programmers first search, next code and test and if the code is not working and the programmer cannot find out the reason, then ask for help. See the search results for [\[batch-file\] ipconfig get IP address](https://stackoverflow.com/search?q=%5Bbatch-file%5D+ipconfig+get+IP+address). – Mofi Jul 10 '20 at 09:07
  • See also the search results for [\[batch-file\] wmic get IP address](https://stackoverflow.com/search?q=%5Bbatch-file%5D+wmic+get+IP+address). – Mofi Jul 10 '20 at 09:10

4 Answers4

1

First of all, if you're looking for your public ip address, you can simply use

ip=$(hostname -I)
echo $ip

Also if you insist on using ifconfig command, you must specify your interface, if your interface for example is 'eth0': (first command from this)

ip=$(ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
echo $ip
mjrezaee
  • 1,100
  • 5
  • 9
  • If I didn't have the `ip` command available, I'd probably use `ip=$(ifconfig -a | awk '/(cast)/ {print $2}')`, which I know works here in `zsh` on macOS too. @NekoMusume, I would not currently assume that this has anything whatsoever to do with Windows batch files. The title, body and tags of the question are all heavily weighted towards a Unix or Mac OS, and Windows does not have `ifconfig`, so I think we should wait for clarification by the OP before we jump to that conclusion. – Compo Jul 10 '20 at 00:42
  • @Compo The code is in batch, if anything this is off topic because it is a `can you convert this to bash` question, but seems more weighted to batch imo... Agree that it needs clarification, taking down comment... – Nico Nekoru Jul 10 '20 at 00:53
  • I know, @NekoMusume, but as the code has no resemblance to the title, question body, or majority of the tags, we do not know for sure at this stage. I do not blame someone for providing and answer which `Runs ifconfig`, `grabs just the inet (IPV4)`, `Place it in a variable` and `Then print the variables value` in code which fulfils the [[tag:linux]], [[tag:bash]], [[tag:shell]], and [[tag:ubuntu]] tags. Personally, I think that the OP has probably done this in 'nix before, and has tried to code something for Windows instead, which has failed to work as intended, but we should wait for the OP. – Compo Jul 10 '20 at 01:02
1

use one of the following


    #!/usr/bin/bash
    IP=`ifconfig wlp3s0 | grep -w inet | cut -d" " -f10 `
    echo $IP

    IP2=`ip addr show wlp3s0 | grep -Po 'inet \K[\d.]+'`
    echo $IP2

replace wlp3s0 with your device name. I would suggest writing your code on "ip addr" instead of ifconfig, as ifconfig may be deprecated on some machines.

I came up with this script which figures all the interfaces and then prints only the ones which have an IP address assigned


    #!/usr/bin/bash
    
    readarray -t interfaces < <(ip l | awk -F ":" '/^[0-9]+:/{dev=$2 ; if ( dev !~ /^ lo$/) {print $2}}')
    for i in "${interfaces[@]// /}" ; 
        do 
            IP=`ip addr show $i | grep -Po 'inet \K[\d.]+'`
            if [ -n $IP ]; then
                echo "Interface is $i"
                echo $IP
            fi
        done

BTW, the above script ignores localhost (lo)

0

I found that this can do the job

address=$(ifconfig | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | grep -Ev '(255|127)')
echo ${address}

I just used a regular expression to grab only the ip format and then exclude localhost and the gateway

Marius
  • 1
0

As you have not clarified whether you're looking for a solution, or an script, and those 'nix based tags have now been edited out, the following is an example of a Windows , which should achieve what your provided code attempted to do. Use the ping command against the computer name to return the IPv4 address, save it as a variable and print the details to the host.

I have written it in such a way as to allow it to function even in Windows 2000, as earlier ping.exe versions, did not have the -4 option.

@Echo Off
SetLocal EnableExtensions
Set "MY_IP="
For /F "Tokens=2 Delims=[]" %%G In (
    '"("%SYSTEMROOT%\System32\ping.exe" "%COMPUTERNAME%" -n 1 -4 2>NUL || "%SYSTEMROOT%\System32\ping.exe" "%COMPUTERNAME%" -n 1 2>NUL) | "%SYSTEMROOT%\System32\find.exe" /I "%COMPUTERNAME%""'
) Do Set "MY_IP=%%G"
If Not Defined MY_IP GoTo :EOF
Echo IP of Current Computer : %MY_IP%
Pause

If you intend using it only on modern versions of Windows OS, then the following shorter version should suffice:

@Echo Off
SetLocal EnableExtensions
Set "MY_IP="
For /F "Tokens=2 Delims=[]" %%G In (
    '""%__APPDIR__%ping.exe" "%COMPUTERNAME%" -n 1 -4 2>NUL | "%__APPDIR__%find.exe" /I "%COMPUTERNAME%""'
) Do Set "MY_IP=%%G"
If Not Defined MY_IP GoTo :EOF
Echo IP of Current Computer : %MY_IP%
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39