PowerCLI

 View Only
Expand all | Collapse all

invoke change IP Debian

  • 1.  invoke change IP Debian

    Posted Apr 14, 2020 03:04 PM

    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?



  • 2.  RE: invoke change IP Debian

    Posted Apr 14, 2020 03:44 PM

    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'?



  • 3.  RE: invoke change IP Debian

    Posted Apr 14, 2020 04:34 PM

    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

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



  • 4.  RE: invoke change IP Debian

    Posted Apr 14, 2020 04:45 PM

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

    You could try to create the file with a heredoc.



  • 5.  RE: invoke change IP Debian

    Posted Apr 14, 2020 04:50 PM

    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



  • 6.  RE: invoke change IP Debian

    Posted Apr 14, 2020 04:57 PM

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



  • 7.  RE: invoke change IP Debian

    Posted Apr 14, 2020 05:26 PM

    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



  • 8.  RE: invoke change IP Debian

    Posted Apr 14, 2020 05:52 PM

    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?



  • 9.  RE: invoke change IP Debian

    Posted Apr 15, 2020 07:32 AM

    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



  • 10.  RE: invoke change IP Debian

    Posted Apr 15, 2020 09:15 AM

    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



  • 11.  RE: invoke change IP Debian

    Posted Apr 15, 2020 09:46 AM

    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



  • 12.  RE: invoke change IP Debian

    Posted Apr 15, 2020 11:10 AM

    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

    '@



  • 13.  RE: invoke change IP Debian

    Posted Apr 15, 2020 11:18 AM

    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