LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   AIX (https://www.linuxquestions.org/questions/aix-43/)
-   -   expect : creating a folder with a timestamp in a send command (https://www.linuxquestions.org/questions/aix-43/expect-creating-a-folder-with-a-timestamp-in-a-send-command-4175475033/)

gbisschops 08-28-2013 10:08 AM

expect : creating a folder with a timestamp in a send command
 
Hello,

I'm working on an AIX 5.3 mainframe. I have to automatically send files with sftp.

I choose the expect way, here is my code :

Code:

#!/usr/bin/expect -d -f
#spawn /usr/bin/sftp -b sftpbatch user@host
#set mydate `date +%Y%m%d%H%M%S`
cd /imxapp4/impr_files/pcltosend
spawn /usr/bin/sftp user@host
match_max 100000
expect "*ass*"
send -- "mypassword\n"
expect "*Connected*"
send -- "lcd /test\n"
sleep 1
send -- "mkdir date +%Y%m%d%H%M%S\n"
sleep 1
send -- "PUT *\n"
expect eof


output result for the mkdir part :
send: sending "mkdir date +%Y%m%d%H%M%S\n" to { exp4 }

This is simply creating a "date" folder :(

You can see my first attempt :

Code:

#set my_command `date +%Y%m%d%H%M%S`
...
send -- "mkdir mydate\n"

output :
...
executing commands from command file ./test_expect.sh
wrong # args: should be "set varName ?newValue?"
while executing
"set mydate `date +%Y%m%d%H%M%S`"
(file "./test_expect.sh" line 3)

Do you have an idea how I can do this ? With another shell script without the expect stuff it is working as expected ?

Thanks in advance,

Greg.

NevemTeve 08-29-2013 03:33 AM

Something like this?
Code:

#!/usr/local/bin/bash

MYDATE=$(date +%Y%m%d%H%M%S)

expect -f - $MYDATE <<"DONE"
set mydate [lrange $argv 0 0]
spawn /usr/bin/sftp user@localhost
match_max 100000
expect "*ass*"
send -- "passvoid\n"
expect "*Connected*"
send -- "cd /tmp\n"
sleep 1
send -- "mkdir date-$mydate\n"
sleep 1
send -- "QUIT\n"
expect eof
DONE


gbisschops 08-29-2013 04:04 AM

Hi,

Thanks for your help.

I don't have bash shell installed I think :

Current available shells:
/bin/sh
/bin/bsh
/bin/csh
/bin/ksh
/bin/tsh
/bin/ksh93
/usr/bin/sh
/usr/bin/bsh
/usr/bin/csh
/usr/bin/ksh
/usr/bin/tsh
/usr/bin/ksh93
/usr/bin/rksh
/usr/bin/rksh93
/usr/sbin/uucp/uucico
/usr/sbin/sliplogin
/usr/sbin/snappd

I replaced the first line with #!/usr/bin/bsh
Then I have this :

./sftp_send.sh: syntax error at line 3: `MYDATE=$' unexpected

?

NevemTeve 08-29-2013 04:40 AM

It should work with the default AIX shell (/bin/sh)

gbisschops 08-29-2013 05:06 AM

Hi thanks you a lot this made my day :)

Just one more thing, do you know how I can log the expect stuff in a file ?
I need a logfile to proof I've uploaded the files :)

NevemTeve 08-29-2013 05:22 AM

With redirection?

Code:

#!/bin/sh

MYDATE=$(date +%Y%m%d%H%M%S)

exec >/tmp/ftp.$MYDATE.log 2>&1

expect -f - $MYDATE <<"DONE"
...

then later:
Code:

$ cat /tmp/ftp.20130829122051.log
spawn /usr/bin/sftp user@localhost
user@localhost's password:
Connected to localhost.
cd /tmp
sftp> cd /tmp
sftp> mkdir date-20130829122051
sftp> QUIT


gbisschops 08-29-2013 06:58 AM

Thank you again it is perfect :)


All times are GMT -5. The time now is 02:24 AM.