Expect non-interactive features of Linux

Source: Internet
Author: User

In my last blog post , SSH key authentication for Linux has been used to authenticate each other, but each time I use SSH to log on to another server or enter a password.

Expect is a tool that provides automatic interaction, automatically connects to managed servers, and does not require a password to be entered manually.

1, installation expect

[email protected] ~]# Yum install-y expect

2, write expect script, direct distribution SSH public key, do not enter the password manually.

Vim/server/scripts/expect.exp

1#!/usr/bin/expect2 3#-------------CopyRight-------------4 # name:ssh Send password5# Version Number:1.00  6 # Type:sh7 # Language:expect8# Date:2018- to- -  9 # Author:sandyTen# QQ:442656067 One# Email:[email Protected]163. com A# Blog:https://www.cnblogs.com/eeexu123/ -  - if{$ARGC! =2 } { theSend_user"usage:expect fenfa_expect file host\n"//Determine if the incoming parameter is 2 -Exit1 - } -  + #defineVar - Setfile [lindex $argv0]//The first parameter is an SSH public key + SetHost [lindex $ARGV1]//The second parameter is the remote host address of the connection A Setpasswd"Herine"//Set the password for the connecting user at  -  - #send ssh Key -Spawn Ssh-copy-id-i $file"- p [email protected] $host"//Send SSH public key command - Expect { -   "yes/no"{Send"yes\r"Exp_continue}//whether to continue the connection, expect interactive function, add Yes automatically, and continue. Yes after must add \ r return character - } to  +Sleep3//Waiting to connect to the remote host -Expect"*password"//Enter password, expect interactive function, add password variable automatically. Back add \ r return character theSend"$passwd \ r" * expect EOF $ Panax NotoginsengExit-OnExit { -Send_user"goodbye!\n"//Exit the}

3. Testing

/usr/bin/expect test_expect.exp ~/.ssh/id_dsa.pub 172.16.1.72
The above command can be placed in the script, a large number of SSH key connection
1 [email protected] scripts]#/usr/bin/expect test_expect.exp ~/.ssh/id_dsa.pub 172.16. 1.72 2Spawn Ssh-copy-id-i/root/.ssh/id_dsa.pub-p A[Email protected]172.16.1.723The authenticity of host'172.16.1.72 (172.16.1.72)'Can't be established.4RSA Key Fingerprint isA5: -:D 4: the: $: -: -: aa: About: 8d:f0:ce:98: 5a:d3:f4.5Is you sure-want toContinueConnecting (yes/no)?Yes6Warning:permanently added'172.16.1.72'(RSA) to the list of known hosts.7[Email protected]172.16.1.72's Password:8NowTryLogging into the machine, with"ssh '-p [email protected] '", and checkinch:9 Ten. ssh/Authorized_keys One  ATo make sure we haven'T added extra keys that you weren'T expecting. -  -goodbye!

SSH Remote use command

 1 [[email protected] scripts]# ssh [email protected]172.16. 1.72 "/sbin/ifconfig eth1" 2eth1 Link encap:ethernet HWaddrxx: 0C: in: 8D: $: the  3inet Addr:172.16.1.72Bcast:172.16.1.255Mask:255.255.255.04Inet6 addr:fe80::20c:29ff:fe8d:6592/ -Scope:link5Up broadcast RUNNING multicast MTU: theMetric:16RX Packets:560Errors0Dropped0Overruns:0Frame07TX Packets:218Errors0Dropped0Overruns:0Carrier08Collisions:0Txqueuelen: + 9RX bytes:72275(70.5KiB) TX Bytes:39742(38.8KiB)

By on can, the expect interactive function succeeds in SSH password-free operation.

Expect non-interactive features of Linux

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.