Automation

 View Only
  • 1.  Invoke-sshcommand not working

    Posted Sep 07, 2022 05:01 PM

    Hi,

    I am unable to run the below script on Linux VMs remotely as I am getting the below error

    Please help

    $script = @"
    fqdn=$(hostname)
    ipaddress=`/sbin/ifconfig ens160 | grep 'inet' | awk '{print $2}' | sed -e s/.*://`
    mydnsserver=$(nslookup -type=soa $(hostname -d) | grep origin | awk -F'= ' '{print $2}')
    echo "server $mydnsserver
    update delete $fqdn.mydomain.com A
    update add $fqdn.mydomain.com 3600 IN A $ipaddress
    send
    quit
    " | nsupdate
    "@

    $serv = "192.168.1.100"
    $Username = 'root'
    $pass = ConvertTo-SecureString -AsPlainText 'password' -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
    $connected = $session.Connected
    $result = Invoke-SSHCommand -SSHSession $session -Command $($script) -Verbose
    $result.Output
    Remove-SSHSession $session -Verbose | Out-Null

     

    Error

    ganapa2000_0-1662570065013.png

     



  • 2.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 05:12 PM

    Did you already try with single quotes for the here-string

    $script = @'
    fqdn=$(hostname)
    ipaddress=`/sbin/ifconfig ens160 | grep 'inet' | awk '{print $2}' | sed -e s/.*://`
    mydnsserver=$(nslookup -type=soa $(hostname -d) | grep origin | awk -F'= ' '{print $2}')
    echo "server $mydnsserver
    update delete $fqdn.mydomain.com A
    update add $fqdn.mydomain.com 3600 IN A $ipaddress
    send
    quit
    " | nsupdate
    '@
    
    $serv = "192.168.1.100"
    $Username = 'root'
    $pass = ConvertTo-SecureString -AsPlainText 'password' -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey -ErrorAction Stop
    $connected = $session.Connected
    $result = Invoke-SSHCommand -SSHSession $session -Command $($script) -Verbose
    $result.Output
    Remove-SSHSession $session -Verbose | Out-Null


  • 3.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 05:24 PM

    LucD,

    Now it executes without any error but the command seems to did not work via script as I am unable to ping



  • 4.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 06:28 PM

    I didn't analyse the actual code you are running there.

    Did you try to run that code from an actual SSH session from the command prompt?

     



  • 5.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 06:40 PM

    LucD,

    I am running from windows powershell



  • 6.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 06:42 PM

    That is not what I mean.
    Can you connect to that Linux box (via ssh) and run the commands in $code from the command prompt?
    Does it work that way?



  • 7.  RE: Invoke-sshcommand not working

    Posted Sep 07, 2022 07:05 PM

    LucD,

    if I run the below code directly via ssh that works as expected

    fqdn=$(hostname)
    ipaddress=`/sbin/ifconfig ens160 | grep 'inet' | awk '{print $2}' | sed -e s/.*://`
    mydnsserver=$(nslookup -type=soa $(hostname -d) | grep origin | awk -F'= ' '{print $2}')
    echo "server $mydnsserver
    update delete $fqdn.mydomain.com A
    update add $fqdn.mydomain.com 3600 IN A $ipaddress
    send
    quit
    " | nsupdate



  • 8.  RE: Invoke-sshcommand not working
    Best Answer

    Posted Sep 07, 2022 09:08 PM

    Ok, there are a couple of issues with your script

    1. If the NIC also has IPv6 the grep 'inet' will return both, hence the change to grep 'inet ' (blank after inet).
    This cuases the inet6 to be ignored

    2. By default the stderr stream is not returned, only the stdout.
    To see any errors from the nsupdate command you need to redirect the stderr to stdout

    3. This script was most probably created on a Windows box, and the lines are ended with a <CR><LF>
    This will cause problems on a Linux box where only <LF> is used.
    Hence the removal of all <CR> in the $script variable, effectively only using <LF> at the end of each line.

    With these changes, the code works on my test Linux box.
    Be aware that depending on the configuration of your DNS servers you might get an error "update failed: REFUSED".
    This has nothing to do with the script,  it's due to the configuration of your DNS server(s)

    $script = @'
    fqdn=`hostname`
    ipaddress=`/sbin/ifconfig ens160 | grep 'inet ' | awk '{print $2}' | sed -e s/.*://`
    mydnsserver=$(nslookup -type=soa $(hostname -d) | grep origin | awk -F'= ' '{print $2}')
    echo "server ${mydnsserver}
    update delete ${fqdn}.mydomain.com A
    update add ${fqdn}.mydomain.com 3600 IN A ${ipaddress}
    send
    quit
    " | nsupdate 2>&1
    '@
    
    $serv = "192.168.1.100"
    $Username = 'root'
    $pass = ConvertTo-SecureString -AsPlainText 'password' -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
    
    $session = New-SSHSession $serv -Credential $Cred -AcceptKey
    $connected = $session.Connected
    $result = Invoke-SSHCommand -SSHSession $session -Command $script.replace("`r", '') -Verbose
    $result.Output
    Remove-SSHSession $session -Verbose | Out-Null


     



  • 9.  RE: Invoke-sshcommand not working

    Posted Sep 08, 2022 07:45 AM

    Thank you very much LucD that worked. as mentioned the encoding could to be one of the issue.