我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。
假设我需要使用密码a1234b SSH到myname@somehost。
我已经试过了……
#~/bin/myssh.sh
ssh myname@somehost
a1234b
...但这并不奏效。
如何将此功能放入脚本中?
我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。
假设我需要使用密码a1234b SSH到myname@somehost。
我已经试过了……
#~/bin/myssh.sh
ssh myname@somehost
a1234b
...但这并不奏效。
如何将此功能放入脚本中?
当前回答
我得到了这个工作如下
.ssh/config被修改以消除是/否提示-我在防火墙后面,所以我不担心欺骗ssh密钥
host *
StrictHostKeyChecking no
为expect创建一个响应文件,即answer.expect
set timeout 20
set node [lindex $argv 0]
spawn ssh root@node service hadoop-hdfs-datanode restart
expect "*?assword {
send "password\r" <- your password here.
interact
创建bash脚本并在文件中调用expect
#!/bin/bash
i=1
while [$i -lt 129] # a few nodes here
expect answer.expect hadoopslave$i
i=[$i + 1]
sleep 5
done
获取128个用新配置刷新的hadoop datanode -假设您正在为hadoop/conf文件使用NFS挂载
希望这能帮助到一些人——我是一个Windows傻瓜,这花了我大约5个小时才弄明白!
其他回答
在寻找这个问题的答案几个月后,我终于找到了一个更好的解决方案:写一个简单的脚本。
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "password:"
send "$password\r";
interact
把它放在/usr/bin/exp,所以你可以使用:
Exp <password> SSH <任何> Exp <password> SCP <anysrc> <anydst>
完成了!
在ubuntu linux -
ssh username@server_ip_address -p port_number
按enter键,然后输入服务器密码
如果您不是root用户,请在命令的开头添加sudo
使用公钥认证:https://help.ubuntu.com/community/SSH/OpenSSH/Keys
在源主机中只运行一次:
ssh-keygen -t rsa # ENTER to every field
ssh-copy-id myname@somehost
这就是全部,之后你就可以不用密码使用ssh了。
在脚本中使用脚本tossh,第一个参数是主机名,第二个参数是密码。
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$pass\n";
interact"
我得到了这个工作如下
.ssh/config被修改以消除是/否提示-我在防火墙后面,所以我不担心欺骗ssh密钥
host *
StrictHostKeyChecking no
为expect创建一个响应文件,即answer.expect
set timeout 20
set node [lindex $argv 0]
spawn ssh root@node service hadoop-hdfs-datanode restart
expect "*?assword {
send "password\r" <- your password here.
interact
创建bash脚本并在文件中调用expect
#!/bin/bash
i=1
while [$i -lt 129] # a few nodes here
expect answer.expect hadoopslave$i
i=[$i + 1]
sleep 5
done
获取128个用新配置刷新的hadoop datanode -假设您正在为hadoop/conf文件使用NFS挂载
希望这能帮助到一些人——我是一个Windows傻瓜,这花了我大约5个小时才弄明白!