When using JSCH to connect, you need to enter a password.
Source: Internet
Author: User
Background: Some database operations are performed during the installation script, which can be performed only by the DBA account. We use JSCH to create an SSH channel and directly connect it to the target machine for execution. The effect is the same as opening the command line. The only special issue is the DBA password. Oracle (... SyntaxHighlighter. all (); Background: Some database operations are performed during the installation script, which can only be performed by the DBA account. We use JSCH to create an SSH channel and directly connect it to the target machine for execution. The effect is the same as opening the command line. The only special issue is the DBA password. The Oracle (Oracle for our database) script provides the function of inputting the password together,! I'm afraid of everything! The customer's requirement is: there is no tolerance for plaintext storage of the account and password! OK. To put it bluntly, you cannot write the DBA password in the configuration file and then execute it in batches. You must enter the password when prompted by Oracle. Problem: the original channels provided by JSCH are relatively direct. SFTP, ChannelShell, and ChannelExec are commonly used. In order to take environment variables conveniently, we use ChannelShell. ChannelShell can be used to set InputStream and OutputStream. However, the password can only be entered from the console, so does channelshell. setInputStream (System. in? But other commands are input from the configuration file, so does channelshell. setInputStream (new FileInputStream (filename? In any case, only one InputStream can be set. How can this problem be solved? Investigation: Step 1: google. There are several articles for reference, but they are not suitable. Step 2: Try to use Except4J, but it is also full-script. You cannot switch to System. in. Step 3. Control JSCH input/output stream by yourself according to the Except4J practice. So with this article. Solution: the purpose of talking so much nonsense is to introduce the ins and outs. Some heroes may find that I have done a repetitive task and a better solution, please give me some advice. If you are also troubled by this problem, let's take a look and discuss it together. My method is: Write InputStream and OutputStream by yourself, and then set them to the channelshell of JSCH so that it will read and write from here, I read it from my file when I read it. I told it to input "xyz" and it will get xyz. When I write it, it will be written to my outputstream, I knew the result was ABC. With the above two prerequisites, I will add some minor functions: 1. when I read the file, I wrote more configuration information and found that the current password is used. in reads the data and sends it to JSCH channel; 2. when I get response, I will determine whether the result is the same as I expected, and then I will execute the next one. The configuration file [java] [html] # This file format just for POC, you can define yours. # For my format, it is: #1. # starts for comments #2. there are 3 parts :[ ] [ ] ## 3. , It's easy: [$] means you have CT a' $ 'ends; #4. , Support 'send' and 'Password'. # If 'send', means Will be sent to SSH channel. # If 'Password', means Come from system console, that is, need you type in. [$] send [pwd] [$] send [whoami] [$] send [su-root] [Password:] password [] [#] send [pwd] [#] send [ls] [#] send [whoami] [#] send [exit] [$] send [ls] [$] send [pwd] [$] send [whoami] [$] send [su-root] [Password:] password [] [#] send [pwd] [#] send [ls] [#] send [whoami] [#] send [exit] [$] send [exit] principle is now, it's easy to work hard.
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.