ganapa2000
Hot Shot
Hot Shot

Invoke-sshcommand not working

Jump to solution

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

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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


 


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

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


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

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

0 Kudos
LucD
Leadership
Leadership

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?

 


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

I am running from windows powershell

0 Kudos
LucD
Leadership
Leadership

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?


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

0 Kudos
LucD
Leadership
Leadership

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


 


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

0 Kudos