Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

paul.jobb

session.executecommand adjusting timeout

Hi;

I have a powershell script that's using the .net assembly, I am using the executecommand to run a shell script that is rather long running and it appears to terminate the process after about a minute with the following message

Exception calling "ExecuteCommand" with "1" argument(s): "Terminated by user."- error creating ssh session

Is there anyway to increase the timeout value on the execute statement?

The code works against another server where the process I am calling runs under a minute with no issue. Attached is the portion of the script using winscp, its failing at $session.executecommand("./reconfigure_nagios.sh").output

try {
# Load WinSCP .NET assembly
# Use "winscp.dll" for the releases before the latest beta version.
[Reflection.Assembly]::LoadFrom("c:\program files\winscp\WinSCP.dll") | Out-Null
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = $sshhost
$sessionOptions.UserName = $sshid
$sessionOptions.Password = $sshpw
$sessionOptions.SshHostKeyFingerprint = $hostkeyfingerprint.Get_Item($sshhost)

$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles("$cfgfolder\*", "/usr/local/nagios/etc/import/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
"Upload of " + $transfer.FileName + " succeeded"
Remove-Item $transfer.FileName
}
remove-item $cfgfolder
"Removing configuration folder" + $cfgfolder
if ($applyconfig) {
$copytodirectory = $session.ListDirectory("/usr/local/nagios/etc/import/")
if ($copytodirectory.files.count -gt 2) {
"Applying Configuration on " + $sshhost
$session.executecommand(" cd /usr/local/nagiosxi/scripts")
$session.executecommand("./reconfigure_nagios.sh").output
}
else {"usr/local/nagios/etc/import/ directory on " + $sshhost + " empty, configuration not applied."}
}
}
finally {
# Disconnect, clean up
$session.Dispose()
}
}
catch {
$_.Exception.Message + "- error copying files. config files created in " + $cfgfolder + ",please manually upload to nagios"
}