Solved

How do I get error codes or output from the sftp command within a batch file?

Posted on 2007-03-26
19
1,126 Views
Last Modified: 2008-01-09
using sftp in a batch file and would like to be able to get error codes for the connection I make to a server from the commands output.
0
Comment
Question by:alphonsobasemore
  • 10
  • 7
  • 2
19 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18796185
You could do this:

sftp www.site.com >errors.txt

If you want to check the error code then you could examine %errorlevel%

sftp www.site.com >errors.txt

echo sftp returned %errorlevel%

You may be able to glean more insight into the workings of sftp by accessing the command line help:

sftp ?

Good Luck,
Steve
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18796370
What sftp client program are you using?  

I don't think any of the Windows operating systems come with a sftp client, only ftp.

The %errorlevel% as described by SteveGTR should be what you are looking for.

0
 

Author Comment

by:alphonsobasemore
ID: 18799738
I'm working in MS DOS!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:alphonsobasemore
ID: 18799759
I'm using an sftp client!  The network I'm using allows both ftp and sftp usage.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18799801
Did you try any of my suggestions?
0
 

Author Comment

by:alphonsobasemore
ID: 18799815
Hi SteveGTR,

     I just tried the sftp www.site.com >errors.txt technique and also I checked the errorlevel variable as needed!  Seems that the errors.txt file doesn't have anything inside of it when it is overwritten.  Also, the errorlevel variable doesn't seem have any value but 0.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18799911
So if you say:

sftp www.someinvalidsite.com >errors.txt

What is displayed at the MS-DOS console?

And nothing is placed in errors.txt?

You could try:

sftp www.someinvalidsite.com 2>errors.txt

What is in errors.txt in this case?

Also, what is output when you do this:

sftp ?

Please answer those questions and maybe we can get this to work for you.

Thanks :)
0
 

Author Comment

by:alphonsobasemore
ID: 18800940
Hi SteveGTR,

     When I perform the sftp ? command Dos doesn't give me what you expect, but instead it tries to login onto the the ?@? server.  The ?@? format I believe is based on the fact that this sftp client is ran by saying

sftp <username>@<servername> , which allows user username to login into the server servername.

Also, when I run the sftp www.someinvalidsite.com 2>errors.txt I get another prompt without any output and then dos comes up with the same window to login into the server www.someinvalidsite.com.

Thanks,

Alphonso
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18801652

Can you supply the version of MS DOS and SFTP that you are using?

The experts will be able to assist you better given this information.

You can find out the version of MS DOS by typing VER at the command line.

You might be able to find the Version of your sftp program by typing sftp /? or just sftp by itself on the command line.  It is most likely a 3rd part sftp program.  If you can provide the link for the vendor of this program that would also be helpful.


0
 

Author Comment

by:alphonsobasemore
ID: 18801993
The MS DOS is: version is 5.1.2600 for MS Windows XP.

At this point I can't get access to the sftp server due to network issues to get the version, but I can get the versioning information for the ftp protocol.  The ftp version is 4.1 and I'd imagine that the sftp protocol wouldn't be any further away from that version.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18801998
Yes, try the following:

sftp /?

or

sftp -?

Post the results.

Also, if you fo this:

sftp www.someinvalidsite.com 2>errors.txt

Enter a bogus user name and password.

What's in errors.txt?
0
 

Author Comment

by:alphonsobasemore
ID: 18802122
Oh on the sftp version.  The only thing I can tell you is that when I do the command sftp by itself I get another command prompt with the windown title:     sftp

and then inside the window the program gives the syntax of the command like so:
-----------------------------------------------------------------------------------------------------

usage: sftp [-b batchfile] [-p port]  [-i identityfile] [-o Cipher]  [user@]host[:file [file]]

press ENTER key to quit

-----------------------------------------------------------------------------------------------------

I imagine you guys might be fimiliar with an sftp client that gives it's syntax within a seperate dos prompt window since most sftp clients run like ftp inside the same prompt right?
0
 

Author Comment

by:alphonsobasemore
ID: 18802209
Hi SteveGTR,

when I run sftp -? or sftp /? I get a seperate dos prompt with a title of sftp -? or sftp /? based on the command I ran and the syntax of the command printed out twice like so:

usage: sftp [-b batchfile] [-p port]  [-i identityfile] [-o Cipher]  [user@]host[:file [file]]

usage: sftp [-b batchfile] [-p port]  [-i identityfile] [-o Cipher]  [user@]host[:file [file]]

