Shell implementation SSH Automatic login server script

Source: Internet
Author: User
Tags ssh

The

SSH connection to a remote host will ask for a password, like the default behavior of the SU, sudo command, is not read from stdin, the legend is for security considerations, but sometimes in the script does require an unattended landing.
Search for a similar example, using expect to complete the password answer:

  code is as follows copy code

#!/bin/bash
Auto_login_ssh () {
    expect-c" Set Timeou t-1;
                Spawn-noecho ssh- o Stricthostkeychecking=no $ ${@:3};
                Expect *assword:* ;
                send-$1r;
                interact; ";
}
 
auto_login_ssh password user@host

Stricthostkeychecking=no

parameter allows SSH to default to add the new host's public key fingerprint, there will be no indication of whether to continue yes/no.

The above expect code basically can meet the requirement, can realize automatic login


Automatic SSH/SCP Scripts

If you need to from a, to B, and then to C, then need SSH and SCP two times, is more troublesome.
SSH Automatic Login:

The code is as follows Copy Code

#!/usr/bin/expect-f
Set Timeout 30
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SSH weiqiong@cr"
Expect "Password:"
Send "PPPPPPR"
Interact

SCP copies files from a to C:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn SCP $file Weiqiong@b:/home/weiqiong
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SCP $file weiqiong@c:/home/weiqiongr"
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Exit
Interact

SCP copies files from C to a:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SCP weiqiong@c:/home/weiqiong/$file. R"
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "Exitr"
Expect "]*"
Spawn SCP weiqiong@b:/home/weiqiong/$file.
Expect "Password:"
Send "PPPPPPR"
Interact

4. Establishment of SSH/SCP Channel

For example, my machine is a, intermediate server is B, target server is C
From a can ssh to B, from B can ssh to C, but a cannot ssh directly to C
Now demonstrates using SSH channel technology to transfer files directly from A to C

The code is as follows Copy Code
1. Ssh-l1234:c:22 userid@b
Input B ' s password

(1234 is a free port for native A, which requires root user rights on machine A, and actually establishes a channel on the native 1234 port)

2. Open a new console and type:

The code is as follows Copy Code
scp-p1234 filename Userid@localhost:
Input C ' s password


If it is just daily use, in order to avoid the trouble of frequently entering the host password, the most ideal method is to produce the local public/private key pair, the fingerprint is copied directly to the remote host, the newer OpenSSH provides the Ssh-copy-id tool:

The code is as follows Copy Code

Ssh-keygen
Ssh-copy-id User@host1
Ssh-copy-id User@host2
Ssh-copy-id User@host3

Run Ssh-keygen will ask a few questions, all return by default is the effect we want, respectively, the key distribution to the remote host, after the implementation of SSH user@host, or SCP, are directly completed.

If you need to remove the remote machine corresponding to the local account key, login to the account, open the ~/.ssh/authorized_keys file, search your username, delete the line, save, you can.

Of course it can be automated:

The code is as follows Copy Code

Auto_ssh_copy_ ID () {
    expect-c "set timeout-1;
                Spawn Ssh-copy-id $;
                Expect {
                     * (yes/no) * {send-yesr;exp_continue;}
                     *assword:* {send-$1r;exp_continue;}
                     eof        {exit 0;}
               } ";
}

Related Article

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.