3 Windows environment interacts with Shell

Source: Internet
Author: User

 /** * by Sshconfig Configuration get a session * @param conf * @return*/     Public StaticSession createsession (sshconfig conf) {Log.info ("try Connect to"+ conf.gethostip () +", Username:"+ conf.getusername () +", Password:"+ Conf.getpassword () +", Port:"+Conf.getport ()); Jsch Jsch=NewJsch ();//Create a Jsch objectSession session=NULL;//gets a Session object based on the user name, host IP, and port        Try{Session=jsch.getsession (Conf.getusername (), Conf.gethostip (), Conf.getport ()); Session.setpassword (Conf.getpassword ()); //Set PasswordProperties config=NewProperties (); //stricthostkeychecking No//"Stricthostkeychecking" if set to "Yes", SSH will not automatically add the computer's key "$HOME/.ssh/known_hosts" file, and once the computer's key has changed, refused to connectConfig.put ("stricthostkeychecking","No"); Session.setconfig (config);//setting properties for session ObjectsSession.settimeout (Conf.gettimeout ());//Set TimeoutSession.connect ();//Connect via sessionLog.info ("connect to {} success", Conf.gethostip ()); } Catch(jschexception e) {log.error ("connect to {} fail, connect infos:{}", Conf.gethostip (), conf.tostring ());        E.printstacktrace (); }        returnsession; }    /** * Close Session * @param session*/     Public Static voidCloseSession (Session session) {Session.disconnect (); }
 /** * Get local DST file from src file * @param session * @param src * @param dst * @return*/     Public Staticboolean downLoad (Session session,string src,string DST) {Boolean success=true; //SRC Linux Server file address, DST local storage addressChannelsftp channelsftp=NULL; Try{channelsftp= (channelsftp) Session.openchannel ("sftp");            Channelsftp.connect (); Channelsftp.Get(SRC, DST);        Channelsftp.quit (); } Catch(Jschexception e) {Success=false;            E.printstacktrace ();        Log.error (E.getmessage ()); } Catch(Sftpexception e) {
Success=false; E.printstacktrace (); Log.error (E.getmessage ()); } returnsuccess; }

/** * hostname * @param hostip * @param username * @param password * @return for IP username and password */     Public Staticstring GetHostName (String HostIP, string Username, string password) {Session session=createsession (NewSshconfig (Hostip,username,password)); String hostname= Execommand (Session,"hostname"); returnHostname.trim (); }    /** * Execute command to return execution result * @param session * @param command * @return*/     Public StaticString execommand (Session session,string command) {channelexec channelexec=NULL; String out=NULL; Try{channelexec= (channelexec) Session.openchannel ("exec"); InputStreaminch=Channelexec.getinputstream ();            Channelexec.setcommand (command);            Channelexec.seterrstream (System.err);            Channelexec.connect ();  out= Ioutils.tostring (inch,"UTF-8");            Channelexec.disconnect (); //Session.disconnect ();}Catch(jschexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        if(NULL== out) {Log.error ("Execute {} error", command);  out=""; }        return  out; }
 Public classSshconfig {PrivateString HostIP; PrivateString username; PrivateString password; //Default Port    Private intport= A; //10 Hour Timeout    Private inttimeout= +* -* -*Ten;  Publicsshconfig (String hostip, string Username, string password) { This. HostIP =HostIP;  This. Password =password;  This. Username =username; }     PublicString Gethostip () {returnHostIP; }     Public voidSethostip (String hostip) { This. HostIP =HostIP; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     Public intGetport () {returnPort; }     Public voidSetport (intPort) {         This. Port =Port; }     Public intgettimeout () {returnTimeOut; }     Public voidSetTimeOut (intTimeOut) {         This. TimeOut =TimeOut; } @Override Publicstring toString () {string Builder="\nhostip"+" : "+HostIP+"\nusername"+" : "+username+"\npassword"+" : "+Password+"\nport"+" : "+Port+"\ntimeout"+" : "+TimeOut; returnBuilder; }}

3 Windows environment interacts with Shell

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.