Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-24-2012, 12:23 PM
DaveFlys DaveFlys is offline
Registered User
 
Join Date: Jan 2012
Posts: 15
'import smtplib' Failure in Python

I am trying to add a function to a Python script that will email me when it finds an error. Everything I see online indicates I should use the import smtplib command to get the ball rolling, but as soon as SecureCRT encounters this line, it dies. It shouldn't matter, but I am attempting to send a message to myself through gMail. Here is the basic code, followed by the error I am getting:

Code:
#$language = "Python"
#$interface = "1.0"

import smtplib

fromaddr = 'fromuser@gmail.com'
toaddrs  = 'touser@gmail.com'
msg = 'There was a terrible error that occured and I wanted you to know!'

# Credentials (if needed)
username = 'username'
password = 'password'

# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Here's the error:

Quote:
ImportError

Error: No module named _socket
File: [path]\sendmail_test_alpha.py
Line: 4

import smtplib
Any idea what I am missing here? SecureCRT seems to have built-in support for sending email, but I cannot seem to find documentation on how to call it from a Python script.

Your help will be much appreciated.
Reply With Quote
  #2  
Old 01-24-2012, 12:48 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi DaveFlys,

Thanks for reporting this problem. We are investigating the issue. I will post to this thread what we find.

To help clarify, SecureCRT does not have any built-in for sending e-mail messages. It does include a Python module that might be able to be used, but it appears as though there is an error associated with trying to use the module.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 01-24-2012, 12:49 PM
DaveFlys DaveFlys is offline
Registered User
 
Join Date: Jan 2012
Posts: 15
Todd,

Thanks for the prompt reply. I'll keep my eye on this thread while you look into the issue.

My very best,
David
Reply With Quote
  #4  
Old 01-24-2012, 12:59 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
You are welcome David.

You can also send a message to support@vandyke.com with a subject of Attn: Todd - Forum thread #10263 if you like me to notify you via e-mail in addition to posting to this thread when we have an update.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 02-08-2012, 02:16 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi All,

One way to work around the issue in this thread is to install a 32-bit or 64-bit version of Python 2.6.

We will post any updates as we have them regarding the use of SecureCRT with the embedded Python that is included rather than having to install Python 2.6 from another source.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #6  
Old 02-14-2012, 02:56 PM
DaveFlys DaveFlys is offline
Registered User
 
Join Date: Jan 2012
Posts: 15
Thumbs up My Findings

Todd,

Based on your suggestions, I made some tests. Here are my findings:
  1. This does NOT work with Python 2.7.x, but it does work with Python 2.6 installed.
  2. This also does not work if you are using a portable installation of SecureCRT running from a flash drive, regardless of whether or not Python 2.6 is installed on the system.
  3. BOTTOM LINE: to make this work, Python 2.6 needs to be installed, and SecureCRT needs to be installed and running from the system's hard disk drive.
I presume this is the case because of Windows registry entries - which do not exist when using a portable installation.

Sooooo, thanks Todd and the rest of the team at Vandyke. I think we have a workaround that will suffice.

~David
Reply With Quote
  #7  
Old 02-14-2012, 03:40 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi DaveFlys,

Thanks for posting your detailed test results. It is appreciated.

We have had a few reports of different modules not working in the Windows version of SecureCRT that has Python embedded. We are investigating the problems. We will post to this thread with any new information specific to the smtplib import issue.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #8  
Old 04-03-2012, 07:46 PM
ozymandius100 ozymandius100 is offline
Registered User
 
Join Date: Feb 2012
Posts: 20
Any updates to this issue? I am trying to utilize ftplib and i receive the _socket error as well. I did not want to have to install Python on the target machine.
Reply With Quote
  #9  
Old 04-04-2012, 12:07 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ozymandius100,

I am checking to see if there is an update on using ftplib. I will post what I find.

In the meantime, can you tell me what problem ftplib helps you solve?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #10  
Old 04-04-2012, 12:12 PM
ozymandius100 ozymandius100 is offline
Registered User
 
Join Date: Feb 2012
Posts: 20
Quote:
Originally Posted by rtb View Post
Hi ozymandius100,

I am checking to see if there is an update on using ftplib. I will post what I find.

In the meantime, can you tell me what problem ftplib helps you solve?
i would like to utilize ftplib so that I can ftp the a log file that is collected at the end of a session. This would be for backup purposes.
Reply With Quote
  #11  
Old 04-04-2012, 12:31 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ozymandius100,

Thanks for the information. Do you need to use FTP, or can you use SFTP instead?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #12  
Old 04-04-2012, 12:33 PM
ozymandius100 ozymandius100 is offline
Registered User
 
Join Date: Feb 2012
Posts: 20
Quote:
Originally Posted by rtb View Post
Hi ozymandius100,

Thanks for the information. Do you need to use FTP, or can you use SFTP instead?
either would be fine. I can always setup a SFTP server if that lib works.

