Solved

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

Posted on 2007-03-26
19
1,112 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
Comment Utility
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
Comment Utility
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
Comment Utility
I'm working in MS DOS!
0
 

Author Comment

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

Expert Comment

by:SteveGTR
Comment Utility
Did you try any of my suggestions?
0
 

Author Comment

by:alphonsobasemore
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:alphonsobasemore
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanx for the help! I'll post my results.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now