網友的工作中SHELL問題:
問題說明:
需要截取以下命令輸出的證書編碼信息:
#openssls_client-showcerts-connect192.168.0.111:443|sed-ne'/-BEGINCERTIFICATE-/,/-ENDCERTIFICATE-/p'(以下是證書的輸出信息,期望:將第一個-----BEGINCERTIFICATE-----到-----ENDCERTIFICATE-----區域間的編碼保存為一個文件,第二個EGINCERTIFICATE-----到-----ENDCERTIFICATE區間保存另外一個文件,同時不能用取行的方式....)命令顯示的內容就是oldboy.log。
近期每周至少講4天課,時間很緊,這裡老男孩老師僅給個簡單實現,其實方法很多的,對不起伙伴們了。
簡單解答:
[root@oldboytraining~]# cat oldboy.log ----BEGINCERTIFICATE----- 1MIIDvDCCAqSgAwIBAgIBCTANBgkqhkiG9w0BAQUFADCBgzELMAkGA1UEBhMCQ04x EDAOBgNVBAgMB0JlaUppbmcxEDAOBgNVBAcMB0JlaUppbmcxETAPBgNVBAoMCENT REEgSUJNMRkwFwYDVQQDDBB2bTE5OC5jbi5pYm0uY29tMSIwIAYJKoZIhvcNAQkB FhNqdW5sdW9iakBjbi5pYm0uY29tMB4XDTEzMDkwMjAyNDE1NVoXDTE0MDkwMjAy NDE1NVowQjELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB0JlaUppbmcxETAPBgNVBAoM CENTREEgSUJNMQ4wDAYDVQQDDAV2bTE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEP ADCCAQoCggEBANr4U+ipiJAfpmw8xTSjhsfItM9+TRWRTuLYetD3oBAb6xVOxD8T 3krBxOT0Uu98+8DytfjJr1J/48BO+A1nxvrVYYWgkgnmsggju/BRt5NBORFR+DPH wjYtu2aULW7ZIy8eIS1BDb5Dk+Axywt3psE5MuI9gVtb7zC6uufQmC0HVS2yhzgk e+CqPuawywvEPxAfY97k1XA43gFqWJYPs1/DY/cbVN2SgBGx3fsQAzVPQJ2c+lBk ZbO79Ps75Ey2hlDfi/9/gMKTnVusvMOJTxh9WuMcCFQeOOTDPc7vd3Fj+XamBaOx g3yx410OMup9J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qhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5j4hS8wJvvzdmmRN Ob8VPrrwNecZU4QFbDWfkzzCDxRHoHIb8Y8OYw9EYB1wbe+CmOCwaBtIJHqhmyJ8 6zLYAt5hCn1rM4SxjZvEtpLoc2EL2bZor6eq2ta5kSnAOwhnioTivu7vwqpwOTDo lGIsmh2qq37rm4k9+fnW8TOY/XRF5JfAIiMlhlgQqvL8//X3c4TzBse4HHtM4JCf C2Wa2mvG9IOPFT0ugMi5bFmYn6GyfY7XO9ZnG2HSat/l2jgveUiS8zOBgf6cqb1B EY2frkBsEfIcqkvpttPLNp/kJbJi8xEylmqxN2p0PuAG5oFA2ANZIPocyYM8dF8u JH1qkQIDAQABo1AwTjAdBgNVHQ4EFgQUGPqupC28Usi0Befhrx6c6OMA6R4wHwYD VR0jBBgwFoAUGPqupC28Usi0Befhrx6c6OMA6R4wDAYDVR0TBAUwAwEB/zANBgkq hkiG9w0BAQsFAAOCAQEAz2yj+8ABofDAj/aYOre7fPmrB/oKjxLFc/d5V/gftbNa ho1noru62d1zVcjlWNwliNEBATpA6mZFz8swxJ4AYHx1lxbAt1p98vHWmKVG8l+e mMeSZ/VVmNU5HCgD+Yb1L//WO4Hjo74y3t/Q8VBa+tydvEA3VuK/CQlU4fFzKp2p QkrqYTKYejr8vBZ8hSmx0w2AsztfS8R0cIPmOP4nzyiccPxyGenGWtmJbVoW+YJR 8DQfE95o4LyYYsFhpDjXPMthxyEmt5eDsVOIF4vG8qXq7r3VUpgZvt6k6GgFNuNz fAXsIQU6b1q0NNssLfLSMOIBdNmIPT4B03m4FS4PTA== -----ENDCERTIFICATE----- [root@oldboytraining~]# END="-----END CERTIFICATE-----" [root@oldboytraining~]# echo ${#END} 25 [root@oldboytraining~]# BEGIN="-----BEGIN CERTIFICATE-----" [root@oldboytraining~]# echo ${#BEGIN} 27 [root@oldboytraining~]# cat oldboy.sh i=1 whileread line do if[ ${#line} -eq 27 ];then echo "$line">>$i.log elif [ ${#line} -eq 25 ];then echo "$line">>$i.log ((i++)) else echo "$line">>$i.log fi done<oldboy.log [root@oldboytraining~]# sh oldboy.sh [root@oldboytraining~]# cat 1.log ----BEGINCERTIFICATE----- 1MIIDvDCCAqSgAwIBAgIBCTANBgkqhkiG9w0BAQUFADCBgzELMAkGA1UEBhMCQ04x EDAOBgNVBAgMB0JlaUppbmcxEDAOBgNVBAcMB0JlaUppbmcxETAPBgNVBAoMCENT REEgSUJNMRkwFwYDVQQDDBB2bTE5OC5jbi5pYm0uY29tMSIwIAYJKoZIhvcNAQkB FhNqdW5sdW9iakBjbi5pYm0uY29tMB4XDTEzMDkwMjAyNDE1NVoXDTE0MDkwMjAy NDE1NVowQjELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB0JlaUppbmcxETAPBgNVBAoM CENTREEgSUJNMQ4wDAYDVQQDDAV2bTE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEP ADCCAQoCggEBANr4U+ipiJAfpmw8xTSjhsfItM9+TRWRTuLYetD3oBAb6xVOxD8T 3krBxOT0Uu98+8DytfjJr1J/48BO+A1nxvrVYYWgkgnmsggju/BRt5NBORFR+DPH wjYtu2aULW7ZIy8eIS1BDb5Dk+Axywt3psE5MuI9gVtb7zC6uufQmC0HVS2yhzgk e+CqPuawywvEPxAfY97k1XA43gFqWJYPs1/DY/cbVN2SgBGx3fsQAzVPQJ2c+lBk ZbO79Ps75Ey2hlDfi/9/gMKTnVusvMOJTxh9WuMcCFQeOOTDPc7vd3Fj+XamBaOx g3yx410OMup9JQnZnLhNm3mFa7988hTSFqcCAwEAAaN7MHkwCQYDVR0TBAIwADAs BglghkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYD VR0OBBYEFFUoqZIIsnS2FheA1eKdZK4WOy3rMB8GA1UdIwQYMBaAFBj6rqQtvFLI tAXn4a8enOjjAOkeMA0GCSqGSIb3DQEBBQUAA4IBAQAz7ahoRKPvtCyU8pwahhu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