Shell script realizes SSH automatic login function sharing _linux shell

Source: Internet
Author: User
Tags ssh

FileName: Ssh_auto_login

Copy Code code as follows:

#!/usr/bin/expect
##
# SSH Analog Lander
#
# @author Zhiyuan ##
If {$ARGC <4} {
Puts "Error params: $argv"
Puts "Expect params:user passwd IP port [translate_id]"
Exit 1
}

Set Default_passcode "The default password for the tunnel here"

Set user [lindex $argv 0]
Set password [lindex $argv 1]
Set IP [lindex $argv 2]
Set port [lindex $argv 3]
Set Timeout 10

While 1 {
Spawn ssh-p $port $user @ $ip
#如果最后的字符匹配则执行命令 \ r ends with an indication of determining
Expect {
"*yes/no" {send "yes\r"; Exp_continue}
' *password: ' {send ' $password \ r '}
}
#这里是需要通过通道机登陆时的匹配流程, modify it as needed.
Expect {
"*passcode:" {
Send_user "Please input channel machine dynamic password:";
Expect_user-re "(. *) \ \"
Set Random_passcode $expect _out (1,string)
Send "$default _passcode$random_passcode\r"
Expect {
"Access Denied" {Continue}
"Enter:" {send "1\r"}
}
Set TRANSLATE_IP [lindex $argv 4]
If {$translate _ip!= ""} {
Expect "*):" {send "$translate _ip\r"}
}
}
# "Last login:*" {}
}
Break
}
#无法匹配 $, still don't know how to solve
#expect-re "*\$" {puts "test123"; send "source/etc/profile\r"}
#expect "*\$" {Send CD ~\r}
Send_user "Login success!"
Interact

The above is SSH automatic login, can cooperate with the shell below use, very convenient.
FileName: Xxx_launcher

Copy Code code as follows:

#!/bin/sh
##
# Server Lander
#
# @author Zhiyuan ##
Channel_user= "User_namexxx"
channel_passwd= "XXXX"
#内网通道机
Internal_ip1=xxx.xxx.xxx.xxx
#联通
Unicom_ip1=xxx.xxx.xxx.xxx
#电信
Telecom_ip1=xxx.xxx.xxx.xxx
Case "$" in
Ci
Expect Ssh_auto_login $channel _user $channel _passwd $internal _ip3 22

Cl
Expect Ssh_auto_login $channel _user $channel _passwd $unicom _ip1 22

cd
Expect Ssh_auto_login $channel _user $channel _passwd $telecom _ip1 22

149)
Expect Ssh_auto_login Channel_user channel_passwd xxx.xx.xxx.xxx 22

49)
Expect Ssh_auto_login $channel _user $channel _passwd $unicom _ip1 22 require channel machine jump ipxxx.xxx.xx

*)
echo "Help info:"
echo "\tthere is not a server named [$]"
echo "\ t Server 149:\t149"
echo "\ t Server 49:\t49"

Esac

At this point when landing a server directly with the above shell with the server parameters to log in, such as:./xxx_launcher 49

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.