Linux and linux

Source: Internet
Author: User

Linux and linux

Simple mode:

#!/usr/bin/expect -f   set timeout 5   spawn ssh root@192.168.0.1expect "*assword*"   send "root\r"   expect "#"   send "ifconfig \r"   expect eof

Explanation:

Send: used to send a string to the process: receive a string from the process, for example, receive CT "* assword *"
Spawn: Start a new process interact: allow user interaction

How to Use variables:
#!/usr/bin/expect -fset port 22set user rootset host 192.168.0.12set password rootset timeout -1spawn ssh -D $port $user@$host "ifconfig"expect {  "*yes/no" { send "yes\r"; exp_continue}  "*assword:" { send "$password\r" }  }  expect "*#*"  send "ifconfig > /home/cfg \r"send "exit\r"}

Explanation:

Keep CT {"* yes/no" {send "yes \ r"; exp_continue} "* assword:" {send "$ password \ r "}}
Select the mode. exp_continue indicates that the operation continues.

Get the variable by reading the configuration file:
Configuration File
192.168.0.1 root192.168.0.2 root

Automated login script

#!/usr/bin/expect -fset f [open ./ip r]  while { [gets $f line ]>=0 } {       set ip [lindex $line 0]     set pwd [lindex $line 1]spawn ssh $ipexpect  "*password:" { send "$pwd\r" }expect "#"send "ifconfig \r"send "exit\r"interact}

Explanation:

Multiple servers can be executed cyclically. This is a very useful method!

Automatic Remote file copy:

#!/usr/bin/expect -fset port 22set user rootset host 192.168.28.30set password rootset timeout -1spawn scp $host:/home/cfg ./expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect eof

Explanation:

Same principle as ssh

Run the command remotely and write the file to the local server through scp:

#!/usr/bin/expect -fset port 22set user rootset host 192.168.28.30set password rootset timeout -1spawn ssh -D $port $user@$host "ifconfig"expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect "*#*"send "ifconfig > /home/cfg \r"send "exit\r"interactspawn scp $host:/home/cfg ./expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect eof

Explanation:

Automated O & M, remote interaction from server A ssh to server B, and then execute commands on server B.

Http://www.cnblogs.com/Javame/p/4272440.html

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.