SFTP script

Clovertech Forums Read Only Archives Cloverleaf General SFTP script

  • Creator
    Topic
  • #48846
    Steve Carter
    Participant

      I’ve been tasked with passing order and result data to a client’s system via SFTP.  Does anyone have a script that they would be willing to share?

      If not, can anyone give me some ideas on where to start?  Too bad Tcl doesn’t have an SFTP package…..or does it???

      Thanks.

      Steve

    Viewing 14 reply threads
    • Author
      Replies
      • #59882

        Here’s what I use …

        Code:

        ######################################################################
        # Name:      tps_sftp_out
        # Purpose:   Custom protocol for sending message via sftp
        # UPoC type:   tps
        # Args:    tps keyedlist containing the following keys:
        #          MODE    run mode (”start”, “run” or “time”)
        #          MSGID   message handle
        #          ARGS    user-supplied arguments:
        #                  SFTPSERVER – sftp server hostname or IP
        #                  SFTPUSER   – sftp user name
        #                  SFTPPWD    – sftp user password
        #                  EXPECTSN   – Expect script name (temp)
        #
        # Returns: tps disposition list:
        #          
        #

        proc tps_sftp_out { args } {
           keylget args MODE mode                 ;# Fetch mode
           keylget args ARGS uargs                ;# Get user arguments
           keylget args CONTEXT ctx               ;# Context

           global HciConnName HciRootDir HciSiteDir ibdir

           if { ! [info exists HciConnName] } {
               set HciConnName “UNKNOWN_TD”
           }
           set procName [lindex [info level 0] 0]
           set module “$HciConnName: $procName”

           set dispList {}            ;# Nothing to return

           #puts $args

           switch -exact — $mode {
               start {
                   # Perform special init functions
                   # N.B.: there may or may not be a MSGID key in args
               }

               run {
                   # ‘run’ mode always has a MSGID; fetch and process it
                   keylget args MSGID mh

                   # Get Mid (message ID) from the message metadata
                   set midList [msgmetaget $mh MID]
                   keylget midList NUM msgId

                   # Expect script name
                   keylget uargs EXPECTSN expectSN
                   if { ![info exists expectSN] } {
                       set expectSN “es.${msgId}”
                   }
                   set esn [file join ${HciRootDir} temp ${expectSN}.${HciConnName}.${msgId}]

                   # sftp user name
                   keylget uargs SFTPUSER user
                   if { ![info exists user] } {
                       error “$module Please set up SFTPUSER parameter”
                   }

                   # sftp password
                   keylget uargs SFTPPWD password
                   if { ![info exists password] } {
                       error “$module Please set up SFTPPWD parameter”
                   }

                   # sftp host name
                   keylget uargs SFTPSERVER host
                   if { ![info exists host] } {
                       error “$module Please set up SFTPSERVER”
                   }

                   #set userdata [msgmetaget $mh USERDATA]
                   #keylget userdata OBFILENAME remoteFile
                   #puts [msgdump $mh]

                   # dump incoming message into a temp file
                   #set tfn [file join ${HciRootDir} temp msg.${HciConnName}.${msgId}]
                   #set tf [open $tfn w]
                   #msgwrite raw $mh $tf
                   #flush $tf
                   #close $tf

                   # modified to wirk with tpsScanDir
                   # get the list of files
                   set fileList [msgget $mh]

                   # create expect script
                   set es [open $esn w]
                   puts $es “spawn sftp $user@$host”
                   puts $es “expect {”
                   puts -nonewline $es {”password:” }
                   puts $es “{send “$password\n”}”
                   puts $es {”Connection closed” return}
                   puts $es “}”
                   #puts $es {expect “sftp>”}
                   puts $es “expect {”
                   puts $es “timeout {exit 1}”
                   puts $es {”sftp>”}
                   puts $es “}”
                   foreach tfn $fileList {
                       regsub {tpsScanDir.} $tfn {} remoteFile
                       puts $es “send “put $ibdir/$tfn $remoteFile\n””
                       puts $es {expect “sftp>”}
                   }
                   #puts $es “send “put $tfn $remoteFile\n””
                   #puts $es {expect “sftp>”}
                   puts $es “send “bye\n””
                   flush $es
                   close $es

                   # execute expect
                   set retVal [exec $HciRootDir/tcl/bin/expect $esn >>& $HciSiteDir/iig/files/sftp.out]
                   #puts “$module retVal: <$retVal>”
                   if { [info exists errorCode] } {
                       puts “$module errorCode: <$errorCode>”
                   }
                   #exec /quovadx/qdx5.2/integrator/tcl/bin/expect $esn

                   #msgdump $mh

                   foreach tfn $fileList {
                       file delete $ibdir/$tfn
                   }

                   lappend dispList “CONTINUE $mh”

                   file delete $esn
               }

               time {
                   # Timer-based processing
                   # N.B.: there may or may not be a MSGID key in args
               }

               shutdown {
                   # Doing some clean-up work
               }
           }

           return $dispList
        }

        -- Max Drown (Infor)

      • #59883
        Steve Carter
        Participant

          Thanks.  I’ll check it out.

          Steve

        • #59884
          Robert Kersemakers
          Participant

            I dug up this old post as we are on CL5.5 (on our way to CL6.0 on a new platform) and need to have some SFTP functionality. This script works perfectly!

            Max: I haven’t used ‘expect’ before, so if you also have a script for receiving/getting files through SFTP, that would be great! Thanks in advance.

            Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

          • #59885

            Cloverleaf now have a component called Secure Messenger that adds sftp/ftps functionality to the cloverleaf ftp protocol. The module makes it significantly easier to work with sftp/ftps connections. It requires a license, so get with your AE if you would like to obtain this module.

            That being said, here’s an working example of how I’ve done sftp in the past. I have not done a script for ftps. I wrote this years ago in an older version of Cloverleaf, I’d do it differently today. To set this up correctly, you need a working knowledge of how sftp works, especially sftp certificates. Google has many tutorials on this. Also, if I were to do this today, I’d use a timer thread (UPOC protocol) instead of cron. And, for newer version of Cloverleaf, use CL_INSTALL_DIR instead of QUOVADX_INSTALL_DIR or use the $HciRoot and $HciSiteDir globals in a UPOC.

            SSH Config File

            Code:

            #ssh_config file
            Host=
            HostbasedAuthentication=yes
            HostName=
            IdentityFile=./.ssh/id_rsa
            LogLevel=VERBOSE
            PasswordAuthentication=yes
            PubkeyAuthentication=yes
            RhostsRSAAuthentication=yes
            RSAAuthentication=yes
            StrictHostKeyChecking=no
            User=
            UserKnownHostsFile=./.ssh/known_hosts

            Cron Script

            Code:

            # Call the script from cron

            # Set up the Cloverleaf Environment
            export QUOVADX_INSTALL_DIR=/qdx/qdx5.4
            root=${QUOVADX_INSTALL_DIR}/integrator
            site=
            eval `${QUOVADX_INSTALL_DIR}/integrator/sbin/hcisetenv -root ksh $root $site`

            # Run the SiteDoc script
            cd $HCISITEDIR/data/sftp
            $HCISITEDIR/data/sftp/sftp.tcl

            Tcl Script

            #!/qdx/qdx5.4/integrator/bin/tcl

            global HciSiteDir

            # Create the sftp commands file
            set f [open ./sftp.commands w+]
            set date [clock format [clock scan “yesterday”] -format %Y%m%d]
            set file $HciSiteDir/data/sftp/to.$date
            puts $f “ls -al”
            puts $f “put $file”
            puts $f “ls -al”
            puts $f “exit”
            close $f

            # Transfer the file
            set log [open sftp.log w]
            puts $log “nBegin file transfer: [clock format [clock scan now]]n”
            if {[catch {exec sftp -F ./sftp.config -C -b ./sftp.commands } cr] && $::errorCode != “NONE”} {

            [code]#!/qdx/qdx5.4/integrator/bin/tcl

            global HciSiteDir

            # Create the sftp commands file
            set f [open ./sftp.commands w+]
            set date [clock format [clock scan “yesterday”] -format %Y%m%d]
            set file $HciSiteDir/data/sftp/to.$date
            puts $f “ls -al”
            puts $f “put $file”
            puts $f “ls -al”
            puts $f “exit”
            close $f

            # Transfer the file
            set log [open sftp.log w]
            puts $log “nBegin file transfer: [clock format [clock scan now]]n”
            if {[catch {exec sftp -F ./sftp.config -C -b ./sftp.commands } cr] && $::errorCode != “NONE”} {

            -- Max Drown (Infor)

          • #59886
            Russ Ross
            Participant

              I have a couple of SFTP scripts called

              Russ Ross
              RussRoss318@gmail.com

            • #59887
              Robert Kersemakers
              Participant

                Thanks Max and Russ for all your help and information. I really like Max’s original solution to send a message through sftp from tps/upoc inside CL. So I am going to try to build a similar tps for receiving files/messages, time permitted…

                I am sure going to ask for the Secure Courier option for CL6.0 with both sftp and ftps capabilities. You never know what people ask in the future and if they do, they always want it yesterday.

                Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

              • #59888
                Ian Morris
                Participant

                  I also have a need to transmit files to vendors via SFTP.  I’d like to employ the script max first posted in this thread.  However, I’m unclear on which thread configuration to use and exactly where to place this proc.  

                  Max or Robert, Did you create a single thread and specify the .tcl on the “Inbound – TPS Inbound Data” field in order to create your interface?

                • #59889
                  Yves Guerin
                  Participant

                    Dear,

                    to send file to ftp server I used tclCurl (bundle in cloverleaf) from my tcl script, works great and more easy than using expect.

                    Regards,

                    Yves

                  • #59890
                    Ian Morris
                    Participant

                      Yves Guerin wrote:

                      Dear,

                      to send file to ftp server I used tclCurl (bundle in cloverleaf) from my tcl script, works great and more easy than using expect.

                      Regards,

                      Yves

                      Interesting!  Did you use it for SFTP, though?

                    • #59891
                      Robert Kersemakers
                      Participant

                        Hi Ian,

                        Here are the scripts I made, based on Max’s script. We use these to get and put files/messages through sftp. So these suit our needs, but feel free to change things.

                        First the script to get files:

                        ######################################################################
                        # Name:      orbis_sftp_in
                        # Purpose:   Custom protocol for receiving messages via sftp
                        #            Put this proc in UPOC to read the files from sftp.
                        #            Also put this in TPS Inbound Data to pass the original
                        #            filename of the file. This is done through a global
                        #            ‘orbis_sftp_filenames’ which is a list of filenames.
                        # UPoC type:   tps
                        # Args:    tps keyedlist containing the following keys:
                        #          MODE    run mode (”start”, “run” or “time”)
                        #          MSGID   message handle
                        #          ARGS    user-supplied arguments:
                        #                  SFTPSERVER – sftp server hostname or IP
                        #                  SFTPUSER   – sftp user name
                        #                  SFTPPWD    – sftp user password
                        #                  SFTPDIR    – sftp directory where to get files; all files are taken!!
                        #                  SFTPFILTER – simpel filter (like ‘*.txt’ or ‘*log*.txt’) to get certain files.
                        #                               Default: ‘*’ (= all files in directory).
                        #                  BACKUPDIR  – directory (local, so should be available by Cloverleaf) where a copy of the files are placed
                        #                               Default: empty, so no backup is made
                        #                  LOCALDIR   – local directory where files are temporarily saved
                        #                  TMPDIR     – local tmp directory where expect script is written; should be different from LOCALDIR!
                        #                               Default: ${HCIROOTDIR}/temp
                        #                  EXPECTSN   – Expect script name (temp)
                        #                  DEBUG      – Debug information level (default = 0)
                        #
                        # Returns: tps disposition list:
                        #          
                        #

                        proc orbis_sftp_in { args } {
                           keylget args MODE mode                 ;# Fetch mode
                           keylget args ARGS uargs                ;# Get user arguments
                           keylget args CONTEXT ctx               ;# Context

                           global HciConnName HciRootDir HciSiteDir orbis_sftp_filenames

                           set debug 0 ; keylget uargs DEBUG debug

                           if { ! [info exists HciConnName] } {
                               set HciConnName “UNKNOWN_TD”
                           }
                           set procName [lindex [info level 0] 0]
                           set module “$HciConnName: $procName”

                           set dispList {}            ;# Nothing to return

                           switch -exact — $mode {
                               start {
                                   # Perform special init functions
                                   # N.B.: there may or may not be a MSGID key in args

                                   # Initialiseer de global ‘orbis_sftp_filenames’
                                   set orbis_sftp_filenames {}

                                   if {$debug > 0} {
                                     echo “Start – orbis_sftp_filenames: ”
                                   }

                               }

                               run {
                                   # ‘run’ mode always has a MSGID; fetch and process it

                                   # Neem het eerste element van global ‘orbis_sftp_filenames’ als naam.
                                   # Indien deze global leeg is, dan een tijdelijke naam verzinnen.

                                   keylget args MSGID mh
                                   set filename [lvarpop orbis_sftp_filenames 0]

                                   # Indien geen originele filenaam, dan filenaam maken
                                   if {[string length $filename] 0} {
                                     echo “Run – filename: ”
                                   }

                               }

                               time {
                                   # Timer-based processing
                                   # N.B.: there may or may not be a MSGID key in args

                                   # sftp user name
                                   keylget uargs SFTPUSER user
                                   if { ![info exists user] } {
                                     error “$module Please set up SFTPUSER parameter”
                                   }

                                   # sftp password
                                   keylget uargs SFTPPWD password
                                   if { ![info exists password] } {
                                     error “$module Please set up SFTPPWD parameter”
                                   }

                                   # sftp host name
                                   keylget uargs SFTPSERVER host
                                   if { ![info exists host] } {
                                     error “$module Please set up SFTPSERVER”
                                   }

                                   # sftp dir name
                                   keylget uargs SFTPDIR dir
                                   if { ![info exists dir] } {
                                     error “$module Please set up SFTPDIR”
                                   }

                                   # sftp filter
                                   set filter “*”
                                   keylget uargs SFTPFILTER filter

                                   # backup dir
                                   set backupdir “”
                                   keylget uargs BACKUPDIR backupdir
                                   if { ([string length $backupdir] > 0) && ![file isdirectory $backupdir] } {
                                     error “$module BACKUPDIR ${backupdir} does not exist”
                                   }

                                   # local dir name
                                   keylget uargs LOCALDIR localdir
                                   if { ![info exists localdir] || ![file isdirectory $localdir] } {
                                     error “$module Please set up LOCALDIR”
                                   }

                                   # tmp dir name
                                   set tmpdir [file join ${HciRootDir} temp]
                                   keylget uargs TMPDIR tmpdir
                                   if { ![file isdirectory $tmpdir] } {
                                     error “$module Please set up TMPDIR”
                                   }

                                   # Expect script name
                                   keylget uargs EXPECTSN expectSN
                                   if { ![info exists expectSN] } {
                                     # Bepaal uniek nummer om bestandsnaam uniek te maken
                                     set ctrfile “orbis_sftp_in_icr”
                                     if ![file exists “$ctrfile.ctr”] {
                                       CtrInitCounter $ctrfile file 1 10000 1
                                     }
                                     # Get next counter value
                                     set number [CtrNextValue $ctrfile file]
                                     set expectSN “sftp_in_tmpscript_${number}”
                                   }
                                   set esn [file join ${tmpdir} ${expectSN}]

                                   # Haal nu de bestanden via sftp uit de genoemde directory op
                                   # en zet deze in de localdir.

                                   # create expect script
                                   set es [open $esn w]

                                   # Zet de timeout op 60 seconden
                                   puts $es “set timeout 60”
                                   # Start sftp
                                   puts $es “spawn sftp $user@$host”
                                   # Wacht op wachtwoord en voer dit in
                                   puts $es “expect {”
                                   puts $es ” “password:” {send “$password\r”}”
                                   puts $es ” “Connection closed” return”
                                   puts $es “}”
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Ga lokaal naar local directory
                                   puts $es “send “lcd $localdir\r””
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Ga remote naar de sftp directory
                                   puts $es “send “cd $dir\r””
                                   puts $es “expect {”
                                   puts $es ” “Couldn’t canonicalise: No such file or directory” {exit 1}”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Haal de bestanden via de filter op en controleer of ophalen goed is gegaan
                                   puts $es “send “mget $filter\r””
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   puts $es “send “bye\r””
                                   puts $es “expect eof”

                                   flush $es
                                   close $es
                                   if {$debug > 1} {
                                     echo “expect script:[exec cat $esn]”
                                   }

                                   # execute expect
                                   set retVal [exec $HciRootDir/tcl/bin/expect $esn]
                                   puts “$module retVal: ”
                                   if { [info exists errorCode] } {
                                       puts “$module errorCode: ”
                                   }
                                   file delete $esn

                                   # Verwerken van eventueel zojuist opgehaalde bestanden
                                   # Bestanden moeten daarna weer verwijderd worden via SFTP; via removelist
                                   set removelist {}
                                   # Haal de bestanden op uit de localdir
                                   # Directories krijgen door de -1 (min
                        [code]######################################################################
                        # Name:      orbis_sftp_in
                        # Purpose:   Custom protocol for receiving messages via sftp
                        #            Put this proc in UPOC to read the files from sftp.
                        #            Also put this in TPS Inbound Data to pass the original
                        #            filename of the file. This is done through a global
                        #            ‘orbis_sftp_filenames’ which is a list of filenames.
                        # UPoC type:   tps
                        # Args:    tps keyedlist containing the following keys:
                        #          MODE    run mode (”start”, “run” or “time”)
                        #          MSGID   message handle
                        #          ARGS    user-supplied arguments:
                        #                  SFTPSERVER – sftp server hostname or IP
                        #                  SFTPUSER   – sftp user name
                        #                  SFTPPWD    – sftp user password
                        #                  SFTPDIR    – sftp directory where to get files; all files are taken!!
                        #                  SFTPFILTER – simpel filter (like ‘*.txt’ or ‘*log*.txt’) to get certain files.
                        #                               Default: ‘*’ (= all files in directory).
                        #                  BACKUPDIR  – directory (local, so should be available by Cloverleaf) where a copy of the files are placed
                        #                               Default: empty, so no backup is made
                        #                  LOCALDIR   – local directory where files are temporarily saved
                        #                  TMPDIR     – local tmp directory where expect script is written; should be different from LOCALDIR!
                        #                               Default: ${HCIROOTDIR}/temp
                        #                  EXPECTSN   – Expect script name (temp)
                        #                  DEBUG      – Debug information level (default = 0)
                        #
                        # Returns: tps disposition list:
                        #          
                        #

                        proc orbis_sftp_in { args } {
                           keylget args MODE mode                 ;# Fetch mode
                           keylget args ARGS uargs                ;# Get user arguments
                           keylget args CONTEXT ctx               ;# Context

                           global HciConnName HciRootDir HciSiteDir orbis_sftp_filenames

                           set debug 0 ; keylget uargs DEBUG debug

                           if { ! [info exists HciConnName] } {
                               set HciConnName “UNKNOWN_TD”
                           }
                           set procName [lindex [info level 0] 0]
                           set module “$HciConnName: $procName”

                           set dispList {}            ;# Nothing to return

                           switch -exact — $mode {
                               start {
                                   # Perform special init functions
                                   # N.B.: there may or may not be a MSGID key in args

                                   # Initialiseer de global ‘orbis_sftp_filenames’
                                   set orbis_sftp_filenames {}

                                   if {$debug > 0} {
                                     echo “Start – orbis_sftp_filenames: “
                                   }

                               }

                               run {
                                   # ‘run’ mode always has a MSGID; fetch and process it

                                   # Neem het eerste element van global ‘orbis_sftp_filenames’ als naam.
                                   # Indien deze global leeg is, dan een tijdelijke naam verzinnen.

                                   keylget args MSGID mh
                                   set filename [lvarpop orbis_sftp_filenames 0]

                                   # Indien geen originele filenaam, dan filenaam maken
                                   if {[string length $filename] <= 0} {
                                     set ctrfile “sftp_filename_in_icr”
                                     if ![file exists “${ctrfile}.ctr”] {
                                       CtrInitCounter $ctrfile file 1 100000000 1
                                     }
                                     # Get next counter value
                                     set counter [list [CtrNextValue $ctrfile file]]

                                     set filename “TEMP_FILENAME_${counter}”
                                   }

                                   set driverctl “{FILESET {{OBFILE $filename}}}”
                                   msgmetaset $mh DRIVERCTL $driverctl
                                   lappend dispList “CONTINUE $mh”

                                   if {$debug > 0} {
                                     echo “Run – filename: “
                                   }

                               }

                               time {
                                   # Timer-based processing
                                   # N.B.: there may or may not be a MSGID key in args

                                   # sftp user name
                                   keylget uargs SFTPUSER user
                                   if { ![info exists user] } {
                                     error “$module Please set up SFTPUSER parameter”
                                   }

                                   # sftp password
                                   keylget uargs SFTPPWD password
                                   if { ![info exists password] } {
                                     error “$module Please set up SFTPPWD parameter”
                                   }

                                   # sftp host name
                                   keylget uargs SFTPSERVER host
                                   if { ![info exists host] } {
                                     error “$module Please set up SFTPSERVER”
                                   }

                                   # sftp dir name
                                   keylget uargs SFTPDIR dir
                                   if { ![info exists dir] } {
                                     error “$module Please set up SFTPDIR”
                                   }

                                   # sftp filter
                                   set filter “*”
                                   keylget uargs SFTPFILTER filter

                                   # backup dir
                                   set backupdir “”
                                   keylget uargs BACKUPDIR backupdir
                                   if { ([string length $backupdir] > 0) && ![file isdirectory $backupdir] } {
                                     error “$module BACKUPDIR ${backupdir} does not exist”
                                   }

                                   # local dir name
                                   keylget uargs LOCALDIR localdir
                                   if { ![info exists localdir] || ![file isdirectory $localdir] } {
                                     error “$module Please set up LOCALDIR”
                                   }

                                   # tmp dir name
                                   set tmpdir [file join ${HciRootDir} temp]
                                   keylget uargs TMPDIR tmpdir
                                   if { ![file isdirectory $tmpdir] } {
                                     error “$module Please set up TMPDIR”
                                   }

                                   # Expect script name
                                   keylget uargs EXPECTSN expectSN
                                   if { ![info exists expectSN] } {
                                     # Bepaal uniek nummer om bestandsnaam uniek te maken
                                     set ctrfile “orbis_sftp_in_icr”
                                     if ![file exists “$ctrfile.ctr”] {
                                       CtrInitCounter $ctrfile file 1 10000 1
                                     }
                                     # Get next counter value
                                     set number [CtrNextValue $ctrfile file]
                                     set expectSN “sftp_in_tmpscript_${number}”
                                   }
                                   set esn [file join ${tmpdir} ${expectSN}]

                                   # Haal nu de bestanden via sftp uit de genoemde directory op
                                   # en zet deze in de localdir.

                                   # create expect script
                                   set es [open $esn w]

                                   # Zet de timeout op 60 seconden
                                   puts $es “set timeout 60”
                                   # Start sftp
                                   puts $es “spawn sftp $user@$host”
                                   # Wacht op wachtwoord en voer dit in
                                   puts $es “expect {”
                                   puts $es ” “password:” {send “$password\r”}”
                                   puts $es ” “Connection closed” return”
                                   puts $es “}”
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Ga lokaal naar local directory
                                   puts $es “send “lcd $localdir\r””
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Ga remote naar de sftp directory
                                   puts $es “send “cd $dir\r””
                                   puts $es “expect {”
                                   puts $es ” “Couldn’t canonicalise: No such file or directory” {exit 1}”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   # Haal de bestanden via de filter op en controleer of ophalen goed is gegaan
                                   puts $es “send “mget $filter\r””
                                   puts $es “expect {”
                                   puts $es ” timeout {exit 1}”
                                   puts $es ” “sftp>””
                                   puts $es “}”
                                   puts $es “send “bye\r””
                                   puts $es “expect eof”

                                   flush $es
                                   close $es
                                   if {$debug > 1} {
                                     echo “expect script:[exec cat $esn]”
                                   }

                                   # execute expect
                                   set retVal [exec $HciRootDir/tcl/bin/expect $esn]
                                   puts “$module retVal: “
                                   if { [info exists errorCode] } {
                                       puts “$module errorCode: “
                                   }
                                   file delete $esn

                                   # Verwerken van eventueel zojuist opgehaalde bestanden
                                   # Bestanden moeten daarna weer verwijderd worden via SFTP; via removelist
                                   set removelist {}
                                   # Haal de bestanden op uit de localdir
                                   # Directories krijgen door de -1 (min

                        Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                      • #59892
                        Yves Guerin
                        Participant

                          Dear,

                          Yes, I use sftp or something like that (may be ftps) I have to send two files to an windows sftp server

                          And more easy than Java class

                          My code with tclcurl

                          On top of my tcl file I define a global var

                          Code:


                          set gAvoirTclCurl 0

                          In my tcl proc: get reference to my global var

                          Code:


                          upvar #0 gAvoirTclCurl avoirTclCurl


                          in start state

                          Code:

                          # obtenir le pkg TclCurl
                                     if {[catch {set idPkg [package require TclCurl]} erreur]} {
                                         # err
                                         set defAction ERROR

                                         set  msgErr “$msgErr + ERREUR paquetage TclCurl = $erreur”
                                     } else {
                                         # ok
                                         puts “$module: TclCurl = $idPkg”
                                         set avoirTclCurl 1
                                     }


                          in run state:

                          Code:


                          # get parameters from the command line

                          # check if tclcurl is loaded
                          if {1 == $avoirTclCurl} {
                              # have pkg tclcurl loaded
                               if {[catch { ::curl::transfer -verbose $verbose
                                                                      -noprogress $noprogress
                                                                      -url $url$nomF
                                                                      -userpwd “$userpwd”
                                                                      -sslverifypeer $sslverifypeer
                                                                      -ftpssl $ftpssl
                                                                      -sslverifyhost $sslverifyhost
                                                                      -infile $fichier
                                                                      -upload $upload
                                                                      -errorbuffer erreur
                                                                      -postquote [list “rnfr $nomF” “rnto $nomFStrm$ext”]
                                            } erreur]} {
                                                             # error handler
                                                            } else {
                                                                 # sftp transfert is ok
                                                            }
                          } else {
                                 # error handler for not having tclcurl loaded
                          }

                          hope that help

                          Regards,

                          Yves

                        • #59893
                          Yves Guerin
                          Participant

                            Dear,

                            Nice scripts with expect but using tclcurl seems lineless  ðŸ˜‰

                            Regards,

                            Yes

                          • #59894
                            Vijay Dunnala
                            Participant

                              I was able to the use the SFTP script but I have an issue connecting to the Server but when i tried to do it using File Zilla I was able to connect but not through the script.

                              [pd  :pdtd:ERR /0:Test_Luminant:02/28/2017 13:58:52] Tcl error:

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]    msgId   = none

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]    proc    = ‘SFTP_Script’

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]    args    = ‘{SFTPUSER mummy} {SFTPPWD 1l0v10myj0b} {SFTPSERVER 198.61.172.16} {SFTPDIR /} {LOCALDIR

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] /cloverleaf/cis6.1/integrator/epic_mdm_test/data/Test_MDM}

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] ‘

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]    result  = ‘spawn sftp ctdummy@198.61.172.16

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] Connecting to 198.61.172.16…

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] The authenticity of host ‘198.61.172.16 (198.61.172.16)’ can’t be established.

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] RSA key fingerprint is f9:22:5c:a7:d9:31:c1:a2:af:dc:32:6b:92:c8:0a:9b.

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] Are you sure you want to continue connecting (yes/no)? child process exited abnormally’

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]    errorInfo: ‘

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] spawn sftp ctdummy@198.61.172.16

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] Connecting to 198.61.172.16…

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] The authenticity of host ‘198.61.172.16 (198.61.172.16)’ can’t be established.

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] RSA key fingerprint is f9:22:5c:a7:d9:31:c1:a2:af:dc:32:6b:92:c8:0a:9b.

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] Are you sure you want to continue connecting (yes/no)? child process exited abnormally

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]     while executing

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] “exec $HciRootDir/tcl/bin/expect $esn”

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]     (procedure “SFTP_Script” line 178)

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–]     invoked from within

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] “SFTP_Script {CONTEXT pdupoc_read} {ARGS {{SFTPUSER mummy} {SFTPPWD 1l0v10myj0b} {SFTPSERVER 198.61.172.16} {SFTPDIR /} {LOCALDIR

                              [pd  :pdtd:ERR /0:Test_Luminant:–/–/—- –:–:–] /cloverleaf/cis6.1/i…”‘

                            • #59895
                              Peter Heggie
                              Participant

                                I’m not sure I have identified the issue, but I think the key is the message about the fingerprint. You can configure the command with additional arguments to automatically add the remote host’s fingerprint to the ‘known_hosts’ file, but that is a dangerous and insecure way of scripting connections.

                                So you should connect to the remote host manually at the command line first, at least once. When you receive the message about the fingerprint, you should reply ‘yes’. This will add the remote host’s fingerprint to the known-hosts file.

                                Then at this point also I recommend you manually test CD’ing to the directory required, to make sure you have access to it. And then also recommend you get or put the file(s) you expect to process. This will verify that you have the required security. If you intend the script to delete behind, you should test this also.

                                Once these things are done manually at least once, then you can run your script automatically and you should not be getting the fingerprint prompt.

                                If you then still get some kind of error, then kindly please use optional argument -vvv to get verbose debugging output and review it or share it here.

                                Peter Heggie

                              • #59896
                                Robert Kersemakers
                                Participant

                                  Yes, what Peter said!

                                  And be aware that whenever the other party changes RSA keys, then you will need to manually logon once again. So the other party needs to inform you beforehand when changing RSA keys.

                                  Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                              Viewing 14 reply threads
                              • The forum ‘General’ is closed to new topics and replies.