Shell script: Login with SSH and change password

Source: Internet
Author: User
Tags administrator password

In peacetime, we sometimes encounter the need to change the server administrator password, if the server is more, we can write a script to achieve, save time and effort.

Linux uses SSH login need to manually enter Yes to confirm the connection, so first to solve this problem, so that the script implementation of remote execution commands without human intervention.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/89/84/wKioL1gWuTywIc-wAABXtJNoc0s559.jpg "/>

The first step is to create a native public and private key using Ssh-keygen

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/89/86/wKiom1gWuTzCsi1CAACefdXp39g376.jpg "/>

After a successful creation, the private key and public key are generated under/ROOT/.SSH

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/89/84/wKioL1gWuTyAZpviAABLE6X53I4325.jpg "/>

The second step is to use Ssh-copy-id to copy the public key to the remote host and expect internal command to write the SSH automatic login script

auto_ssh_copy_id () {

Expect-c "Set timeout-1;

Spawn/usr/bin/ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]$2;

Expect {

* (yes/no) * {send--yes\r;exp_continue;}

*password:* {send--$1\r;exp_continue;}

Eof{exit 0;}

}";

}

Call method: auto_ssh_copy_id $pass 1 $ipnet. $i

Suppose you need to change the password of the server IP between 172.18.0.1-172.18.0.100, the script is as follows.

#!/bin/bash

#Program

#

#relase

#tryrus 20161029

ipnet=172.18.0 #改成实际的IP Segment

DECLARE I=1 #改成实际开始的IP

Pass1=password1 #ssh远程登陆root的密码

Pass2=password2 #要设定的新密码

auto_ssh_copy_id () {

Expect-c "Set timeout-1;

Spawn/usr/bin/ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]$2;

Expect {

* (yes/no) * {send--yes\r;exp_continue;}

*password:* {send--$1\r;exp_continue;}

Eof{exit 0;}

}";

}

AUTO_SSH_CHANGE_PSW () {

Expect-c "Set timeout-1;

Spawn ssh [email protected]$2 "passwd";

Expect {

*new* {send--$1\r;exp_continue;}

*retype* {send--$1\r;exp_continue;}

Eof{exit 0;}

}";

}

while [["$i"-le "]] #控制循环, the value is changed to the actual IP to be used

Do

Ping "$ipnet. $i"-C 3 >/dev/null

If [$?-eq 0];then

auto_ssh_copy_id $pass 1 $ipnet. $i #运行一次后, it's not necessary.

AUTO_SSH_CHANGE_PSW $pass 2 $ipnet. $i

Fi

Let "I+=1"

Done

Test results for the second time

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/89/86/wKiom1gWuT3xftwnAACfow3H04M685.jpg "/>


Thank you for opening this blog post, and always adhere to see here, if you feel helpful, please do not skimp on the lower right corner of the praise.


This article is from the "Bit Technology" blog, please be sure to keep this source http://tryrus.blog.51cto.com/10914693/1867564

Shell script: Login with SSH and change password

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.