Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用脚本自动输入SSH密码_Linux_Shell_Ssh_Openssh - Fatal编程技术网

Linux 使用脚本自动输入SSH密码

Linux 使用脚本自动输入SSH密码,linux,shell,ssh,openssh,Linux,Shell,Ssh,Openssh,我需要创建一个脚本,自动向OpenSSHsshclient输入密码 假设我需要SSH到myname@somehost使用密码a1234b 我已经试过了 #~/bin/myssh.sh ssh myname@somehost a1234b …但这不起作用 如何将此功能写入脚本?使用公钥身份验证: 在源主机中,仅运行一次: ssh-keygen -t rsa # ENTER to every field ssh-copy-id myname@somehost 所有这些,在那之后,您就可以不用密码

我需要创建一个脚本,自动向OpenSSH
ssh
client输入密码

假设我需要SSH到
myname@somehost
使用密码
a1234b

我已经试过了

#~/bin/myssh.sh
ssh myname@somehost
a1234b
…但这不起作用


如何将此功能写入脚本?

使用公钥身份验证:

在源主机中,仅运行一次:

ssh-keygen -t rsa # ENTER to every field
ssh-copy-id myname@somehost

所有这些,在那之后,您就可以不用密码进行ssh了。

您可以使用一个脚本。我已经有一段时间没有写过了,但它应该如下所示。您需要使用
#作为脚本的开头/usr/bin/expect

#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:" 
send "username\r"
expect "Password:"
send "password\r"
interact
#!/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"
首先,您需要安装

  • Ubuntu/Debian:
    apt-get-install-sshpass
  • Fedora/CentOS:
    yum安装sshpass
  • 拱门:
    pacman-sshpass

示例:

sshpass-p“您的密码”ssh-o strichostkeychecking=no您的密码_USERNAME@SOME_SITE.COM
自定义端口示例:

sshpass-p“您的密码”ssh-o strichostkeychecking=no您的密码_USERNAME@SOME_SITE.COM:2400
注意事项:

  • sshpass
    还可以在传递
    -f
    标志时从文件中读取密码。
    • 如果执行
      ps
      命令,则使用
      -f
      可防止密码可见
    • 存储密码的文件应具有安全权限

    • 我的工作原理如下


      .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
      
      获取使用新配置刷新的128个hadoop数据节点-假设您正在为hadoop/conf文件使用NFS装载

      希望这有助于某人-我是一个Windows numpty,这花了我大约5个小时才弄明白

      变体I

      sshpass -p PASSWORD ssh USER@SERVER
      
      变式二

      #!/usr/bin/expect -f
      spawn ssh USERNAME@SERVER "touch /home/user/ssh_example"
      expect "assword:"
      send "PASSWORD\r"
      interact
      

      在为这个问题寻找了几个月的答案后,我终于找到了一个更好的解决办法:写一个简单的脚本

      #!/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
      
      将其置于
      /usr/bin/exp
      ,然后您可以使用:

      • exp-ssh
      • exp-scp

      完成了

      我有一个更好的解决方案,包括使用您的帐户登录,而不是更改为root用户。 这是一个bash脚本


      参考资料:

      关于@abbotto的答案对我来说并不适用,我不得不用不同的方式来做一些事情:

    • yum安装sshpass更改为-rpm-ivh
    • 使用sshpass的命令更改为-sshpass-p“pass”sshuser@mysite-第2122页

    • 要通过shell脚本连接远程计算机,请使用以下命令:

      sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS
      
      其中,
      IPADDRESS
      USERNAME
      PASSWORD
      是需要在脚本中提供的输入值,或者如果我们想在运行时提供,请使用“read”命令。

      sshpass
      具有更好的安全性 我在寻找ssh进入陷入困境的服务器的方法时偶然发现了这个线程——处理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
      
      sshpass+autossh 前面提到的
      sshpass
      的一个好处是,您可以将它与
      autossh
      一起使用,从而消除了更多的交互低效

      sshpass -p mypassword autossh -M0 -t myusername@myserver.mydomain.com
      

      这将允许自动重新连接,例如,您的wifi因关闭笔记本电脑而中断。

      我想我没有看到任何人建议这样做,OP只是说了“脚本”,所以

      我需要解决同样的问题,我最熟悉的语言是Python

      我用过帕拉米科图书馆。此外,我还需要使用
      sudo
      发布需要升级权限的命令。原来sudo可以通过stdin通过“-S”标志接受其密码!见下文:

      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
      ,然后想出了这个方法。

      在脚本中使用这个脚本,第一个参数是主机名,第二个参数是密码

      #!/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_ASKPASS="echo \"my-pass-here\""
      ssh -tt remotehost -l myusername
      

      这就是我登录服务器的方式

      ssp <server_ip>
      

      如果在Windows系统上执行此操作,则可以使用Plink(PuTTY的一部分)

      在linux/ubuntu中

      ssh username@server_ip_address -p port_number
      
      按enter键,然后输入服务器密码


      如果您不是root用户,那么在下面的示例中,在命令开始时添加sudo,我将编写我使用的解决方案:

      场景:我想使用sh脚本从服务器复制文件:

      #!/usr/bin/expect
      $PASSWORD=password
      my_script=$(expect -c "spawn scp userName@server-name:path/file.txt /home/Amine/Bureau/trash/test/
      expect \"password:\"
      send \"$PASSWORD\r\"
      expect \"#\"
      send \"exit \r\"
      ")
      
      echo "$my_script"
      

      我正在使用下面的解决方案,但为此,您必须安装
      sshpass
      ,如果尚未安装,请使用
      sudo apt install sshpass

      现在你可以这样做了

      sshpass-p*yourspassword*shhroot@IP

      还可以创建bash别名,这样就不必反复运行整个命令。 遵循以下步骤

      cd~

      sudo nano.bash\u配置文件

      在文件末尾添加以下代码

      mymachine(){sshpass-p*yourspassword*shhroot@IP}

      source.bash\u配置文件

      现在只需从终端运行
      mymachine
      命令,您就可以在没有密码提示的情况下进入您的计算机

      注:

    • plink your_username@yourhost -pw your_password
      
      ssh username@server_ip_address -p port_number
      
      #!/usr/bin/expect
      $PASSWORD=password
      my_script=$(expect -c "spawn scp userName@server-name:path/file.txt /home/Amine/Bureau/trash/test/
      expect \"password:\"
      send \"$PASSWORD\r\"
      expect \"#\"
      send \"exit \r\"
      ")
      
      echo "$my_script"
      
      #!/bin/bash
      
      sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM
      
      sudo chmod +x startMyServer
      
      #!/usr/bin/bash
      read -p 'Enter Your Username: ' UserName;
      read -p 'Enter Your Password: ' Password;
      read -p 'Enter Your Domain Name: ' Domain;
      
      sshpass -p "$Password" ssh -o StrictHostKeyChecking=no $UserName@$Domain