+ -
当前位置:首页 → 问答吧 → 求助关于SSH的问题。。。。

求助关于SSH的问题。。。。

时间:2011-07-08

来源:互联网


两台没有建立信任关系的服务器,如果用ssh服务去连接
ssh root@${host_ip}

会需要输入密码:
Password:

查看了一下SSH的用法:
usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
  [-D port] [-e escape_char] [-F configfile]
  [-i identity_file] [-L [bind_address:]port:host:hostport]
  [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
  [-R [bind_address:]port:host:hostport] [-S ctl_path]
  [user@]hostname [command]

这其中并没有传密码的参数,有没有高手能指导一下,SSH能否输入密码?如果可以改怎么传参?
多谢!

另外,能否修改这个脚本向终端返回参数?比如获取对端服务器的主机名。

Perl code

#!/usr/bin/expect -f

set serverip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set action [lindex $argv 3]
set timeout 60

spawn ssh $user@$serverip

expect {
        -nocase -re "(yes/no)" {
            send -- "yes\r" 
            expect {
                -nocase -re "Password:" {
                    send -- "$password\r"                        
                }
            }
        }
        -nocase -re "Password:" {
            send -- "$password\r"                        
        }
        expect timeout {
                exit 2
        }
}

expect -nocase -re "Last login:"

send -- "$action ;echo `whoami` $?\r"

expect {
        timeout {
                exit 2
        }
        -nocase -re "$user 0" {
                exit 0
        }
        eof {}
}

send -- "exit\r\n"
expect {
        -nocase -re "closed." {
                exit 0
        }
        timeout {
                exit 5
        }
        eof {}
}
            
            
exit 0



作者: l394255032   发布时间: 2011-07-08

你的脚本就能实现自动登录的功能了
通过参数传递你要执行的动作就可以了吧
your.sh ip uu pp hostname

作者: justkk   发布时间: 2011-07-09