셸 프로 그래 밍 의 대화 면제(Here Document+Expect)
24627 단어 셸 스 크 립 트
<<
...
...
1.1 Here 문서 사용 주의사항
[root@localhost ~]# vim hello.sh
#!/bin/bash
read i <<EOF
hello world
EOF
echo $i
[root@localhost ~]# chmod +x hello.sh
[root@localhost ~]# ./hello.sh
hello world
[root@localhost ~]# vim user.sh
#!/bin/bash
passwd lisi <<EOF
123abc
123abc
EOF
[root@localhost ~]# ./passwd.sh
lisi 。
: : 7
:passwd: 。
1.3 여기 문서 변수 설정
[root@localhost ~]# vim user.sh
#!/bin/bash
name="cllt"
filename="test.txt"
cat > $filename <<EOF
this is $name.com
EOF
[root@localhost ~]# cat test.txt
this is cllt.com
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
filename=$(cat <<EOF
this is cllt.com
my name is cllt
$first
EOF
)
echo $filename
[root@localhost ~]# ./user.sh
this is cllt.com my name is cllt hello
1.4 Here 문서 형식 제어
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
cat <<'EOF' ' '
this is cllt.com
$first
EOF
[root@localhost ~]# ./user.sh
this is cllt.com
$first
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
cat <<-EOF '- TAB '
this is cllt.com
$first
EOF
[root@localhost ~]# ./user.sh
this is cllt.com
hello
1.5 Here Document 여러 줄 주석
Here Document 방식 을 통 해 Bash 가 여러 줄 주석 을 지원 합 니 다.
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
: <<EOF ' '
this is cllt.com
$first
EOF
echo "lemon"
[root@localhost ~]# ./user.sh
lemon
2.기대 개술
Linux 시스템 에 서 는 expect 가 자체 적 으로 가지 고 있 지 않 으 므 로 스스로 설치 해 야 합 니 다.
yum install expect -y
2.1 기본 명령
expect "password: " {send "mypassword\r“;}
expect "aaa" {send“AAA\r"} 'send , \r
'
expect "bbb" {send“BBB\r"}
expect "ccc" {send“CCC\r"}
expect { ' aaa bbb ccc , send expect '
"aaa"{send“AAA\r"}
"bbb" {send“BBB\r"}
"CCC" {send“CCC\r"}
}
expect { 'exp_ continue , aaa, send bbb'
"aaa" {send“AAA"; exp_ continue}
"bbb" {send“BBB"; exp_ continue}
"ccc" {send“CCC"}
}
2.3 기대 실행 방식
[root@localhost ~]# vim demo01.sh
#!/usr/bin/expect
#
set timeout 20
#
log_file test.log
#
log_user 1
#
set hostname [lindex $argv 0]
set password [lindex $argv 1]
#
spawn ssh root@${hostname}
#
expect {
"connecting (yes/no)"
{send "yes\r";exp_continue}
"*password:"
{send "${password}\r";}
}
#
interact
[root@localhost ~]# ./demo01.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:34:58 2020 from 192.168.179.1
[root@server ~]#
[root@localhost ~]# vim demo02.sh
#!/bin/bash
hostname=$1
password=$2
#expect
/usr/bin/expect <<-EOF
spawn ssh root@${hostname}
#
expect {
"connecting (yes/no)"
{send "yes\r";exp_continue}
"*password:"
{send "${password}\r";}
}
expect "*]#"
send "exit\r"
expect eof
EOF
[root@localhost ~]# ./demo02.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:35:17 2020 from 192.168.179.100
[root@server ~]# exit
Connection to 192.168.179.144 closed.
[root@localhost ~]#
3.실험 사례
3.1 사용자 생 성 및 비밀번호 설정
[root@localhost ~]# vim demo03.sh
#!/bin/bash
users=$1
passwd=$2
useradd ${users}
#expect
/usr/bin/expect <<-EOF
spawn passwd ${users}
#
expect {
" :"
{send "$2\r";exp_continue}
" :"
{send "$2\r";}
}
#expect eof
EOF
[root@localhost ~]# ./demo03.sh wangwu 123456
spawn passwd wangwu
wangwu 。
:
: 8
:
passwd: 。
3.2 ssh 자동 로그 인 실현
[root@localhost ~]# vim demo04.sh
#!/usr/bin/expect
set timeout 5
set hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh ${hostname}
expect {
"Connection refused" exit
"Name or service not known" exit
"connecting (yes/no)"{send "yes\r";exp_continue}
"*password:"{send "${password}\r"}
}
interact
exit
[root@localhost ~]# ./demo04.sh 192.168.179.144 123456
spawn ssh 192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:50:59 2020 from 192.168.179.100
[root@server ~]# exit
[root@localhost ~]# ./demo04.sh
spawn ssh root@
ssh: Could not resolve hostname : Name or service not known
[root@localhost ~]#
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
셸 스 크 립 트 (다 중 스 레 드 대량 생 성 사용자)예 를 들 어 백업 데이터 베 이 스 는 100 개의 라 이브 러 리 가 있 고 정상 적 인 백업 효율 이 매우 낮 습 니 다.다 중 스 레 드 가 있 으 면 백업 하 는 데 10 시간 이 걸 릴 수도 있 었 는데 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.