我需要创建一个自动向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
...但这并不奏效。
如何将此功能放入脚本中?
当前回答
您可以使用expect脚本。我没有写一个在相当长的一段时间,但它应该如下所示。您需要在脚本前面加上#!/usr/bin/expect
#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:"
send "username\r"
expect "Password:"
send "password\r"
interact
其他回答
我想我没有看到任何人提出这个建议,而操作人员只是说“脚本”,所以……
我需要解决同样的问题,而我最舒服的语言是Python。
我用了paramiko图书馆。此外,我还需要使用sudo发布需要升级权限的命令。事实证明,sudo可以通过“-S”标志通过stdin接受其密码!见下文:
import paramiko
ssh_client = paramiko.SSHClient()
# To avoid an "unknown hosts" error. Solve this differently if you must...
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# This mechanism uses a private key.
pkey = paramiko.RSAKey.from_private_key_file(PKEY_PATH)
# This mechanism uses a password.
# Get it from cli args or a file or hard code it, whatever works best for you
password = "password"
ssh_client.connect(hostname="my.host.name.com",
username="username",
# Uncomment one of the following...
# password=password
# pkey=pkey
)
# do something restricted
# If you don't need escalated permissions, omit everything before "mkdir"
command = "echo {} | sudo -S mkdir /var/log/test_dir 2>/dev/null".format(password)
# In order to inspect the exit code
# you need go under paramiko's hood a bit
# rather than just using "ssh_client.exec_command()"
chan = ssh_client.get_transport().open_session()
chan.exec_command(command)
exit_status = chan.recv_exit_status()
if exit_status != 0:
stderr = chan.recv_stderr(5000)
# Note that sudo's "-S" flag will send the password prompt to stderr
# so you will see that string here too, as well as the actual error.
# It was because of this behavior that we needed access to the exit code
# to assert success.
logger.error("Uh oh")
logger.error(stderr)
else:
logger.info("Successful!")
希望这能帮助到一些人。我的用例是在大约300台服务器上创建目录、发送和解压文件以及启动程序。因此,自动化至关重要。我试过sshpass, expect,然后想到了这个。
这是我登录服务器的方式:
ssp <server_ip>
别名ssp = ' / home / myuser /文件/ ssh_script.sh” 猫/home/myuser/Documents/ssh_script.sh
ssp:
#!/bin/bash
sshpass -p mypassword ssh root@$1
因此:
ssp server_ip
这基本上是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。
Ubuntu/Debian: apt-get安装sshpass Fedora/CentOS: yum安装sshpass Arch:吃豆人-S sshpass
例子:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM
自定义端口示例:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400
注:
当传递-f标志时,Sshpass还可以从文件中读取密码。 使用-f可以防止在执行ps命令时密码显示。 存储密码的文件应具有安全权限。
在脚本中使用脚本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"