Most of the time, we need to use ftp or other methods in the script to retrieve files from another machine. For security reasons, user authentication is generally required to log on to another machine. The example here calls the round CT script in the sh script to implement automatic interaction.
CT is a software used to implement automatic conversation. CT is built on the basis of the TCL language. It also provides commands not available in some TCL languages. For example, the spawn command starts a Unix/Linux program for interactive running, the send command sends a string to the program, and the keep CT command waits for some strings to be sent.
Ct. Sh file content:
Bytes ----------------------------------------------------------------------------------------------------------------
#! /Usr/bin/CT-F
If {[lindex $ argv 0] = "getinfo "}{
Set first_time "N"
Spawn SFTP username@192.168.0.1
CT {
"* (Yes/No )? "{
Set first_time "Y"
Sleep 1
Send "Yes/R"
}
"* Password :"{
Sleep 1
Send "password/R"
}
Timeout {
Exit 2
}
}
If {"$ first_time" = "Y "}{
CT {
"* Password :"{
Sleep 1
Send "password/R"
}
Timeout {
Exit 2
}
}
}
CT "SFTP>"
Send "ls-All [lindex $ argv 1]/R"
CT "SFTP>"
Exit 0
}
If {[lindex $ argv 0] = "Download "}{
Spawn SFTP username@192.168.0.1
CT {
"* Password :"{
Sleep 1
Send "password/R"
}
Timeout {
Exit 2
}
}
Set timeout-1
CT "SFTP>"
Send "get [lindex $ argv 1]/[lindex $ argv 2] [lindex $ argv 3]/R"
CT "SFTP>"
Exit 0
}
Bytes ----------------------------------------------------------------------------------------------------------------
This is because the following confirmation information is displayed when one machine accesses another for the first time:
Bytes ----------------------------------------------------------------------------------------------------------------
$ SFTP username@192.168.0.1
Connecting to 192.168.0.1...
The authenticity of host '192. 168.0.1 (192.168.0.1) 'can't be established.
RSA key fingerprint is 8A: 3E: 5b: A2: DD: B1: 19: 2D: C9: 1f: B9: 69: 96: 15: 5C: 41.
Are you sure you want to continue connecting (Yes/No )?
Bytes ----------------------------------------------------------------------------------------------------------------
However, these authentication information will not appear in future access, so use first_time to differentiate.
The getinfo and download functions are implemented here, because the function cannot be called in the script as in C ++. Here, the function is selected by passing parameters. The first function getinfo lists the files in a directory on the target machine, and the second function downloads the files on the target machine.
The next step is how to call reverse ct in the sh script. In this case, the main reason why SH is called in SH is that sh has powerful functions and many functions can be implemented in SH, such as calling sed to process strings.
Content of the autodownload. Sh file:
Bytes ----------------------------------------------------------------------------------------------------------------
#! /Bin/bash
Expect_shell = "Expect CT. Sh"
Temp_file = "serverinfo. tmp"
# Get server target directory information and then save it into a temp local file
'Exec/usr/bin/empty CT $ working_dir/$ expect_shell getinfo $ server_spider_dir>./$ temp_file'
While read line_str
Do
# Your process to get your target file & Save the target file name in target_file
Done <./$ temp_file
# Delete Temp File
Rm $ temp_file
# Download the target file
$ Download_place = ./
'Exec/usr/bin/expect CT./$ expect_shell download $ target_file $ download_place>/dev/null'
...
Exit 0
Bytes ----------------------------------------------------------------------------------------------------------------
2007/11/14