Currently I am working in Excel vba. I dont know much about it.
I am doing file transfer operation using putty sftp command,
I am using below code for the same.
I have also installed winscp & putty on my pc.
My code is:
Dim shell As Object
Dim command As String
Dim errorCode As Integer
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0
Set shell = VBA.CreateObject("WScript.Shell")
command = "cmd.exe /c echo y |"C:\Program Files (x86)\PuTTY\pscp.exe"
-sftp -P 22 -l Username_Here -pw Password_Here
SRC_IP_Address_Here:File_Path_Here " " Destination_Path_Here
' error code is 0 if all ok otherwise 1
errorCode = shell.Run(command, windowStyle, waitOnReturn)
Above code works fine if ip address & destination path exists,
but if ip address is not reachable in network excel takes 20-30 seconds for response & errorCode = 1.
Is it possible to modify above code in such a way that it should give me response only within 10 sec?
How can I set putty sftp connection timeout in above code?
I tried following links but not getting any result:
https://patrickmn.com/aside/how-to-keep-alive-ssh-sessions/
https://www.computerhope.com/unix/sftp.htm
https://library.netapp.com/ecmdocs/ECMP1196993/html/GUID-02FF883B-9913-4137-BC8B-5CB47282B944.html
https://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter5.html
Related
i'm not a expert in python and it's my fist post in stack overflow.
Maybe you can help me
I try to use python instead of vba to create document for a specific application.
This application is used to configure Programmable RTUs
It is possible to automate the creation of documents via excel by activating in excel a reference to this specific application.
This is a exemple to add a specific tag to a exsisting timer in a document with VBA in Excel
Sub timer()
Dim TWsdoc As TWinSoft.Document
Dim MyTag As TWinSoft.Tagname
Dim file_path As String
file_path = ThisWorkbook.Path
Set TWsdoc = GetObject("/RTU:MS32S2", "TWinSoft.Document")
Set TWsdoc = GetObject(file_path & "\test.tws")
Dim MyTimer As TWinSoft.OSCTimer
Set MyTimer = TWsdoc.OSCTimers("Timer_1")
'add Status Tag in Timer_1
Set MyTimer.Status = TWsdoc.Tagnames("Timer_1_State")
TWsdoc.Save 'save the document
Set TWsdoc = Nothing
End Sub
This is the python code i use to:
Creat a tag (status) with soms parameter
creat a timer (Timer 1)
try to associate timer status to a timer
and save the document
I use Visual Studio Code
THE problem, i can not associate the tag Timer_1_Status to the Timer_1 like in vba
I always have this error
Une exception s'est produite : AttributeError Property
'.Status' can not be set.
from multiprocessing import context
import win32com.client #need pywin32 (pip install pywin32)
import os, os.path
file = "\\test.tws"
file_path = os.path.dirname(os.path.abspath(__file__))
file_path = file_path + file
twdoc = win32com.client.Dispatch('Twinsoft.Document')
#-----------Creat Timer Status Tag-----------------
tagTimer1Sta=twdoc.AddTag('Timer_1_Status','DIV')
tagTimer1Sta.comment = 'Timer 1 Status'
tagTimer1Sta.ModbusAddress = 5000
#-----------Creat Timer-----------------
twdoc.AddOSCTimer("Timer_1")
#----------Associate Timer Status Tag to Timer
mytimer = twdoc.OSCTimers("Timer_1")
mytimer.Status = twdoc.Tagnames(tagTimer1Sta)
#-----------Save the created tws Document-----------------------
twdoc.SaveAs(file_path)
I thank you in advance for your help and I apologize for the bad English that I write, I am French.....
Yvan
I have written the following VBA code to automate SAS processes.
Rem Start the SAS server
Dim SASws As SAS.Workspace
Dim SASwsm As New SASWorkspaceManager.WorkspaceManager
Dim strError As String
Set SASws = SASwsm.Workspaces.CreateWorkspaceByServer _
("MySAS", VisibilityProcess, Nothing, "", "", strError)
Dim code_location As String, code_name As String, param_str As String
Dim param_flag As Boolean
code_location = "file:" & ThisWorkbook.Sheets("Control").Range("B2").Value
code_name = ThisWorkbook.Sheets("Control").Range("C2").Value
param_flag = ThisWorkbook.Sheets("Control").Range("C4").Value
If param_flag = True Then
param_str = ThisWorkbook.Sheets("Control").Range("D5").Value
Else: param_str = "ds=Sasuser.Export_output"
End If
Rem Run the stored process
Dim SASproc As SAS.StoredProcessService
Set SASproc = SASws.LanguageService.StoredProcessService
SASproc.Repository = code_location
SASproc.Execute code_name, "ds=Sasuser.Export_output"
'SASproc.Repository = "file:C:\Duopa_Repository\SAS_Codes\Weekly"
'SASproc.Execute "weekly_refresh_run.sas", "ds=Sasuser.Export_output"
Rem Shut down the SAS server
SASwsm.Workspaces.RemoveWorkspaceByUUID SASws.UniqueIdentifier
SASws.Close
Set SASws = Nothing
All the SAS codes seem to work through this except the ones that import csv files using proc import method. Can someone please explain me why
Thanks in advance
I have some problems calling commands from a Linux .net core consol application.
Sub Main()
Dim PI As New ProcessStartInfo
Using Proc As New Process
With PI
.FileName = "cat"
.Arguments = "/etc/*-release"
.RedirectStandardOutput = True
.UseShellExecute = False
.CreateNoWindow = True
End With
Proc.StartInfo = PI
Proc.Start()
PI = Nothing
Console.Write(Proc.StandardOutput.ReadToEnd)
End Using
End Sub
I get this error:
cat: '/etc/*-release': No such file or directory
It work if I disable redirection and set UseShellExecute = True, but I would like to be able to use the output in my code.
When you run a command like
cat /etc/*-release
The linux shell (bash, probably) evaluates each argument, then calls the program with the evaluated arguments. Simple words evaluate to themselves, but variables (like $SOME_ARGUMENT) and patterns (like *) evaluate to the values and file names. So the shell really evaluates the above into (assuming there is just one file):
cat /etc/os-release
And so the cat program just gets that one argument - without any globs like *.
If you want to emulate that behaviour in your program, you need to set UseShellExecute to True.
Or, perhaps you can do the evaluation yourself. APIs like Directory.EnumerateFiles let you do that:
Dim textFiles = Directory.EnumerateFiles("/etc/", "*-release")
Then you need to walk this collection and use the exact file name as your .Arguments value.
Edit: But to go a bit further, why are you even trying to do this? If you have read the file completely, why call cat. Why not just Console.WriteLine() the file itself rather than calling cat?
Ahhh....got it!
I just have to use bash -c ""cat ""/etc/*-release""""
Sub Main()
Dim PI As New ProcessStartInfo
Using Proc As New Process
With PI
.FileName = "bash"
.Arguments = "-c ""cat ""/etc/*-release"""""
.RedirectStandardOutput = True
.UseShellExecute = False
.CreateNoWindow = True
End With
Proc.StartInfo = PI
Proc.Start()
PI = Nothing
Console.Write(Proc.StandardOutput.ReadToEnd)
End Using
End Sub
i have a logoff VB script (it's from http://www.rlmueller.net/Logon7.htm). it's use for limit login user.
In Win 7 the script run just fine but i've get an error in XP SP2 when i restart the PC :
Script : logoff7.vbs
line : 63
char : 9
Error : Permission denied
code : 800A0046
Source : Microsoft VBScrpt runtime error
below is the code :
' Check if flag file exists for this user.
If (objFSO.FileExists(strFlagFile) = True) Then
' Read encoded computer name from the flag file.
Set objFile = objFSO.OpenTextFile(strFlagFile, ForReading)
strLine = objFile.ReadLine
objFile.Close
' Check encoded computer name.
If (strLine = strComputerEncoded) Then
' Delete the file.
objFSO.DeleteFile strFlagFile
End If
Wscript.Quit
End If
script for line: 63 char: 9 is
objFSO.DeleteFile strFlagFile.
please help me to resolve this problem.
thx before, i'm sorry if my english is not well enough.
I have a program that writes files to a network share at a high rate, from a few (3) threads at once.
After running for a while (usually a short while) some of these threads get stuck. Using Process Monitor, I can see that there are calls to WriteFile and CloseFile that simply have no answer.
At this point, I can't shut down the process at all, even killing it from the task manager does nothing.
The interesting thing is that this happens when the computer hosting the shares is running Windows Server 2008 (R2). If I move the shares to a Windows 2003 computer, I don't see these problems. Also, I only see this problem if the program is run on a computer that is running Windows Server 2008 (different computer than the share host).
Here is a short program that quickly reproduces the problem. The files in the source directory range in size from 1 to 20 MB:
Imports System.IO
Imports System.Threading
Module Module1
Private m_sourceFiles As FileInfo()
Private m_targetDir As String
Sub Main(ByVal args As String())
Dim sourceDir As New DirectoryInfo(args(0))
m_sourceFiles = sourceDir.GetFiles()
m_targetDir = args(1)
For i As Integer = 0 To 2
ThreadPool.QueueUserWorkItem(AddressOf DoWork)
Next
Console.ReadLine()
End Sub
Private Const BUFFER_SIZE As Integer = (128 * 1024)
Private Sub DoWork(ByVal o As Object)
Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
Dim random As New Random(Thread.CurrentThread.ManagedThreadId)
While True
Dim fileIndex As Integer = random.Next(m_sourceFiles.Count)
Dim sourceFile As FileInfo = m_sourceFiles(fileIndex)
Dim input As FileStream = sourceFile.OpenRead
Dim targetName As String = sourceFile.Name.Replace(sourceFile.Extension, random.Next(Integer.MaxValue) & sourceFile.Extension)
Dim targetPath As String = m_targetDir & "\" & targetName
Dim output As FileStream = File.Create(targetPath)
Dim bytes() As Byte = New Byte((BUFFER_SIZE) - 1) {}
Dim read As Integer = input.Read(bytes, 0, bytes.Length)
While read <> 0
output.Write(bytes, 0, read)
read = input.Read(bytes, 0, bytes.Length)
End While
output.Flush()
output.Close()
Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
End While
End Sub
End Module
The problem was caused by Symantec Antivirus.
Apparently they don't support 2008 R1 yet.
I was able to workaround the issue by disabling SMB 2.0 on the client computer, as described here:
sc config lanmanworkstation depend= bowser/mrxsmb10/nsi
sc config mrxsmb20 start= disabled