在shell脚本中使用expect命令获取文件

时间:2014-07-14 20:32:28

标签: linux shell unix expect

我试图在expect命令的帮助下使用UNIX shell expect脚本自动执行SFTP命令。我的代码如下。当我尝试执行脚本时,它会抛出异常。如果您遇到类似的问题,有人可以帮助我。任何帮助,将不胜感激。谢谢。

--Shell Script

  #This ftp script will copy all the files from Buxton directory into the local directory.

#!/usr/local/bin/expect
spawn sftp -b cmdFile XYZ@sftp.abc.com
expect "password:"
send "6n8W6u0J"
interact

Command File

SRC_DIR='From_Bux'
TRGT_DIR='/work/informatica/release81x/DEV2/DEP_EXT_ARCH/SrcFiles/LANDING'
FILE='buxt_summary_20140702.csv'
cd $SRC_DIR
lcd $TRGT_DIR
get $FILE
Execution command ./get_bxtn_src_files.ksh

Error Message

$ ./get_bxtn_src_files.ksh
./get_bxtn_src_files.ksh[3]: spawn:  not found
couldn't read file "password:": no such file or directory
send: unable to stat draft file /home/vvutukuri/Mail/6n8W6u0J: No such file or directory
./get_bxtn_src_files.ksh[6]: interact:  not found

1 个答案:

答案 0 :(得分:1)

这看起来像是一个翻译问题。

再次检查脚本是否有#!作为脚本中绝对的第一个字符。

head -1 get_bxtn_src_files.ksh | cut -c 1-2
# The result should be: #!

如果没有,脚本将使用您登录的shell进行解释。

您收到的错误表明expect命令由常规shell(ksh / sh / bash / ...)而不是expect解释。

请参阅以下示例:

$ ksh
$ cd test
$ cat expect1-test.exp   # the hash-bang is in my first line
#!/usr/bin/expect
spawn sftp -b cmdFile XYZ@sftp.abc.com
expect "password:"
send "6n8W6u0J"
interact
$ ./expect1-test.exp    # Notice the error messages, they come from Expect
spawn sftp -b cmdFile XYZ@sftp.abc.com
No such file or directory (cmdFile).
send: spawn id exp4 not open
    while executing
"send "6n8W6u0J""
    (file "./expect1-test.exp" line 4)
$ cat expect2-test.exp   # Notice the blank line

#!/usr/bin/expect
spawn sftp -b cmdFile XYZ@sftp.abc.com
expect "password:"
send "6n8W6u0J"
interact
$ ./expect2-test.exp   # Notice that i.e. spawn is not found.
./expect2-test.exp[3]: spawn: not found [No such file or directory]
couldn't read file "password:": no such file or directory
./expect2-test.exp[5]: send: not found [No such file or directory]
./expect2-test.exp[6]: interact: not found [No such file or directory]

您还应该查看autoexpect,您可以使用它来创建可以使用和编辑的期望脚本。

最后:您应该考虑切换到使用ssh私钥/公钥而不是期望,除非 - 当然 - 您无法执行此操作/需要使用密码。

SSH私钥/公钥

在远程计算机上

mkdir -p $HOME/.ssh/
chmod 700 $HOME/.ssh/
chmod go-w $HOME     # NB! If sshd is set to strict your $HOME - and parent directories must _not_ be world writable

在本地计算机上

mkdir -p $HOME/.ssh # Create the .ssh directory if necessary
chmod 700 $HOME/.ssh  # Low perms to protect the directory
ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -N ""   # Create the private/pulic key pair
                                               # with no pw since you'll use it in a script
scp $HOME/.ssh/id_dsa.pub remote_machine:.ssh/authorized_keys  # Provided it's not allready there, in which case you should add your public key to the bottom of the file

验证您无需密码即可登录:

ssh remote_machine  # or user@remote_machine

您的脚本现在可以是:

#!/bin/ksh
# Or /usr/bin/ksh..?
sftp -b cmdFileXYZ@sftp.abc.com

在命令行上,您也可以使用

scp XYZ@sftp.abc.com:$SRC_DIR/$FILE $TRGT_DIR/