press ENTER key to quit

Also, when I do the sftp www.someinvalidesite.com 2>errors.txt command I get nothing in errors.txt. I think this is because of the fact that this sftp client doesn't seem to print anything out at the original command prompt.  The only thing the command does is distribute focus to the new dos prompt the command opens for logging into the sftp server.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18802232
Do you know who makes this version of sftp?

When you type sftp at the command prompt can you identify the program that it is running?

You might be able to do this by doing the following:

c:
cd \
dir /s sftp.*

What does the dir command find?
0
 

Author Comment

by:alphonsobasemore
ID: 18802650
Great idea SteveGTR!

     I just ran the dir /s sftp.* command in the C drive and the version of the sftp is:

          8.0.2119.0 and the Attachmate Corporation is the 3rd party company that created the protocol.

     There also is a sftp.vbs file associated with sftp, but I don't think that file is needed necessarily to figure out this issue or is it really important to the issue.  The fact that Attachmate created the sftp version should be enough right?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18802769
Well the VBS file is what is running the processing. Can you post the contents of the sftp.vbs file?
0
 

Author Comment

by:alphonsobasemore
ID: 18802794
Ya,

here it is:

# $language = "VBScript"
# $interface = "1.0"

' Example script that starts SecureNetTerm, connects to the active profile,
' then waits for a $ prompt.  When it is received, it tests the SFTP commands.

' This assumes an SSH style connection in which no userid or password is
' required to be entered on the command line.

' You should place a very large (4-8MB) file in the host /tmp directory,
' with a name of remote.fil which will be used by this test.

' Globals
dim dir
dim snt

Sub StartSNT
  ' Request to handle errors ourselves so we can handle possible failure
  ' of GetObject() to connect to SecureNetTerm...
  '
  On Error Resume Next
  ' Gets an instance of SecureNetTerm if it is already running
  Set snt = GetObject(,"SecureNetTerm.Document")
  If TypeName(snt) <> "Document" Then
    ' It wasn't already running so start it running.
    Set snt = CreateObject("SecureNetTerm.Document")
  End If
End Sub

Sub Main()
 StartSNT()
 snt.WindowState = 1 'Normal (SW_SHOW)
 If snt.Connected <> TRUE Then
      If snt.Connect("Active") <> TRUE Then
           Exit Sub
      End If
      If snt.WaitForString("$",15) <> TRUE Then
           Exit Sub
      End If
 End If
 ret = snt.SFTP_OpenConnection()
 if ret = TRUE Then
      dir = snt.SFTP_GetCurrentDirectory()
      snt.StatusLine "SFTP Directory = " + dir
      ret = snt.SFTP_SetCurrentDirectory("/tmp")
      if ret = TRUE Then
           ' Make sure test directory/files are not present
           snt.SFTP_DeleteFile("sntData/remote2.fil")
           snt.SFTP_RemoveDirectory("sntData")
           ret = snt.SFTP_GetFile("remote.fil","local.fil")
           if ret = TRUE Then
                dir = snt.SFTP_GetCurrentDirectory()
                snt.StatusLine "SFTP Directory = " + dir
                ret = snt.SFTP_CreateDirectory("sntData")
                if ret = TRUE Then
                     ret = snt.SFTP_PutFile("local.fil","sntData/remote1.fil")
                     if ret = TRUE Then
                          ret = snt.SFTP_RenameFile("sntData/remote1.fil","sntData/remote2.fil")
                          if ret = TRUE Then
                               ret = snt.SFTP_ChMod("sntData/remote2.fil","644")
                               if ret = TRUE Then
                                    ret = snt.SFTP_GetFile("sntData/remote2.fil","local2.fil")
                                    if ret = TRUE Then
                                         ret = snt.SFTP_DeleteFile("sntData/remote2.fil")
                                         if ret = TRUE Then
                                              ret = snt.SFTP_RemoveDirectory("sntData")
                                         End If
                                    End If
                               End If
                          End If
                     End If
                End If
           End If
      End If
 snt.SFTP_CloseConnection()
 snt.StatusLine ""
 End If
End Sub
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 18802935
Looks like this script drives InterSoft Internation, Inc's SecureNetTerm.

I'm not familiar with this software and don't have the time to learn it. Here's a link to the PDF manual.

http://www.securenetterm.com/pub/SecureNetTerm.pdf

Good Luck,
Steve
0
 

Author Comment

by:alphonsobasemore
ID: 18802958
Thanx for the help! I'll post my results.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question