VMware Cloud Community
vespavbb
Enthusiast
Enthusiast

invoke change IP Debian

Hi,

I try to do an IP change via invoke-vmscript on an debian 10 vm.

I was able to change the Name but for the IP I have problems, maybe someone knows a quick solution

this seems to work

$scriptname = ("hostnamectl set-hostname "+$vm)

Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $scriptname -guestuser $user -GuestPassword $pw

here I have Problems, path and syntax looks wrong

#NetworkIP

$scriptnet = "sed -i 's:^IPADDR=.*`$:IPADDR=$($IP):g' /etc/network/ifcfg-ens32"

Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $scriptnet -guestuser $user -GuestPassword $pw

sed: can't read /etc/network/ifcfg-ens32:

and what I want to know is, how can I add Gateway and Mask on the same way?

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership

I never use Debian, but that error from the sed command looks like the file doesn't exist or perhaps the user is not permitted to read it.

Can you just try a 'ls -l /etc/network'?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

the invoke looks like this

$test = "ls -l /etc/network?"

$result = Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $test -guestuser $user -GuestPassword $pw

$result

$result = Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $ifconfig -guestuser $user -GuestPassword $pw

$result

ScriptOutput

-----------------------------------------------------------------------------------------------------------------------|  -rw-r--r-- 1 root root 60 Mar 20  2018 /etc/networks

-----------------------------------------------------------------------------------------------------------------------

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership

That looks as if there are no files in that folder.

You could try to create the file with a heredoc.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

on shell it looks like this

so this should be the correct location, but how can I modify this?

cat /etc/network/interfaces

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

# The loopback network interface

auto lo

iface lo inet loopback

# The primary network interface

allow-hotplug eth0

iface eth0 inet static

        address 10.10.10.10

        netmask 225.55.255.255.0

        network 10.1.1.1

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership

If you use the correct filename on the sed command in your earlier script, that should work.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

I dont get it work with the sed -i 's:^IPADDR .... ieven dont know how to add mask and gateway this way

if use it thes simple way it looks ok, but I dont know if it is setup correct in all linux config files?

$scriptnet = "ifconfig ens32 $ip netmask $newMask up"

$result = Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $scriptnet -guestuser $user -GuestPassword $pw

$scriptgw = "route add default gw $newGateway"

$result = Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $scriptgw -guestuser $user -GuestPassword $pw

$scriptcheck = "ifconfig"

$result = Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $scriptcheck -guestuser $user -GuestPassword $pw

$result

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership

Not sure if you used the same sed command, but the file you showed earlier doesn't contain a line with IPADDR=.*


Like I said earlier, I never really used Debian.
But it looks like you are using a sed command intended for /etc/sysconfig/network-scripts/ifcfg-eth0 to change something in /etc/network/interfaces


Did you try your commands from the bash prompt inside the Debian guest OS?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

good Morning,

I testet a bit and found that the ifconfig command is ok, but the configuration will not be safed after reboot. so this will not work

the correct Location is this, and here it needs to be modifed,

how can I invoke the ip information into linux? any idea?

$ vim /etc/network/interfaces

# Content of /etc/network/interfaces

iface eth0 inet static
address 192.168.178.32
netmask 255.255.255.0
gateway 192.168.178.1

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

maybe i found a solution but one little problem here, i hpe it is just a smal thing what i did not see

bash: -c: line 0: syntax error near unexpected token `;'

##############

$VMNetwork=”auto lo

iface lo inet loopback

#The Primary network interface

allow-hotplug ens32

iface ens32 inet static

address $ip

network $newMask

gateway $newGateway

dns-nameservers 8.8.8.8"

$configNetwork=”echo `”$VMNetwork`” >

/etc/network/interfaces.bak;sed `’s/; /\n/g`’

/etc/network/interfaces.bak >

/etc/network/interfaces; rm

/etc/network/interfaces.bak;

service networking restart”

Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $configNetwork -guestuser $user -GuestPassword $pw

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership

Unfortunately, the Invoke-VMScript cmdlet currently does not support here-documents, but my Invoke-VMScriptPlus function does.

On Linux boxes I normally would then do something like this

$vmName = 'MyVM'

$user = 'user'

$pswd = 'VMware1!'

$code = @'

cat > /etc/network/interfaces << EOF

iface eth0 inet static

address 192.168.178.32

netmask 255.255.255.0

gateway 192.168.178.1

EOF

'@


$sInvoke = @{

    VM = Get-VM -Name $vmName

    ScriptText = $code

    ScriptType = 'bash'

    GuestCredential = New-Object PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)

}

Invoke-VMScriptPlus @sInvoke


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast

ah ok,

I forgott how to pass the var in to it, for example $ip

$code = @'

cat > /etc/network/interfaces << EOF

iface eth0 inet static

address `$ip

netmask $newMask

gateway $newGateway

EOF

'@

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership

You can use the ExpandString method.

$vmName = 'MyVM'

$user = 'user'

$pswd = 'VMware1!'

$code = @'

cat > /etc/network/interfaces << EOF

iface eth0 inet static

address $ip

netmask 255.255.255.0

gateway 192.168.178.1

EOF

'@


$ip = '192.168.1.10'

$sInvoke = @{

    VM = Get-VM -Name $vmName

    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)

    ScriptType = 'bash'

    GuestCredential = New-Object PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)

}


Invoke-VMScriptPlus @sInvoke


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos