shell里面输入密码

比如scp文件从另外的主机上来的时候,需要输入密码,怎么写道shell里面自动输入密码

阅读 15.9k
8 个回答
#!/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

请参考前几天我写的一个 通过跳板机自动登录服务器脚本

ssh指令似乎不能显式指定密码
如果要自动化脚本,不能输入密码,只能用信任关系来解决

用key(字数补丁)

胆大可以用sshpass套scp,或者putty-tools中的pscp,都支持明文passwd。

但是胆小点对大家都好不是吗?特别是生产环境下。。。无关紧要的环境那无所谓。

Expect is the answer.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进