用一个域名免费做网站,网站建设过程与思路,宁德app开发,百万级别wordpressscp 功能很强大#xff0c;但需要人工输入 password, 当然可以通过把 公钥保存在远程主机的 ~/.ssh 目录中#xff0c;而后就不用输入password#xff0c;但这需要配置.
用 sshpass 可能在命令输入 password, 但 需要用 “sudo apt-get install sshpass” 安装
如果不想用…scp 功能很强大但需要人工输入 password, 当然可以通过把 公钥保存在远程主机的 ~/.ssh 目录中而后就不用输入password但这需要配置.
用 sshpass 可能在命令输入 password, 但 需要用 “sudo apt-get install sshpass” 安装
如果不想用上面两种方法可以用 expect 编写脚本可以帮助我们自动交互
虽然 python 也提供 pexpect 模块但既然 expect 很简单为何不直接用 os.system() 去执行呢
下面是我编写的类实现了远程复制 [html] view plaincopyprint? class RemoteShell: def __init__(self, host, user, pwd): self.host host self.user user self.pwd pwd def put(self, local_path, remote_path): scp_put spawn scp %s %s%s:%s expect (yes/no)? { send yes\r expect password: send %s\r } password: {send %s\r} expect eof exit os.system(echo %s scp_put.cmd % (scp_put % (os.path.expanduser(local_path), self.user, self.host, remote_path, self.pwd, self.pwd))) os.system(expect scp_put.cmd) os.system(rm scp_put.cmd) class RemoteShell:def __init__(self, host, user, pwd):self.host hostself.user userself.pwd pwddef put(self, local_path, remote_path):
scp_put
spawn scp %s %s%s:%s
expect (yes/no)? {
send yes\r
expect password:
send %s\r
} password: {send %s\r}
expect eof
exitos.system(echo %s scp_put.cmd % (scp_put % (os.path.expanduser(local_path), self.user, self.host, remote_path, self.pwd, self.pwd)))os.system(expect scp_put.cmd)os.system(rm scp_put.cmd)但发现每次文件都没有复制完我想看expect 究竟做了什么事情还好 expect 提供 -d 参数给出更多的信息。 最后发现是被复制文件太大expect 超时退出了
在脚本前加入 “set timeout -1 就OK了 [html] view plaincopyprint? scp_put set timeout -1 spawn scp %s %s%s:%s expect (yes/no)? { send yes\r expect password: send %s\r } password: {send %s\r} expect eof exit scp_put
set timeout -1
spawn scp %s %s%s:%s
expect (yes/no)? {
send yes\r
expect password:
send %s\r
} password: {send %s\r}
expect eof
exit总结 1 expect 每一条语句都是顺序执行 [html] view plaincopyprint? /pre 因为scp 可能先返回 (yes/no)? 再 返回 password:, 也可能直接返回password:, 考虑顺序关系上面语句的层次关系其实如下 div classdp-highlighter bg_htmldiv classbardiv classtoolsstrong[html]/strong a target_blank titleview plain classViewSource hrefhttp://blog.csdn.net/span76/article/details/11575231#view plain/aa target_blank titlecopy classCopyToClipboard hrefhttp://blog.csdn.net/span76/article/details/11575231#copy/aa target_blank titleprint classPrintSource hrefhttp://blog.csdn.net/span76/article/details/11575231#print/aa target_blank title? classAbout hrefhttp://blog.csdn.net/span76/article/details/11575231#?/a/div/divol classdp-xmlli classaltspanspanexpect (yes/no)? { send yes\r /span/span/lilispan expect password: /span/lili classaltspan send %s\r /span/lilispan } /span/lili classaltspan password: {send %s\r} /span/li/ol/divpre classhtml styledisplay: none; namecodeexpect (yes/no)? { send yes\rexpect password:send %s\r} password: {send %s\r}2) 每当 spawn 的程序有输出的时候的expect都会去匹配 如果匹配不上就等下次有输出再次执行当前的 expect, 直到超时 我用 expect -d 去追踪得到的结论当然可以设置没有超时 set timeout -1 3) 如果 expect 退出 被它 spawn 的程序会被 kill 掉 4 spawn 结束的时候它向标准输出的的 eof 会被 expect 检测到正好作为 expect 脚本退出的时机。
对于 scp 可以先检测 100%因为 scp 会输出复制进度再检测 eof [html] view plaincopyprint? expect 100%% expect eof expect 100%%
expect eof5 expect 是部分匹配所以不要担心自己不知道程序的完整输出 版权声明本文为博主原创文章未经博主允许不得转载。