A.
1. command Line:/usr/bin/keep CT-c "set timeout-1; spawn Su; keep CT \" Password: \ "; send \" 123456 \ r \"; interact "2 3 B. script: 4 5 #! /Usr/bin/analyze CT 6 set timeout 5 7 spawn SU 8 analyze CT "password: "9 10 Send" 123456 \ r "11 interact12 13 # expect" #"14 15 # Send" whoami"
B:
Use the login CT script to log on to the root account and execute the command
1. Use the login CT script to log on to the root account. The method is relatively simple. You only need to write a script in the following format: 2 3 #! /Usr/bin/Reset CT 4 5 set timeout = 30 # set 30 seconds timeout 6 7 spwan su root 8 9 reset CT "password *" 10 11 send "123456 \ r" # Send Password, don't forget to end with \ r 12 13 CT "* # *" # Wait # prompt appears 14 15 send "ls-L>/tmp/1 \ r" # Run the LS command, and redirect to/tmp/116 17 CT "* # *" # Wait # appears, this indicates that the previous command has been executed and sent to "uname-A>/tmp/2 \ r" # Then run the next command 20 21 # interact # After interact is used, the script will exit under the root account. You can manually execute the command 22 23 subject ct eof 24 25 exit
________________________________________________________________________________________________________________________
Modern shell provides minimum control (start, stop, and so on) for the program, and leaves the interaction feature to the user. This means that you cannot run some programs without interaction, such as passwd.
Some programs can be run without interaction, but the flexibility is largely lost, such as fsck. This indicates that the construction logic of UNIX tools is starting to have problems. CT just fills in some of the cracks and solves the problem of long-term existence in the Unix environment.
. Expect uses TCl as the core language. In addition, CT can be used no matter whether the program is interactive or non-interactive. This is a classic example of how a small language works with other UNIX tools to generate powerful functions. For example, if you are a "lazy" person and want to do repetitive work every day, you just don't want to "do ". Therefore, a "lazy" approach you come up with is the script tool. After executing the script, you can get the result while drinking tea. This is a wonderful thing
The other side is naturally not so beautiful. Maybe shell needs to communicate with you to complete (that is, the interaction we often say ). At this time, you have to think of the magic CT. Use the following method: A. Enter the autoclientct command in the terminal, and then you will see the prompt information of autoclientct started, file is script. Exp. At this time, you have entered the automatic command recording and done all the things you want to do.
The tool will record it in detail and write it into the script. Exp file. When you are sure you are finished, enter exit to exit "Recording ". The prompt message of AutoComplete CT done and file is script. Exp is displayed, indicating that the recording is successful. Script. Exp
It is a text file that can be opened and viewed by using vim or Emacs. You can also modify the file based on your needs, provided that the file meets the rules. The above mentioned is to use the automatic method to use CT, you can also manually write the script for execution. Script #! Starting with/usr/bin/CT, the path where the CT command is stored can also be obtained by executing which CT. Below is a simple
Example to implement shell switching:
12 #! /Usr/bin/expect13 # change a login shell to tcsh14 spawn chsh15 regular CT "]:" 16 send "/bin/tcsh \ r" 17 regular CT eof18 19 Save As filename. sh file, add the execution permission ,. /filename. sh to see the results. A little bit... Don't worry. Good stuff is still in the back! 20 21 remember to switch user commands in Linux, right, Su. If you want to switch from a common user to the root user, then continue the operation. Next, let's look at: 22 23 #! /Usr/bin/expect24 25 set user [lindex $ argv 0] 26 set passwd [lindex $ argv 1] 27 spawn Su-$ user28 regular CT ": "29 send" $ passwd \ r "30 secondary CT"] #"31 send" CD/Root \ r "32 secondary CT"] #"33 send" Sh T. sh \ r "34 interact35 36 can write the parameters you want to pass together, as shown in. /filename. sh root 123456, argv [0] is root, argv [1] is 123456.
Spawn executes the command in reverse CT mode, and then reverse CT finds that when the row ends with ":", it uses send to pass the command to be executed to interact with shell. Finally, the interact command gives control to the user you switch.
_________________________________________________________________________________________________________________________
9. Simplified CT manual Chinese Version
Http://blog.csdn.net/classwang/article/details/3928467
10. How to interact twice in CT?
Http://bbs.chinaunix.net/forum.php? MoD = viewthread & tid = 2333695.
11. My own summary:
Objective: To regularly Delete measurement files on a GOMs machine.
Background: The GOMs machine customizes the Linux read-only system for nosi. No STARTUP script can be added.
Method: FEWS is a customized Linux system. Use script a in FEWS to add a scheduled task and call script B at on the first day of every month. Script B will SSH to every GOMs to call script C, script C will
First, switch to the root user (only the root user can delete files), and then call script d to execute the cleanup task.
Disadvantage: script D is not efficient enough, but I am too lazy to change it.
Script:
1 #!/bin/bash 2 3 crontab -r 4 5 echo "00 20 1,10,20 * * /root/kobe/call_goms_file_del.sh" > /root/kobe/crontab_task 6 7 crontab /root/kobe/crontab_task 8 9 crontab -l10 exit 0
Script B:
1 #/bin/bash 2 ######################### 3 #1.SSH every GOMS,Call the function script. 4 #2.touch log 5 ########################### 6 call_goms_file_del_log_file=/root/kobe/call_goms_file_del.log 7 8 del_cmd="" 9 10 function log()11 {12 echo -e "$(date +'%Y-%m-%d %T ') $*" >> $call_goms_file_del_log_file13 }14 15 16 if [ -f "$call_goms_file_del_log_file" ]; then17 log $call_goms_file_del_log_file exilt!18 del_cmd=$(date | awk '$3==1{printf("rm -f /root/kobe/call_goms_file_del.log")}')19 log del_cmd:$del_cmd20 if [ "${del_cmd}" != "" ]; then21 ${del_cmd}22 log recreat $call_goms_file_del_log_file...23 touch $call_goms_file_del_log_file24 fi25 26 else27 log $call_goms_file_del_log_file not exilt,creating...28 touch $call_goms_file_del_log_file29 fi30 31 32 33 log "Let's go..."34 35 goms_IP_list[1]='Nemuadmin@10.56.19.30'36 goms_IP_list[2]='Nemuadmin@10.56.19.28'37 goms_IP_list[3]='Nemuadmin@10.56.19.26'38 39 40 for x in ${goms_IP_list[*]}41 do42 log "Enter $x"43 expect -c "44 spawn ssh $x /home/Nemuadmin/realtime_task/goms_file_del.sh45 46 expect {47 \"*assword\" {set timeout 21600; send \"nemuuser\r\";}48 \"yes/no\" {send \"yes\r\"; exp_continue;}49 }50 expect eof"51 52 done53 54 log "It is over!."55 echo "----------------------------------------------------------------------------------" >> $call_goms_file_del_log_file56 exit 0
Script C:
1 #!/usr/bin/expect 2 3 spawn su 4 expect ":" 5 send "password\r" 6 #expect "#" 7 #send "touch /root/jj.ll\r" 8 expect "#" 9 send "/home/Nemuadmin/realtime_task/goms_file_del.sh &\r"10 expect "#"11 12 interact13 14 #expect eof15 16 exit
Script D:
1 #!/bin/bash 2 3 logfile="/home/Nemuadmin/realtime_task/goms_file_del.log" 4 5 function log() 6 { 7 echo -e "$(date +'%Y-%m-%d %T ') $*" >> $logfile 8 } 9 10 11 #expect -c " 12 # spawn su 13 # expect { 14 # \"Password: \" {send \"password\r\";} 15 # } 16 #interact" 17 18 if [ -f "$logfile" ]; then 19 log $logfile exist! 20 else 21 log $logfile not exist,creating... 22 touch $logfile 23 chmod 777 $logfile 24 fi 25 26 echo "----------------------Start time : $(date +'%Y-%m-%d %T ')--------------------------------------" > $logfile 27 28 #get date 29 year=$(date +%Y) 30 log "Year:$year!" 31 32 month=$(date +%m) 33 log "Month:$month!" 34 35 day=$(date +%d) 36 log "Day:$day!" 37 38 39 ################################################## 40 #argc:1 41 #argv:directory 42 #function:delete the files under argv parameter 43 ################################################## 44 function file_del() 45 { 46 log "cd $1" 47 cd $1 48 for i in *.gz 49 do 50 #get file's date 51 file_date=$(echo $i | cut -d "." -f4) 52 log "file[$i] file_date: $file_date!" 53 54 file_year=${file_date:0:4} 55 # log "file_year:$file_year " 56 57 file_month=${file_date:4:2} 58 # log "file_month:$file_month " 59 60 file_day=${file_date:6:2} 61 # log "file_day:$file_day " 62 63 # if (( (year > file_year) || ((month > file_month) && ((((month - file_month) * 30 + day) - file_day) > 10 ) ) ) )) ;then 64 # del_flag="Delete" 65 # else 66 # del_flag="Nodelete" 67 # fi 68 69 70 del_flag=$(awk -v year=$year -v month=$month -v day=$day -v file_year=$file_year -v file_month=$file_month -v file_day=$file_day 'BEGIN{ 71 if ( (year > file_year) || ((month > file_month) && ((((month - file_month) * 30 + day) - file_day) > 10 ) ) ) 72 printf("Delete") 73 else 74 printf("Nodelete")}') 75 76 # log "File($i) del_flag:$del_flag" 77 if [[ $del_flag == "Delete" && $i != "*.gz" ]];then 78 log "Delete file :[$i]!" 79 rm -f $i 80 else 81 log "File :[$i] Nodelete!" 82 fi 83 echo "-------------------------------------------------------------------------------" >> $logfile 84 done 85 } 86 87 #DELETE files under /var/opt/OMSftproot/xmlfiles directory 88 cd /var/opt/OMSftproot/xmlfiles/ 89 file_del "/var/opt/OMSftproot/xmlfiles/" 90 91 #DELETE files under /var/opt/OMSftproot/xmlfilesBU directory 92 cd /var/opt/OMSftproot/xmlfilesBU/ 93 94 for j in $(ls) 95 do 96 cd /var/opt/OMSftproot/xmlfilesBU/ 97 file_del "$j" 98 done 99 100 #done successfully.101 log "It is over!."102 exit 0