Running application from command line using vb.net

Advertisement

voicon
Joined:
Posts:
6

Running application from command line using vb.net

Hi

I have a problem when trying to run a .sh file when using vb.net to write to command line.

I put the xxxx.sh file in the root of th eserver but I am unable to run it, here is the code...

        winscp.StandardInput.WriteLine("option batch abort")
        winscp.StandardInput.WriteLine("option confirm off")
        winscp.StandardInput.WriteLine("open sftp://10.129.8.126:22")
        winscp.StandardInput.WriteLine("user")
        winscp.StandardInput.WriteLine("password")
        winscp.StandardInput.WriteLine("ls")
        winscp.StandardInput.WriteLine("cd /")
        winscp.StandardInput.WriteLine("put c:\directory\example.sh")
        System.Threading.Thread.Sleep(10000)
        winscp.StandardInput.WriteLine("chmod 777 uc3_log.sh")
        winscp.StandardInput.WriteLine("/console ./example.sh")    

I tried scripting it but that failed also.

If I enter ./example.sh from the console within WinSCP it works fine.

Any ideas how I can do this?

Cheers

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

Use "call ./example.sh" instead "/console ...".
_________________
Martin Prikryl

Reply with quote

voicon
Joined:
Posts:
6

Hi Martin

I just got that bit working by changing the connection from sftp to scp and use "call ./example.sh" so thanks for that :)

Now I get "Timeout waiting for external console to complete the command" whilst the process runs, the process will take aound 30 seconds to complete, it is zipping around 1Gb worth of text files?

Thanks

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

What does the rest of your script look like?
_________________
Martin Prikryl

Reply with quote

voicon
Joined:
Posts:
6

create .sh file
Dim TextFile As New StreamWriter("c:\directory\example.sh")

TextFile.WriteLine("ddir=" & Chr(&H22) & "/tmp" & Chr(&H22))
TextFile.WriteLine("host_str=" & "'" & "hostname" & "'")
TextFile.WriteLine("tarfile=" & Chr(&H22) & "ServerLogs.tar.gz" & Chr(&H22))
TextFile.WriteLine("c_flag=0")
TextFile.WriteLine("tar 2>/dev/null --ignore-failed-read -czvf $ddir/$tarfile \")
TextFile.WriteLine("/var/log/messages \")
TextFile.WriteLine("/opt/intertel/log/* \")
TextFile.WriteLine("exit 0")
TextFile.Close()


Const logname As String = "log.xml"

        ' Run hidden WinSCP process
        Dim winscp As Process = New Process()
        winscp.StartInfo.FileName = "C:\Program Files (x86)\WinSCP\winscp.com"
        winscp.StartInfo.Arguments = "/log=" + logname
        winscp.StartInfo.UseShellExecute = False
        winscp.StartInfo.RedirectStandardInput = True
        winscp.StartInfo.RedirectStandardOutput = True
        winscp.StartInfo.CreateNoWindow = True
        winscp.Start()

        ' Feed in the scripting commands
        winscp.StandardInput.WriteLine("option batch abort")
        winscp.StandardInput.WriteLine("option confirm off")
        winscp.StandardInput.WriteLine("open scp://10.129.8.126:22")
        winscp.StandardInput.WriteLine("user")
        winscp.StandardInput.WriteLine("pass")
        winscp.StandardInput.WriteLine("ls")
        winscp.StandardInput.WriteLine("cd /")
        winscp.StandardInput.WriteLine("put c:\directory\example.sh")
        System.Threading.Thread.Sleep(5000)
        winscp.StandardInput.WriteLine("chmod 777 example.sh")
        winscp.StandardInput.WriteLine("call ./example.sh")
        System.Threading.Thread.Sleep(60000)
        winscp.StandardInput.WriteLine("get /tmp/ServerLogs.tar.gz c:\directory\")
        System.Threading.Thread.Sleep(60000)
        winscp.StandardInput.Close()

        ' Collect all output (not used in this example)
        Dim output As String = winscp.StandardOutput.ReadToEnd()

        ' Wait until WinSCP finishes
        winscp.WaitForExit()

        ' Parse and interpret the XML log
        ' (Note that in case of fatal failure the log file may not exist at all)
        Dim log As XPathDocument = New XPathDocument(logname)
        Dim ns As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
        ns.AddNamespace("w", "http://winscp.net/schema/session/1.0")
        Dim nav As XPathNavigator = log.CreateNavigator()

        ' Success (0) or error?
        If winscp.ExitCode <> 0 Then

            Console.WriteLine("Error occured")

            ' See if there are any messages associated with the error
            For Each message As XPathNavigator In nav.Select("//w:message", ns)
                Console.WriteLine(message.Value)
            Next

        Else

            ' It can be worth looking for directory listing even in case of
            ' error as possibly only upload may fail

            Dim files As XPathNodeIterator = nav.Select("//w:file", ns)
            Console.WriteLine(String.Format("There are {0} files and subdirectories:", files.Count))
            For Each file As XPathNavigator In files
                Console.WriteLine(file.SelectSingleNode("w:filename/@value", ns).Value)
            Next

        End If    

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

Ok, at what line does the error occur?
_________________
Martin Prikryl

Reply with quote

voicon
Joined:
Posts:
6

It starts winscp.StandardInput.WriteLine("call ./example.sh") but only completes around 80% of the .sh, you can see what it does at the top of the code. It is zipping a number of files which takes around 25-30 seconds.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

I would try removing the Sleep commands. It may help. They are useless anyway.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

voicon wrote:

It starts winscp.StandardInput.WriteLine("call ./example.sh") but only completes around 80% of the .sh, you can see what it does at the top of the code. It is zipping a number of files which takes around 25-30 seconds.
Just to be sure. Did you mean it started to execute example.sh on the remote side or that it indeed stopped on that line of the VB.NET code? That's huge difference. If the first, what VB.NET code was running when the error occurs?
_________________
Martin Prikryl

Reply with quote

Advertisement

You can post new topics in this forum