date change shell script in java code


 
Thread Tools Search this Thread
Top Forums Programming date change shell script in java code
# 1  
Old 09-19-2011
Error date change shell script in java code

hi,
I have a code to connect to a remote Linux server through windows using SSH, with host-name, username and password built in the code.

I want to add a shell script in the code which should execute automatically without any user intervention.
(as u know that date change requires you/us to be in SU mode , with the SU password embedded in the script)

the code that i have is:
Code:
/*
 * @(#)SshExample.java
 *
 * Copyright (c) 2004 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */
import com.jscape.inet.ssh.*;
import com.jscape.inet.ssh.util.SshParameters;
import java.io.*;
import java.lang.*;

public class SshExample implements SshListener {
    
    // state of SSH connection
    private boolean connected = false;

    /**
     * Creates a new SshExample instance.
     *
     */
    public SshExample() {
        String hostname = null;
        String username = null;
        String password = null;
        Ssh ssh = null;        

        try {
            BufferedReader bin =
                new BufferedReader(new InputStreamReader(System.in));
            
            hostname = "119.82.103.212";

            username = "prasen";


            password = "tclindia";
            
            


            // create new Ssh instance
            SshParameters params = new SshParameters(hostname,username,password);
            ssh = new Ssh(params);
            
            // register to capture events
            ssh.addSshListener(this);
            
            System.out.println("Connecting please wait...");
            
            // connect
            ssh.connect();
            
            
            
                Process proc = Runtime.getRuntime().exec("ls"); 
                System.out.println("Print Test Line."); 
                 

            // get output stream for writing data to SSH server
            OutputStream out = ssh.getOutputStream();

            // holds line entered at console
            String line = null;

            // read data from console
            while (connected && (line = bin.readLine()) != null) {
                // send line with LF to SSH server
                line += "\n";
                try {
                  out.write(line.getBytes());
                  out.flush();
                } catch(Exception ioe){
                  connected = false;
                }    
            }            
        } catch (Exception e) {
            e.printStackTrace();            
        } finally {
            try {
                if(connected) {
                  ssh.disconnect();    
                }
            } catch(Exception e) {
                
            }            
        }
    }

    /**
     * Captures SshConnectedEvent
     */
    public void connected(SshConnectedEvent ev) {
        System.out.println("Connected: " + ev.getHost());
        connected = true;
    }

    /**
     * Captures SshDataReceivedEvent
     */
    public void dataReceived(SshDataReceivedEvent ev) {
        // send data received to console
        System.out.print(ev.getData());
    }

    /**
     * Captures SshDisconnectedEvent
     */
    public void disconnected(SshDisconnectedEvent ev) {
        System.out.println("Disconnected: " + ev.getHost() + ". Press Enter to exit");
        connected = false;
    }

    /**
     * Main method for SshExample
     * @param args
     */
    public static void main(String[] args) {
        SshExample test = new SshExample();
    }

}


Last edited by Scott; 09-19-2011 at 05:13 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Username & password through shell script to java code

Hi, I have a shell script (script.sh) in which we are calling java code which asks for Username: Password: for authentication purpose currently we are passing the credential manually and run the script. but I am trying echo -e "user_id\npassword" | script.sh but its not... (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

2. Solaris

How to change the date automatically on script?

Hello Experts, There is a log file which has date stamp, I just wanted to change the date automatically on daily basis when it runs. Tried the below, but no luck. grep -i error /var/bv1to1_var/logs/bv03.bectondickinson.com/bvlog.out.`date +\%Y\%m\%d` | tee error_bv03.doc I would highly... (10 Replies)
Discussion started by: seenuvasan1985
10 Replies

3. Shell Programming and Scripting

Shell script with Java code in Cron

Dear All, I have a shell script which has JAVA code inside it runs perfectly on command line. But while I set it on cron, all rest of the script runs but Java code doesnt. java -cp... (3 Replies)
Discussion started by: Deei
3 Replies

4. Shell Programming and Scripting

Change date format in shell script

Plz help me To display date in the mm/dd/yyyy. Eg. if date is 28-09-2012 the output of the shell script should be date 09/28/2012. (1 Reply)
Discussion started by: shivasaini
1 Replies

5. UNIX for Dummies Questions & Answers

Script Shell in java code

Hello, I try to run a script shell from a java program: but it runs only if i do :chmod 777 myShellScript in the terminal Please how can i insert chmod 777 in my java code without going through the terminal? Thank you (1 Reply)
Discussion started by: chercheur857
1 Replies

6. Programming

Script shell in java code

Hello, Please i want to insert this code in a java program because i need to call a java function inside the while: Please how can i do? thank you so much (9 Replies)
Discussion started by: chercheur857
9 Replies

7. Shell Programming and Scripting

Code java in script shell

Hello; Is it possible to insert Java code in a shell script, if so how please? Thank you (0 Replies)
Discussion started by: chercheur857
0 Replies

8. Programming

Script Shell in java code

Hello, This is my script shell: echo Mon premier script echo Liste des fichiers : ls -la exit 0 This is my code java: public class test { public static void main(String args) { try { Process process = Runtime.getRuntime().exec("sh script1.sh"); } catch... (2 Replies)
Discussion started by: chercheur857
2 Replies

9. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

10. Shell Programming and Scripting

getting date from shell and pass it to java

Hi, I have a variable called asOfDate in shell script. I need to pass it as a command line argument to a java command which will be called from the same shell script. The format of that variable is "MM/DD/YYYY" while doing echo, it is printing correctly in the java command. ie.... (0 Replies)
Discussion started by: vanathi
0 Replies
Login or Register to Ask a Question