I am currently seeing the same _socket module missing error message as seen with smtplib.
Reply With Quote
  #13  
Old 04-04-2012, 12:59 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ozymandius100,

With regards to the problem using ftplib, adding a more complete distribution of Python to SecureCRT for Windows is not currently on the development roadmap, but it may be considered in the future.

In the meantime, there is the workaround of installing Python 2.6.x on the machine where SecureCRT is installed.

Since you can use SFTP rather than FTP, there is also the option of using the SFTP Tab functionality in SecureCRT rather than a Python module.

We have a VBScript example that illustrates how to automate the use of the SFTP Tab in the following sticky forum post:
http://forums.vandyke.com/showthread.php?t=10365
The name of the example is SFTPTabAutomation.vbs.

Does this seem like an option that might work for you since you don't want to install Python?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #14  
Old 04-04-2012, 01:03 PM
ozymandius100 ozymandius100 is offline
Registered User
 
Join Date: Feb 2012
Posts: 20
i will have to look into it further, since i would need to rewrite the VBS script to be a python one as well for the SFTP functionality.

Installing python as a standalone executable could become problematic going forward.

Does VanDyke have a list of testing libs for python with SecureCRT? I see that ftplib is included in the zip, but obviously it doesn't work. It would be nice to know what does work and what doesn't before investing in the time of writing the code......
Reply With Quote
  #15  
Old 04-04-2012, 04:50 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ozymandius100,

We don't have a list of all the libraries/modules that depend on the missing libraries/modules.

If you haven't converted the script to Python yet, here it is:
Code:
#$language = "Python"
#$interface = "1.0"

# SFTPTabAutomation.py
#   Last Modified: 4 Apr, 2012
# 
# DESCRIPTION:
#   Demonstrates opening an SFTP tab & programmatically driving a file download.
#   Prior to running this script, the currently active tab must already be
#   connected with the SSH2 protocol to a remote server that is capable of
#   providing SFTP functionality; otherwise an error will occur and script
#   execution will be halted.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def Main():
    # Open up an SFTP tab based on the current (script tab) connection
    objSFTPTab = crt.GetScriptTab().ConnectSftp()
    
    # When using Get2, Screen.Synchronous should be False, or else nothing
    # will appear on the screen (since we're not using any WaitForString()
    # or ReadString() methods).
    objSFTPTab.Screen.Synchronous = False
    # Wait for the 'sftp> ' prompt to appear on the screen as an indication that
    # interaction with the SFTP tab can begin.
    while True:
        strScreenText = objSFTPTab.Screen.Get2(
            1,
            1,
            crt.Screen.Rows,
            crt.Screen.Columns)
        
        # If we spot the "sftp> " prompt on the screen, then we're pretty sure
        # it's safe to start interacting with the SFTP tab. Exit the loop:
        if "sftp> " in strScreenText:
            break
        
        # Otherwise, the absence of the "sftp> " prompt on the screen indicates
        # that the tab isn't quite ready to go... sleep for a few milliseconds
        # and then check again...
        crt.Sleep(10)
    
    # Now we can work with the SFTP connection.
    # NOTE: We turn on Synch = True now so that we don't miss any data.
    #       This means that for EVERY Screen.Send(), we must do an accompanying
    #       Screen.WaitForString() so that we don't get out of sync.
    objSFTPTab.Screen.Synchronous = True
    
    # Get a long file listing with the "ls -l" command
    objSFTPTab.Screen.Send("ls -l\r")
    # Use ReadString to wait for the "sftp> " prompt to arrive, capturing all
    # the data we receive in the mean time.
    strFileListing = objSFTPTab.Screen.ReadString("sftp> ")

    # Change local directory (local to the SecureCRT Windows machine) to the
    # folder in which the downloaded file should be saved:
    objSFTPTab.Screen.Send("lcd C:\Temp\r")
    objSFTPTab.Screen.WaitForString("sftp> ")
    
    # Change remote directory to the folder path where the file to be downloaded
    # is known to exist:
    objSFTPTab.Screen.Send("cd tmp\r")
    objSFTPTab.Screen.WaitForString("sftp> ")
    
    #' Get (download) a file named MyFile.txt
    objSFTPTab.Screen.Send("get MyFile.txt\r")
    strResults = objSFTPTab.Screen.ReadString("sftp> ")
    
    # Check for success/failure
    if "100%" in strResults:
        # Put the information we received from the screen into an array where
        # each line of the output gathered is in a unique element of the array
        vDataLines = strResults.split("\r\n")
        # Display success along with the 2nd-to-last line of the information;
        # that's the line that contains stats about the successful transfer.
        crt.Dialog.MessageBox("File transfer successful: \r\r" + vDataLines[-2])
    else:
        crt.Dialog.MessageBox("Failed to get file!\r\r" + strResults)
    
    # Close the SFTP tab once we're done
    objSFTPTab.Close

Main()
Does this help?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 12:12 PM.