我需要创建一个自动向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
...但这并不奏效。
如何将此功能放入脚本中?
当前回答
这基本上是abbotto的答案的扩展,有一些额外的步骤(针对初学者),使启动你的服务器,从你的linux主机,非常容易:
编写一个简单的bash脚本,例如:
#!/bin/bash
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no <YOUR_USERNAME>@<SEVER_IP>
保存文件,例如'startMyServer',然后在你的终端上运行这个命令使文件可执行:
sudo chmod +x startMyServer
将文件移动到“PATH”变量中的文件夹(在终端中运行“echo $PATH”查看这些文件夹)。例如,将它移动到'/usr/bin/'。
瞧,现在你可以通过在终端中输入'startMyServer'进入服务器。
P.S.(1)这不是很安全,请查看ssh密钥以获得更好的安全性。
附注(2)SMshrimant的答案很相似,对一些人来说可能更优雅。但我个人更喜欢使用bash脚本。
其他回答
不同的我
sshpass -p PASSWORD ssh USER@SERVER
后缀II
#!/usr/bin/expect -f
spawn ssh USERNAME@SERVER "touch /home/user/ssh_example"
expect "assword:"
send "PASSWORD\r"
interact
在脚本中使用脚本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个小时才弄明白!
# create a file that echo's out your password .. you may need to get crazy with escape chars or for extra credit put ASCII in your password...
echo "echo YerPasswordhere" > /tmp/1
chmod 777 /tmp/1
# sets some vars for ssh to play nice with something to do with GUI but here we are using it to pass creds.
export SSH_ASKPASS="/tmp/1"
export DISPLAY=YOURDOINGITWRONG
setsid ssh root@owned.com -p 22
参考:https://www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?trk=mp-reader-card
通过shell脚本连接远程机器,使用以下命令:
sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS
其中IPADDRESS, USERNAME和PASSWORD是需要在脚本中提供的输入值,或者如果我们想在运行时提供,使用“read”命令。