欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

远程批量部署

程序员文章站 2022-07-14 10:15:00
...
#!/usr/bin/expect  --


if { [llength $argv] != 2} {
	puts "usage: $argv0 ip \"command \[params\]\""
	exit 1
}


set maxRetry 1
for {set retryNum 0} {$retryNum<$maxRetry} {incr retryNum} {

spawn  /usr/bin/ssh [lindex $argv 0] [lindex $argv 1]

set timeout 60
expect {


 
	"yes/no)?" {
  
		send "yes\r"

		expect eof

		break

	}
  
	timeout {continue}
	eof {continue}

}
}

///////////////////////////////////////////////////////////
#!/usr/bin/expect --

set USER [exec whoami]

puts "pcp.sh..."
if { [llength $argv] < 2} { 

    puts "usage: $argv0 ip filename"  
    exit 1
}
set success 0

set maxRetry 5
for {set retryNum 0} {$retryNum<$maxRetry} {incr retryNum} {

    spawn  /usr/bin/scp -r [lindex $argv 1] [lindex $argv 0]:/home/${USER}

    set timeout 600
    expect { 

        "password:" {    	
            puts "input passwd"
            send "[lindex $argv 3]\r"	
            set timeout 600
            puts "try $retryNum"
            expect { 
                timeout {continue}
                eof {
                    set success 1
                    break
                }
            }
        }

        "yes/no)?" {
            puts "input yes"
            send "yes\r"
            expect "password:" {
                send "[lindex $argv 3]\r"
                set timeout 600
                expect { 
                    timeout {continue}
                    eof {
                        set success 1
                        break
                    }
                }
            }
        }

        timeout {continue}
        eof {
            set success 1
            break
        }
    }
}

puts "pcp sucess..."
if { $success==0 } {
    exit 1
}

 

function SetValue
{
        if [[ $# != 3 ]]
        then
            echo 'input: $1: filename, $2: keyname, $3:keyvalue'
            exit 1
        fi
        filename=$1
        keyname=$2
        keyvalue=$3     # ¿ÉÒÔΪ¿Õ

        # È·±£Ä¿±ê´æÔÚ
        targetLine=`grep "^${keyname}=" ${filename}`

        if [ "$targetLine" ]
        then
            # ÕâÀïÓÃ%×÷Ϊsed·Ö¸ô·û,ÒòΪkeyvalue¿ÉÄܺ¬ÓÐ/
            sed --in-place "s%^${keyname}=.*%${keyname}=${keyvalue}%" ${filename}
        else
            echo "${keyname}=${keyvalue}" >>${filename}
        fi

}