我使用expect通过sftp连接到远程系统。 我想将my.csv文件的最后修改日期变为expect脚本
中的变量以下是我的代码:
spawn sftp -o Port=$port $user@$host
expect "Password:"
send "$password\r";
expect "sftp> "
set file_time [exec ls -la my.csv| awk {{ print $6$7$8}}]
send "echo $file_time\r";
expect "sftp> "
send "quit\r";
expect eof
答案 0 :(得分:2)
好的,我更加努力地看待这个。我看到一些错误:
所以你需要做的是将ls命令发送到sftp服务器,然后使用expect和正则表达式从返回的输出中抓取时间戳。
可能有更优雅的方法来修饰这只猫,但假设几个月的英语,并且你的文件名总是相同的(my.csv),这是该脚本的一个工作变体(在TCL 8.4.13上): / p>
spawn sftp user@host
expect "assword:"
send "mypassword\r";
expect "sftp>"
send "ls -la my.csv\r"
expect -re "\[-rwx\]{10}.*((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*)my.csv"
set timestamp $expect_out(1,string)
expect "sftp>"
send "quit\r";
expect eof
puts "Timestamp is: $timestamp"
我会留下错误控制,处理超时和所有好东西给你。 : - )
如果在您的环境中有效,请告诉我。