比如scp文件从另外的主机上来的时候,需要输入密码,怎么写道shell里面自动输入密码
#!/usr/bin/expect
set timeout 10
spawn ssh god@god.bread.so
expect {
"*yes/no*" {
send "yes\n";
exp_continue;
}
"*password:*" {
send "your password\r";
exp_continue;
}
"*bash*" {
send "ssh god@remote.bread.so\r";
exp_continue;
}
"Password:" {
send "remote server password\r";
}
}
interact
请参考前几天我写的一个 通过跳板机自动登录服务器脚本
excpect
是用于提供自动交互的工具。比如你要用ssh或者 telnet登录服务器的时候,命令行下提示需要输入用户和密码等
项目主页: http://expect.sourceforge.net
这里有详细介绍和用法:http://www.pythonclub.org/linux/expect
最好不要用密码。可以用密钥。参考这个链接 http://blog.sina.cn/dpool/blog/s/blog_7ae2408501018xsj.html
Expect is the answer.
用expect是可以的,之前我写过一篇博客,简单介绍了下,希望对你有所帮助
http://quxiao.github.io/blog/2012/05/13/add-startup-programe-with-expect/
用key(字数补丁)
ssh指令似乎不能显式指定密码
如果要自动化脚本,不能输入密码,只能用信任关系来解决
胆大可以用sshpass
套scp,或者putty-tools
中的pscp,都支持明文passwd。
但是胆小点对大家都好不是吗?特别是生产环境下。。。无关紧要的环境那无所谓。