为了写sftp 服务器间文件shell 脚本 expect报错: spawn: command not found

 

为了写sftp 服务器间文件shell 脚本 expect报错: spawn: command not found

通过Google查找资料,我的expect安装成功的,which expect 查看一下安装路径,发现安装正常无问题。

然后./excute.sh 执行shell 脚本依然报如下错误。

line 3: spawn: command not found
couldn't read file "password:": no such file or directory
line 5: send: command not found
line 6: interact: command not found

大家不要慌,csdn上基本都不行,还是要靠自己去Google。

最后一行命令 expect -f ./excute.sh

直接解决问题。这里附我参考的地址,

https://www.e-learn.cn/content/wangluowenzhang/752290

It all depends on how you invoke the command. Like ray said, even if you specify the environment with a bang at the top, you still have to run it using expect -f.

好的,以下是一个使用expectsftp自动上传文件shell脚本示例: ```bash #!/usr/bin/expect # sftp服务器信息 set sftp_user "username" set sftp_host "hostname" set sftp_port "port" set sftp_key "path/to/private/key" # 本地文件路径和文件名 set local_file "path/to/local/file" set remote_dir "path/to/remote/directory" set remote_filename "filename" # 连接sftp服务器并上传文件 spawn sftp -o Port=$sftp_port -i $sftp_key $sftp_user@$sftp_host expect "sftp>" send "cd $remote_dir\r" expect "sftp>" send "put $local_file $remote_filename\r" expect "sftp>" send "exit\r" # 输出上传完成的提示信息 puts "文件上传完成" ``` 解释一下脚本的各个部分: - sftp服务器信息、本地文件路径和文件名:与之前的脚本相同,需要填sftp服务器的用户名、主机名、端口号和私钥文件路径,以及要上传的本地文件的完整路径和文件名,以及要上传到远程服务器的目录和文件名。 - 连接sftp服务器并上传文件:使用spawn命令在expect环境下启动sftp命令,然后使用expect命令等待sftp命令输出"sftp>"提示符,接着使用send命令向sftp命令发送cd和put命令,并等待输出"sftp>"提示符。最后发送exit命令退出sftp命令。 - 输出上传完成的提示信息:使用puts命令输出上传完成的提示信息。 这个脚本使用了expect命令来模拟交互式的命令行环境,可以方便地处理sftp命令的输入和输出。如果要上传多个文件或者目录,可以在脚本中添加循环和条件语句来实现。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值