0

I want to put the name of an existing interface in Linux into a variable so that I can use it using the variable. this is my source:

Write-Output "⚙ Detecting Linux network interface..."

$nicName = Invoke-VMScript -VM $vm -ScriptText "ip -o link show | awk -F': ' '{print `$2}' | grep -v '^lo'" -GuestUser "root" -GuestPassword $pass -ScriptType Bash
$nicNameString = ($nicName.ScriptOutput.Split("`n")[0]).Trim() -replace "`r",""

Write-Output "⚙ Setting IP for Linux VM on interface $nicNameString..."

$scriptLinux = @"
cat <<EOF > /etc/netplan/*.yaml
network:
  version: 2
  ethernets:
    ${nicNameString}:
      dhcp4: no
      addresses: [$IP/24]
      gateway4: $gateway
      nameservers:
        addresses: [8.8.8.8,1.1.1.1]
EOF
netplan apply
systemctl enable ssh
systemctl restart ssh

I tried this command in PowerShell, but the output was as follows: This is the output image of the code.

But I expected my output to be replaced with ens160 which is the original name of the interface. Can you see my problem and guide me?

Because the interface names are different in different Linux distributions, I need to find it automatically and I can't enter a specific interface name.

1
  • Out of pure curiosity - why use powershell (and netplan, which you only find on Ubuntu derivatives by default) when you expect this to work on different distros? Wouldn't it make more sense to choose a toolset that comes with all distros by default (and then determine whether to create /etc/netplan/* or /etc/network/interfaces or /etc/sysconfig/network-scripts/ifcfg*? Commented Sep 8 at 17:54

2 Answers 2

1

From your post, I'm assuming you are looking for ethernet adapter name, please correct if I'm wrong.

Historically the ethernet adapter names in Linux started with "eth" but now it's replaced with prefix "enp" - a good explanation on this is given here and the github link - https://unix.stackexchange.com/questions/134483/why-is-my-ethernet-interface-called-enp0s10-instead-of-eth0/311844#311844

In my experience the best place I can bet on obtaining Linux system information is /proc filesystem. Instead of using the command in your code:

ip -o link show | awk -F': ' '{print `$2}' | grep -v '^lo'

You can try using below code:

grep -E 'enp|eth' /proc/net/dev | cut -d":" -f1

But if you have reasons to stick with "ip" command, may be try bit modified version:

ip -o link show | awk -F': ' '{print $2}' | grep -E "(enp|eth)[0-9]"

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

What I think you really want is the external link, that's whatever it uses as the default route.

ip route | awk '/^default/ { print $5 }'

or if you want to be extra safe scan for the "dev" field:

ip route | awk '/^default/ { while($(++i)!="dev"); print $(++i) }'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.