Sshpass+expect Solving Interactive problems

Source: Internet
Author: User

1, Sshpass:

Usage scenarios:

SSH login can not be specified in the command line password, Sshpass, resolved the problem, for non-interactive SSH password Authentication It supports the password from the command line, file, environment variables read.

Installation

[[email protected] ~]# Yum install sshpass-y installed: sshpass.x86_64 0:1.05-1.el6 Complete! [Email protected] ~]#

Parameters:

[[email protected] ~]# [[email protected] ~]# sshpass --helpsshpass:  invalid option --  '-' usage: sshpass [-f|-d|-p|-e] [-hv] command  Parameters   -f filename   take password to use from  file   -d number     use number as file  descriptor for getting password   -p password    provide password as argument  (security unwise)    -e             Password is passed as env-var  " Sshpass "   With no parameters - password will be taken  from stdin   -h             show help  (This screEN)    -V            Print  Version informationat most one of -f, -d, -p or -e should  be used# here Sshpass supports three modes, passwords, files, environment variables

Simple mode: (Modify port, host trust) [[email protected] ~]# ssh [email protected] -p21386  ' ls ' Address 192.168.1.221 maps to localhost, but this does not map  back to the address - possible break-in attempt! [email protected] ' s password: node2rpm-gpg-key-epel-6[[email protected] ~]# #命令行下: [[ email protected] ~]# sshpass -prenzhiyuan ssh [email protected] - p21386  ' ls ' address 192.168.1.221 maps to localhost, but this does  not map back to the address - possible break-in attempt! node2rpm-gpg-key-epel-6[[email protected] ~]# #文件模式:[[email protected] ~]# cat  renzhiyuan renzhiyuan[[email protected] ~]# sshpass -f renzhiyuan ssh  [email protected] -p21386  ' ls ' address&Nbsp;192.168.1.221 maps to localhost, but this does not map back  to the address - possible break-in attempt!node2rpm-gpg-key-epel-6[[email  protected] ~]# #环境变量里面 [[email protected] ~]# cat /etc/profile.d/renzhiyuan.sh  export sshpass= "Renzhiyuan" sshpass -e ssh [email protected] -p21386  ' ls ' [[email protected] ~]#[[email protected] ~]# /etc/profile.d/renzhiyuan.sh  Address 192.168.1.221 maps to localhost, but this does not map  back to the address - possible break-in attempt! node2rpm-gpg-key-epel-6[[email protected] ~]#

2, Expect:

Usage scenarios:

Through the shell can achieve simple control flow functions, such as: cycle, judgment and so on. But for situations where interaction needs to be done manually, we may sometimes need to implement interactions with interactive programs such as Telnet Server.

And expect is a free programming language that enables automated and interactive tasks to communicate without the need for human intervention.

[[Email protected] ~]# yum install expect -y Installed:  expect.x86_64  0:5.44.1.15-5.el6_4                                                                                                                  as dependencies are installed:  tcl.x86_64 1:8.5.7-6.el6                                                                                                                           finished! [[email protected] ~]#

Case:

2.1) SSH for automatic login and stop at the login server Yum  install expect -y[[email protected] ~]# cat  ssh.sh #!/usr/bin/expect -f  set ip [lindex  $argv  0 ]   set password [lindex  $argv  1 ]set timeout 20         spawn ssh -p21386 [email protected] $ipexpect  {"*yes/no"   { send  "yes\r"; exp_continue }  "*password:"  { send  "$password \ r"   }}  interact                                 [[ Email protected] ~]# ./ssh.sh 192.168.1.221 renzhiyuanspawn ssh -p21386  [email protected]address 192.168.1.221 maps to localhost, but this  does not map back to the address - possible break-in attempt! [email protected] ' s password: last login: wed dec  7 16:43:27  2016 from 192.168.1.217[[email protected] ~]##!/usr/bin/expect -f    set ip [lindex  $argv  0 ]                    //receives the first parameter and sets the Ip   set password  [lindex  $argv  1 ]              //receives the second parameter and sets the password    set timeout 10                               //set timeout time    spawn ssh [email protected] $ip                           //send ssh please 滶     expect {                                     //return information match     "*yes/no"  { send  "yes\r"; exp_continue}     //the first SSH connection will prompt yes/no, continue     "*password:"  { send  "$password \ r"  }        //password prompt, send password    }    interact                                    // Interactive mode, the user will stay on the remote server.   2, 2) connect to different machines based on IP and password. [[email protected] ~]# ./ssh.sh spawn ssh -p21386 [Email protected] Address 192.168.1.221 maps to localhost, but this does not map  back to the address - possible break-in attempt! [email protected] ' s password: last login: wed dec  7 16:43:56  2016 from 192.168.1.217[[email protected] ~] #2.3) Telnet to the server and execute the command, after execution and exit [email  protected] ~]# ./ssh.sh spawn ssh -p21386 [email protected]address  192.168.1.221 maps to localhost, but this does not map back  to the address - possible break-in attempt! [email protected] ' s password: last login: wed dec  7 16:45:33  2016 from 192.168.1.217[[email protected] ~]# pwd/root[[email protected]  ~]# exitlogoutconnection to 192.168.1.221 closed. [[email protected] ~]#

3, Problem: (limited ability, so far to seek help and research has not come out)

If you do have password SSH trust, how to use Sshpass or except to solve the key password interactive problem?

3.1) #sshpass-P ' password ' ssh-p21345-i Renzhiyuan user @ip (undesirable)

2.2) The except script actually failed to cross the SSH key password.


Welcome everyone to express their opinions, learn from each other progress.

This article is from "Never give up!" Ningzhiyuan "blog, reprint please contact the author!

Sshpass+expect Solving Interactive problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.