use*_*855 173 linux ssh shell openssh
我需要创建一个自动输入密码给OpenSSH ssh
客户端的脚本.
假设我需要myname@somehost
使用密码进入SSH a1234b
.
我已经尝试过......
#~/bin/myssh.sh
ssh myname@somehost
a1234b
Run Code Online (Sandbox Code Playgroud)
......但这不起作用.
如何将此功能添加到脚本中?
abb*_*tto 252
首先,您需要安装sshpass.
apt-get install sshpass
yum install sshpass
pacman -S sshpass
例:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM
Run Code Online (Sandbox Code Playgroud)
自定义端口示例:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400
Run Code Online (Sandbox Code Playgroud)
笔记:
sshpass
也可以在-f
传递标志时从文件中读取密码.
-f
如果ps
执行命令,则使用防止密码可见.dam*_*n_c 82
在寻找问题答案数月后,我终于找到了一个更好的解决方案:编写一个简单的脚本.
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "assword:"
send "$password\r";
interact
Run Code Online (Sandbox Code Playgroud)
把它/usr/bin/exp
,然后你可以使用:
exp <password> ssh <anything>
exp <password> scp <anysrc> <anydst>
完成!
Die*_*sen 65
使用公钥认证:https://help.ubuntu.com/community/SSH/OpenSSH/Keys
在源主机中只运行一次:
ssh-keygen -t rsa # ENTER to every field
ssh-copy-id myname@somehost
Run Code Online (Sandbox Code Playgroud)
这就是全部,之后你就可以在没有密码的情况下做ssh了.
Lip*_*ngo 28
您可以使用expect脚本.我在相当长的一段时间内没有写过,但它应该如下所示.您将需要使用脚本#!/usr/bin/expect
#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:"
send "username\r"
expect "Password:"
send "password\r"
interact
Run Code Online (Sandbox Code Playgroud)
Rem*_*lex 21
变种我
sshpass -p PASSWORD ssh USER@SERVER
Run Code Online (Sandbox Code Playgroud)
变体II
#!/usr/bin/expect -f
spawn ssh USERNAME@SERVER "touch /home/user/ssh_example"
expect "assword:"
send "PASSWORD\r"
interact
Run Code Online (Sandbox Code Playgroud)
已经提到的一个很好的奖励sshpass
是你可以使用它autossh
,消除更多的交互式低效率.
sshpass -p mypassword autossh -M0 -t myusername@myserver.mydomain.com
Run Code Online (Sandbox Code Playgroud)
这将允许自动重新连接,例如,关闭笔记本电脑会中断您的wifi.
sshpass
更好的安全性我偶然发现了这个线程,同时寻找一种方法来进入陷入困境的服务器 - 它花了一分多钟来处理SSH连接尝试,并在我输入密码之前超时.在这种情况下,我希望能够在提示可用时立即提供我的密码.
(如果不是很清楚:在这种状态下服务器,设置公钥登录为时已晚.)
sshpass
救援.但是,有更好的方法来解决这个问题sshpass -p
.
我的实现直接跳转到交互式密码提示(没有时间浪费,看看是否可以发生公钥交换),并且永远不会将密码显示为纯文本.
#!/bin/sh
# preempt-ssh.sh
# usage: same arguments that you'd pass to ssh normally
echo "You're going to run (with our additions) ssh $@"
# Read password interactively and save it to the environment
read -s -p "Password to use: " SSHPASS
export SSHPASS
# have sshpass load the password from the environment, and skip public key auth
# all other args come directly from the input
sshpass -e ssh -o PreferredAuthentications=keyboard-interactive -o PubkeyAuthentication=no "$@"
# clear the exported variable containing the password
unset SSHPASS
Run Code Online (Sandbox Code Playgroud)
这就是我登录服务器的方式。
ssp <server_ip>
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
sshpass -p mypassword ssh root@$1
因此...
ssp server_ip
Run Code Online (Sandbox Code Playgroud)
小智 6
# 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
Run Code Online (Sandbox Code Playgroud)
参考:https://www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?strk = mp-reader-card
我认为我没有看到有人建议这样做,OP只是说了“脚本”,所以...
我需要解决相同的问题,而我最舒适的语言是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!")
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助。我的用例是创建目录,发送和解压缩文件以及一次在300台服务器上启动程序。因此,自动化至关重要。我尝试了sshpass
,expect
然后提出了这个建议。
我正在使用下面的解决方案,但为此您必须安装sshpass
如果尚未安装,请使用安装它sudo apt install sshpass
现在你可以这样做,
sshpass -p *YourPassword* ssh root@IP
您还可以创建 bash 别名,这样您就不必一次又一次地运行整个命令。请按照以下步骤操作
cd ~
sudo nano .bash_profile
在文件末尾添加以下代码
mymachine() { sshpass -p *YourPassword* ssh root@IP }
source .bash_profile
现在只需从终端运行mymachine
命令,您将在没有密码提示的情况下进入您的机器。
笔记:
mymachine
可以是您选择的任何命令。这基本上是 abbotto 答案的扩展,带有一些额外的步骤(针对初学者),使从 Linux 主机启动服务器变得非常容易:
- 编写一个简单的bash脚本,例如:
#!/bin/bash
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no <YOUR_USERNAME>@<SEVER_IP>
Run Code Online (Sandbox Code Playgroud)
- 保存文件,例如“startMyServer”,然后通过在终端中运行以下命令使文件可执行:
sudo chmod +x startMyServer
Run Code Online (Sandbox Code Playgroud)
- 将文件移动到“PATH”变量中的文件夹(在终端中运行“echo $PATH”以查看这些文件夹)。例如,将其移动到“/usr/bin/”。
瞧,现在您可以通过在终端中输入“startMyServer”来进入您的服务器。
PS (1) 这不是很安全,请查看 ssh 密钥以获得更好的安全性。
PS (2) SMshrimant 答案非常相似,对某些人来说可能更优雅。但我个人更喜欢使用 bash 脚本。