Exemple #1
0
        public static void Main(String[] arg)
        {
            try
            {
                //Create a new JSch instance
                JSch jsch = new JSch();

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  Environment.UserName + "@localhost");
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                //Create a new SSH session
                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);

                //Add AES128 as default cipher in the session config store
                System.Collections.Hashtable config = new System.Collections.Hashtable();
                config.Add("cipher.s2c", "aes128-cbc,3des-cbc");
                config.Add("cipher.c2s", "aes128-cbc,3des-cbc");
                session.setConfig(config);

                //Connect to remote SSH server
                session.connect();

                //Open a new Shell channel on the SSH session
                Channel channel = session.openChannel("shell");

                //Redirect standard I/O to the SSH channel
                channel.setInputStream(Console.OpenStandardInput());
                channel.setOutputStream(Console.OpenStandardOutput());

                //Connect the channel
                channel.connect();

                Console.WriteLine("-- Shell channel is connected using the {0} cipher",
                                  session.getCipher());

                //Wait till channel is closed
                while (!channel.isClosed())
                {
                    System.Threading.Thread.Sleep(500);
                }

                //Disconnect from remote server
                channel.disconnect();
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #2
0
        public static void Main(String[] arg)
        {
            try
            {
                JSch jsch = new JSch();

                //Get the private key filename from the user
                Console.WriteLine("Please choose your private key file...");
                String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
                Console.WriteLine("You chose " + file + ".");

                //Add the identity file to JSch
                jsch.addIdentity(file);

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  Environment.UserName + "@localhost");
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                //Create a new SSH session
                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);

                //Connect to remote SSH server
                session.connect();

                //Open a new Shell channel on the SSH session
                Channel channel = session.openChannel("shell");

                //Redirect standard I/O to the SSH channel
                channel.setInputStream(Console.OpenStandardInput());
                channel.setOutputStream(Console.OpenStandardOutput());

                //Connect the channel
                channel.connect();

                //Wait till channel is closed
                while (!channel.isClosed())
                {
                    System.Threading.Thread.Sleep(500);
                }

                //Disconnect from remote server
                channel.disconnect();
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void RunExample(String[] arg)
        {
            try
            {
                JSch jsch=new JSch();

                OpenFileDialog chooser = new OpenFileDialog();
                chooser.Title ="Choose your privatekey(ex. ~/.ssh/id_dsa)";
                //chooser.setFileHidingEnabled(false);
                DialogResult returnVal = chooser.ShowDialog();
                if(returnVal == DialogResult.OK)
                {
                    Console.WriteLine("You chose "+
                        chooser.FileName+".");
                    jsch.addIdentity(chooser.FileName
                        //			 , "passphrase"
                        );
                }
                else
                {
                    Console.WriteLine("Error getting key file...");
                    return;
                }
                InputForm inForm = new InputForm();
                inForm.Text = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName+"@localhost";

                if (inForm.PromptForInput())
                {
                    String host = inForm.textBox1.Text;
                    String user=host.Substring(0, host.IndexOf('@'));
                    host=host.Substring(host.IndexOf('@')+1);

                    Session session=jsch.getSession(user, host, 22);

                    // username and passphrase will be given via UserInfo interface.
                    UserInfo ui=new MyUserInfo();
                    session.setUserInfo(ui);
                    session.connect();

                    Channel channel=session.openChannel("shell");

                    channel.setInputStream(Console.OpenStandardInput());
                    channel.setOutputStream(Console.OpenStandardOutput());

                    channel.connect();
                }
                inForm.Close();

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #4
0
        public static void RunExample(String[] arg)
        {
            if(arg.Length<3)
            {
                Console.Error.WriteLine(
                    "usage: java KeyGen rsa output_keyfile comment\n"+
                    "       java KeyGen dsa  output_keyfile comment");
                Environment.Exit(-1);
            }
            String _type=arg[0];
            int type=0;
            if(_type.Equals("rsa")){type=KeyPair.RSA;}
            else if(_type.Equals("dsa")){type=KeyPair.DSA;}
            else
            {
                Console.Error.WriteLine(
                    "usage: java KeyGen rsa output_keyfile comment\n"+
                    "       java KeyGen dsa  output_keyfile comment");
                Environment.Exit(-1);
            }
            String filename=arg[1];
            String comment=arg[2];

            JSch jsch=new JSch();

            String passphrase="";
            InputForm passphraseField=new InputForm();
            passphraseField.PasswordField = true;
            Object[] ob={passphraseField};

                passphraseField.Text="Enter passphrase (empty for no passphrase)";
                bool result=passphraseField.PromptForInput();
            if(result)
            {
                passphrase=passphraseField.getText();
            }

            try
            {
                KeyPair kpair=KeyPair.genKeyPair(jsch, type);
                kpair.setPassphrase(passphrase);
                kpair.writePrivateKey(filename);
                kpair.writePublicKey(filename+".pub", comment);
                Console.WriteLine("Finger print: "+kpair.getFingerPrint());
                kpair.dispose();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            Environment.Exit(0);
        }
Exemple #5
0
        public static void Main(String[] arg)
        {
            //Get the private key filename from the user
            Console.WriteLine("Please choose your private key file...");
            String pkey = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");

            Console.WriteLine("You chose " + pkey + ".");

            //Create a new JSch instance
            JSch jsch = new JSch();

            try
            {
                //Load the key pair
                KeyPair kpair = KeyPair.load(jsch, pkey);

                //Print the key file encryption status
                Console.WriteLine(pkey + " has " + (kpair.isEncrypted()?"been ":"not been ") + "encrypted");

                String passphrase = "";

                while (kpair.isEncrypted())
                {
                    passphrase = InputForm.GetUserInput("Enter passphrase for " + pkey, true);
                    if (!kpair.decrypt(passphrase))
                    {
                        Console.WriteLine("failed to decrypt " + pkey);
                    }
                    else
                    {
                        Console.WriteLine(pkey + " is decrypted.");
                    }
                }

                passphrase = "";

                passphrase = InputForm.GetUserInput("Enter new passphrase for " + pkey +
                                                    " (empty for no passphrase)", true);

                //Set the new passphrase
                kpair.setPassphrase(passphrase);
                //write the key to file
                kpair.writePrivateKey(pkey);
                //free the resource
                kpair.dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #6
0
        public static void Main(String[] arg)
        {
            int port;

            try
            {
                //Create a new JSch instance
                JSch jsch = new JSch();

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  Environment.UserName + "@localhost");
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                //Create a new SSH session
                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);
                session.connect();

                //Get from user the remote host and remote host port
                String foo = InputForm.GetUserInput("Enter host and port", "host:port");
                host = foo.Substring(0, foo.IndexOf(':'));
                port = int.Parse(foo.Substring(foo.IndexOf(':') + 1));

                Console.WriteLine("System.{in,out} will be forwarded to " +
                                  host + ":" + port + ".");
                Channel channel = session.openChannel("direct-tcpip");
                ((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
                ((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
                ((ChannelDirectTCPIP)channel).setHost(host);
                ((ChannelDirectTCPIP)channel).setPort(port);
                channel.connect();

                while (!channel.isClosed())
                {
                    System.Threading.Thread.Sleep(500);
                }
                channel.disconnect();
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
            public bool promptPassword(String message)
            {
                InputForm passwordField = new InputForm();

                passwordField.Text          = message;
                passwordField.PasswordField = true;

                if (passwordField.PromptForInput())
                {
                    passwd = passwordField.getText();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Exemple #8
0
        public static void Main(String[] arg)
        {
            int port;

            try
            {
                //Create a new JSch instance
                JSch jsch = new JSch();

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  Environment.UserName + "@localhost");
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                //Create a new SSH session
                Session session = jsch.getSession(user, host, 22);

                //Get from user the remote port, local host and local host port
                String foo   = InputForm.GetUserInput("Enter -R port:host:hostport", "port:host:hostport");
                int    rport = int.Parse(foo.Substring(0, foo.IndexOf(':')));
                foo = foo.Substring(foo.IndexOf(':') + 1);
                String lhost = foo.Substring(0, foo.IndexOf(':'));
                int    lport = int.Parse(foo.Substring(foo.IndexOf(':') + 1));

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);
                session.connect();

                Console.WriteLine(host + ":" + rport + " -> " + lhost + ":" + lport);

                //Set port forwarding on the opened session
                session.setPortForwardingR(rport, lhost, lport);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #9
0
        public static void Main(String[] arg)
        {
            try
            {
                JSch jsch=new JSch();

                InputForm inForm = new InputForm();
                inForm.Text = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName+"@localhost";

                if (!inForm.PromptForInput())
                {
                    Console.WriteLine("Cancelled");
                    return;
                }
                String host = inForm.textBox1.Text;
                String user=host.Substring(0, host.IndexOf('@'));
                host=host.Substring(host.IndexOf('@')+1);

                Session session=jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui=new MyUserInfo();
                session.setUserInfo(ui);

                session.connect();

                Channel channel=session.openChannel("sftp");
                channel.connect();
                ChannelSftp c=(ChannelSftp)channel;

                Stream ins=Console.OpenStandardInput();
                TextWriter outs=Console.Out;

                ArrayList cmds=new ArrayList();
                byte[] buf=new byte[1024];
                int i;
                String str;
                int level=0;

                while(true)
                {
                    outs.Write("sftp> ");
                    cmds.Clear();
                    i=ins.Read(buf, 0, 1024);
                    if(i<=0)break;

                    i--;
                    if(i>0 && buf[i-1]==0x0d)i--;
                    //str=Util.getString(buf, 0, i);
                    //Console.WriteLine("|"+str+"|");
                    int s=0;
                    for(int ii=0; ii<i; ii++)
                    {
                        if(buf[ii]==' ')
                        {
                            if(ii-s>0){ cmds.Add(Util.getString(buf, s, ii-s)); }
                            while(ii<i){if(buf[ii]!=' ')break; ii++;}
                            s=ii;
                        }
                    }
                    if(s<i){ cmds.Add(Util.getString(buf, s, i-s)); }
                    if(cmds.Count==0)continue;

                    String cmd=(String)cmds[0];
                    if(cmd.Equals("quit"))
                    {
                        c.quit();
                        break;
                    }
                    if(cmd.Equals("exit"))
                    {
                        c.exit();
                        break;
                    }
                    if(cmd.Equals("rekey"))
                    {
                        session.rekey();
                        continue;
                    }
                    if(cmd.Equals("compression"))
                    {
                        if(cmds.Count<2)
                        {
                            outs.WriteLine("compression level: "+level);
                            continue;
                        }
                        try
                        {
                            level=int.Parse((String)cmds[1]);
                            Hashtable config=new Hashtable();
                            if(level==0)
                            {
                                config.Add("compression.s2c", "none");
                                config.Add("compression.c2s", "none");
                            }
                            else
                            {
                                config.Add("compression.s2c", "zlib,none");
                                config.Add("compression.c2s", "zlib,none");
                            }
                            session.setConfig(config);
                        }
                        catch{}//(Exception e){}
                        continue;
                    }
                    if(cmd.Equals("cd") || cmd.Equals("lcd"))
                    {
                        if(cmds.Count<2) continue;
                        String path=(String)cmds[1];
                        try
                        {
                            if(cmd.Equals("cd")) c.cd(path);
                            else c.lcd(path);
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
                    {
                        if(cmds.Count<2) continue;
                        String path=(String)cmds[1];
                        try
                        {
                            if(cmd.Equals("rm")) c.rm(path);
                            else if(cmd.Equals("rmdir")) c.rmdir(path);
                            else c.mkdir(path);
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
                    {
                        if(cmds.Count!=3) continue;
                        String path=(String)cmds[2];
                        int foo=0;
                        if(cmd.Equals("chmod"))
                        {
                            byte[] bar=Util.getBytes((String)cmds[1]);
                            int k;
                            for(int j=0; j<bar.Length; j++)
                            {
                                k=bar[j];
                                if(k<'0'||k>'7'){foo=-1; break;}
                                foo<<=3;
                                foo|=(k-'0');
                            }
                            if(foo==-1)continue;
                        }
                        else
                        {
                            try{foo=int.Parse((String)cmds[1]);}
                            catch{}//(Exception e){continue;}
                        }
                        try
                        {
                            if(cmd.Equals("chgrp")){ c.chgrp(foo, path); }
                            else if(cmd.Equals("chown")){ c.chown(foo, path); }
                            else if(cmd.Equals("chmod")){ c.chmod(foo, path); }
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("pwd") || cmd.Equals("lpwd"))
                    {
                        str=(cmd.Equals("pwd")?"Remote":"Local");
                        str+=" working directory: ";
                        if(cmd.Equals("pwd")) str+=c.pwd();
                        else str+=c.lpwd();
                        outs.WriteLine(str);
                        continue;
                    }
                    if(cmd.Equals("ls") || cmd.Equals("dir"))
                    {
                        String path=".";
                        if(cmds.Count==2) path=(String)cmds[1];
                        try
                        {
                            ArrayList vv=c.ls(path);
                            if(vv!=null)
                            {
                                for(int ii=0; ii<vv.Count; ii++)
                                {
                                    object obj = vv[ii];
                                    if(obj is ChannelSftp.LsEntry)
                                        outs.WriteLine(vv[ii]);
                                }
                            }
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("lls") || cmd.Equals("ldir"))
                    {
                        String path=".";
                        if(cmds.Count==2) path=(String)cmds[1];
                        try
                        {
                            //java.io.File file=new java.io.File(path);
                            if(!File.Exists(path))
                            {
                                outs.WriteLine(path+": No such file or directory");
                                continue;
                            }
                            if(Directory.Exists(path))
                            {
                                String[] list=Directory.GetDirectories(path);
                                for(int ii=0; ii<list.Length; ii++)
                                {
                                    outs.WriteLine(list[ii]);
                                }
                                continue;
                            }
                            outs.WriteLine(path);
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        continue;
                    }
                    if(cmd.Equals("get") ||
                        cmd.Equals("get-resume") || cmd.Equals("get-append") ||
                        cmd.Equals("put") ||
                        cmd.Equals("put-resume") || cmd.Equals("put-append")
                        )
                    {
                        if(cmds.Count!=2 && cmds.Count!=3) continue;
                        String p1=(String)cmds[1];
                        //	  String p2=p1;
                        String p2=".";
                        if(cmds.Count==3)p2=(String)cmds[2];
                        try
                        {
                            SftpProgressMonitor monitor=new MyProgressMonitor();
                            if(cmd.StartsWith("get"))
                            {
                                int mode=ChannelSftp.OVERWRITE;
                                if(cmd.Equals("get-resume")){ mode=ChannelSftp.RESUME; }
                                else if(cmd.Equals("get-append")){ mode=ChannelSftp.APPEND; }
                                c.get(p1, p2, monitor, mode);
                            }
                            else
                            {
                                int mode=ChannelSftp.OVERWRITE;
                                if(cmd.Equals("put-resume")){ mode=ChannelSftp.RESUME; }
                                else if(cmd.Equals("put-append")){ mode=ChannelSftp.APPEND; }
                                c.put(p1, p2, monitor, mode);
                            }
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("ln") || cmd.Equals("symlink") || cmd.Equals("rename"))
                    {
                        if(cmds.Count!=3) continue;
                        String p1=(String)cmds[1];
                        String p2=(String)cmds[2];
                        try
                        {
                            if(cmd.Equals("rename")) c.rename(p1, p2);
                            else c.symlink(p1, p2);
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if(cmd.Equals("stat") || cmd.Equals("lstat"))
                    {
                        if(cmds.Count!=2) continue;
                        String p1=(String)cmds[1];
                        SftpATTRS attrs=null;
                        try
                        {
                            if(cmd.Equals("stat")) attrs=c.stat(p1);
                            else attrs=c.lstat(p1);
                        }
                        catch(SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        if(attrs!=null)
                        {
                            outs.WriteLine(attrs);
                        }
                        else
                        {
                        }
                        continue;
                    }
                    if(cmd.Equals("version"))
                    {
                        outs.WriteLine("SFTP protocol version "+c.version());
                        continue;
                    }
                    if(cmd.Equals("help") || cmd.Equals("?"))
                    {
                        outs.WriteLine(help);
                        continue;
                    }
                    outs.WriteLine("unimplemented command: "+cmd);
                }
                session.disconnect();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #10
0
            public bool promptPassword(String message)
            {
                InputForm inForm = new InputForm();
                inForm.Text = message;
                inForm.PasswordField = true;

                if ( inForm.PromptForInput() )
                {
                    passwd=inForm.getText();
                    return true;
                }
                else{ return false; }
            }
Exemple #11
0
            public bool promptPassword(String message)
            {
                InputForm passwordField=new InputForm();
                passwordField.Text = message;
                passwordField.PasswordField = true;

                if ( passwordField.PromptForInput() )
                {
                    passwd=passwordField.getText();
                    return true;
                }
                else{ return false; }
            }
Exemple #12
0
 /// <summary>
 /// Shows a message to the user
 /// </summary>
 public void showMessage(String message)
 {
     InputForm.ShowMessage(message);
 }
Exemple #13
0
 /// <summary>
 /// Prompt the user for a password
 /// </summary>\
 public bool promptPassword(String message)
 {
     passwd = InputForm.GetUserInput(message, true);
     return(true);
 }
Exemple #14
0
 /// <summary>
 /// Prompt the user for a Yes/No input
 /// </summary>
 public bool promptYesNo(String str)
 {
     return(InputForm.PromptYesNo(str));
 }
Exemple #15
0
        public static void Main(params string[] arg)
        {
            if (arg.Length < 3)
            {
                Console.Error.WriteLine(
                    "usage: java KeyGen rsa output_keyfile comment\n" +
                    "       java KeyGen dsa  output_keyfile comment");
                Environment.Exit(-1);
            }

            //Get sig type ('rsa' or 'dsa')
            String _type = arg[0];
            int    type  = 0;

            if (_type.Equals("rsa"))
            {
                type = KeyPair.RSA;
            }
            else if (_type.Equals("dsa"))
            {
                type = KeyPair.DSA;
            }
            else
            {
                Console.Error.WriteLine(
                    "usage: java KeyGen rsa output_keyfile comment\n" +
                    "       java KeyGen dsa  output_keyfile comment");
                Environment.Exit(-1);
            }
            //Output file name
            String filename = arg[1];
            //Signature comment
            String comment = arg[2];

            //Create a new JSch instance
            JSch jsch = new JSch();

            //Prompt the user for a passphrase for the private key file
            String passphrase = InputForm.GetUserInput("Enter passphrase (empty for no passphrase)", true);

            try
            {
                //Generate the new key pair
                KeyPair kpair = KeyPair.genKeyPair(jsch, type);
                //Set a passphrase
                kpair.setPassphrase(passphrase);
                //Write the private key to "filename"
                kpair.writePrivateKey(filename);
                //Write the public key to "filename.pub"
                kpair.writePublicKey(filename + ".pub", comment);
                //Print the key fingerprint
                Console.WriteLine("Finger print: " + kpair.getFingerPrint());
                //Free resources
                kpair.dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Environment.Exit(0);
        }
        public static void RunExample(String[] arg)
        {
            try
            {
                JSch jsch=new JSch();

                OpenFileDialog chooser = new OpenFileDialog();
                chooser.Title = "Choose your known_hosts(ex. ~/.ssh/known_hosts)";
                //chooser.setFileHidingEnabled(false);
                DialogResult returnVal = chooser.ShowDialog();
                if(returnVal == DialogResult.OK)
                {
                    Console.WriteLine("You chose "+
                        chooser.FileName+".");
                    jsch.setKnownHosts(chooser.FileName);
                }
                else
                {
                    Console.WriteLine("Error getting host file...");
                    return;
                }

                HostKeyRepository hkr=jsch.getHostKeyRepository();
                HostKey[] hks=hkr.getHostKey();
                if(hks!=null)
                {
                    Console.WriteLine("Host keys in "+hkr.getKnownHostsRepositoryID());
                    for(int i=0; i<hks.Length; i++)
                    {
                        HostKey hk=hks[i];
                        Console.WriteLine(hk.getHost()+" "+
                            hk.getType()+" "+
                            hk.getFingerPrint(jsch));
                    }
                    Console.WriteLine("");
                }

                InputForm inForm = new InputForm();
                inForm.Text = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName+"@localhost";

                if (inForm.PromptForInput())
                {
                    String host = inForm.textBox1.Text;
                    String user=host.Substring(0, host.IndexOf('@'));
                    host=host.Substring(host.IndexOf('@')+1);

                    Session session=jsch.getSession(user, host, 22);

                    // username and password will be given via UserInfo interface.
                    UserInfo ui=new MyUserInfo();
                    session.setUserInfo(ui);

                    session.connect();

                    HostKey hk=session.getHostKey();
                    Console.WriteLine("HostKey: "+
                        hk.getHost()+" "+
                        hk.getType()+" "+
                        hk.getFingerPrint(jsch));

                    Channel channel=session.openChannel("shell");

                    channel.setInputStream(Console.OpenStandardInput());
                    channel.setOutputStream(Console.OpenStandardOutput());

                    channel.connect();
                }
                inForm.Close();

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void RunExample(String[] arg)
        {
            if(arg.Length!=1)
            {
                Console.WriteLine("usage: java ChangePassphrase private_key");
                Environment.Exit(-1);
            }

            JSch jsch=new JSch();

            String pkey=arg[0];

            try
            {
                KeyPair kpair=KeyPair.load(jsch, pkey);

                Console.WriteLine(pkey+" has "+(kpair.isEncrypted()?"been ":"not been ")+"encrypted");

                String passphrase="";
                InputForm passphraseField = null;

                while(kpair.isEncrypted())
                {
                    passphraseField=new InputForm();
                    passphraseField.PasswordField = true;

                    if(!passphraseField.PromptForInput())
                    {
                        Environment.Exit(-1);
                    }
                    passphrase=passphraseField.getText();
                    if(!kpair.decrypt(passphrase))
                    {
                        Console.WriteLine("failed to decrypt "+pkey);
                    }
                    else
                    {
                        Console.WriteLine(pkey+" is decrypted.");
                    }
                }

                passphrase="";

                passphraseField=new InputForm();
                passphraseField.PasswordField = true;

                if(!passphraseField.PromptForInput())
                {
                    Environment.Exit(-1);
                }
                passphrase=passphraseField.getText();

                kpair.setPassphrase(passphrase);
                kpair.writePrivateKey(pkey);
                kpair.dispose();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            Environment.Exit(0);
        }
Exemple #18
0
        public static void Main(String[] arg)
        {
            try
            {
                JSch jsch = new JSch();

                InputForm inForm = new InputForm();
                inForm.Text          = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName + "@localhost";

                if (!inForm.PromptForInput())
                {
                    Console.WriteLine("Cancelled");
                    return;
                }
                String host = inForm.textBox1.Text;
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);

                session.connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp c = (ChannelSftp)channel;

                Stream     ins  = Console.OpenStandardInput();
                TextWriter outs = Console.Out;

                ArrayList cmds = new ArrayList();
                byte[]    buf  = new byte[1024];
                int       i;
                String    str;
                int       level = 0;

                while (true)
                {
                    outs.Write("sftp> ");
                    cmds.Clear();
                    i = ins.Read(buf, 0, 1024);
                    if (i <= 0)
                    {
                        break;
                    }

                    i--;
                    if (i > 0 && buf[i - 1] == 0x0d)
                    {
                        i--;
                    }
                    //str=Util.getString(buf, 0, i);
                    //Console.WriteLine("|"+str+"|");
                    int s = 0;
                    for (int ii = 0; ii < i; ii++)
                    {
                        if (buf[ii] == ' ')
                        {
                            if (ii - s > 0)
                            {
                                cmds.Add(Util.getString(buf, s, ii - s));
                            }
                            while (ii < i)
                            {
                                if (buf[ii] != ' ')
                                {
                                    break;
                                }
                                ii++;
                            }
                            s = ii;
                        }
                    }
                    if (s < i)
                    {
                        cmds.Add(Util.getString(buf, s, i - s));
                    }
                    if (cmds.Count == 0)
                    {
                        continue;
                    }

                    String cmd = (String)cmds[0];
                    if (cmd.Equals("quit"))
                    {
                        c.quit();
                        break;
                    }
                    if (cmd.Equals("exit"))
                    {
                        c.exit();
                        break;
                    }
                    if (cmd.Equals("rekey"))
                    {
                        session.rekey();
                        continue;
                    }
                    if (cmd.Equals("compression"))
                    {
                        if (cmds.Count < 2)
                        {
                            outs.WriteLine("compression level: " + level);
                            continue;
                        }
                        try
                        {
                            level = int.Parse((String)cmds[1]);
                            Hashtable config = new Hashtable();
                            if (level == 0)
                            {
                                config.Add("compression.s2c", "none");
                                config.Add("compression.c2s", "none");
                            }
                            else
                            {
                                config.Add("compression.s2c", "zlib,none");
                                config.Add("compression.c2s", "zlib,none");
                            }
                            session.setConfig(config);
                        }
                        catch {}                       //(Exception e){}
                        continue;
                    }
                    if (cmd.Equals("cd") || cmd.Equals("lcd"))
                    {
                        if (cmds.Count < 2)
                        {
                            continue;
                        }
                        String path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("cd"))
                            {
                                c.cd(path);
                            }
                            else
                            {
                                c.lcd(path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
                    {
                        if (cmds.Count < 2)
                        {
                            continue;
                        }
                        String path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("rm"))
                            {
                                c.rm(path);
                            }
                            else if (cmd.Equals("rmdir"))
                            {
                                c.rmdir(path);
                            }
                            else
                            {
                                c.mkdir(path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
                    {
                        if (cmds.Count != 3)
                        {
                            continue;
                        }
                        String path = (String)cmds[2];
                        int    foo  = 0;
                        if (cmd.Equals("chmod"))
                        {
                            byte[] bar = Util.getBytes((String)cmds[1]);
                            int    k;
                            for (int j = 0; j < bar.Length; j++)
                            {
                                k = bar[j];
                                if (k < '0' || k > '7')
                                {
                                    foo = -1; break;
                                }
                                foo <<= 3;
                                foo  |= (k - '0');
                            }
                            if (foo == -1)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            try{ foo = int.Parse((String)cmds[1]); }
                            catch {}                           //(Exception e){continue;}
                        }
                        try
                        {
                            if (cmd.Equals("chgrp"))
                            {
                                c.chgrp(foo, path);
                            }
                            else if (cmd.Equals("chown"))
                            {
                                c.chown(foo, path);
                            }
                            else if (cmd.Equals("chmod"))
                            {
                                c.chmod(foo, path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("pwd") || cmd.Equals("lpwd"))
                    {
                        str  = (cmd.Equals("pwd")?"Remote":"Local");
                        str += " working directory: ";
                        if (cmd.Equals("pwd"))
                        {
                            str += c.pwd();
                        }
                        else
                        {
                            str += c.lpwd();
                        }
                        outs.WriteLine(str);
                        continue;
                    }
                    if (cmd.Equals("ls") || cmd.Equals("dir"))
                    {
                        String path = ".";
                        if (cmds.Count == 2)
                        {
                            path = (String)cmds[1];
                        }
                        try
                        {
                            ArrayList vv = c.ls(path);
                            if (vv != null)
                            {
                                for (int ii = 0; ii < vv.Count; ii++)
                                {
                                    object obj = vv[ii];
                                    if (obj is ChannelSftp.LsEntry)
                                    {
                                        outs.WriteLine(vv[ii]);
                                    }
                                }
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("lls") || cmd.Equals("ldir"))
                    {
                        String path = ".";
                        if (cmds.Count == 2)
                        {
                            path = (String)cmds[1];
                        }
                        try
                        {
                            //java.io.File file=new java.io.File(path);
                            if (!File.Exists(path))
                            {
                                outs.WriteLine(path + ": No such file or directory");
                                continue;
                            }
                            if (Directory.Exists(path))
                            {
                                String[] list = Directory.GetDirectories(path);
                                for (int ii = 0; ii < list.Length; ii++)
                                {
                                    outs.WriteLine(list[ii]);
                                }
                                continue;
                            }
                            outs.WriteLine(path);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        continue;
                    }
                    if (cmd.Equals("get") ||
                        cmd.Equals("get-resume") || cmd.Equals("get-append") ||
                        cmd.Equals("put") ||
                        cmd.Equals("put-resume") || cmd.Equals("put-append")
                        )
                    {
                        if (cmds.Count != 2 && cmds.Count != 3)
                        {
                            continue;
                        }
                        String p1 = (String)cmds[1];
                        //	  String p2=p1;
                        String p2 = ".";
                        if (cmds.Count == 3)
                        {
                            p2 = (String)cmds[2];
                        }
                        try
                        {
                            SftpProgressMonitor monitor = new MyProgressMonitor();
                            if (cmd.StartsWith("get"))
                            {
                                int mode = ChannelSftp.OVERWRITE;
                                if (cmd.Equals("get-resume"))
                                {
                                    mode = ChannelSftp.RESUME;
                                }
                                else if (cmd.Equals("get-append"))
                                {
                                    mode = ChannelSftp.APPEND;
                                }
                                c.get(p1, p2, monitor, mode);
                            }
                            else
                            {
                                int mode = ChannelSftp.OVERWRITE;
                                if (cmd.Equals("put-resume"))
                                {
                                    mode = ChannelSftp.RESUME;
                                }
                                else if (cmd.Equals("put-append"))
                                {
                                    mode = ChannelSftp.APPEND;
                                }
                                c.put(p1, p2, monitor, mode);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("ln") || cmd.Equals("symlink") || cmd.Equals("rename"))
                    {
                        if (cmds.Count != 3)
                        {
                            continue;
                        }
                        String p1 = (String)cmds[1];
                        String p2 = (String)cmds[2];
                        try
                        {
                            if (cmd.Equals("rename"))
                            {
                                c.rename(p1, p2);
                            }
                            else
                            {
                                c.symlink(p1, p2);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("stat") || cmd.Equals("lstat"))
                    {
                        if (cmds.Count != 2)
                        {
                            continue;
                        }
                        String    p1    = (String)cmds[1];
                        SftpATTRS attrs = null;
                        try
                        {
                            if (cmd.Equals("stat"))
                            {
                                attrs = c.stat(p1);
                            }
                            else
                            {
                                attrs = c.lstat(p1);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        if (attrs != null)
                        {
                            outs.WriteLine(attrs);
                        }
                        else
                        {
                        }
                        continue;
                    }
                    if (cmd.Equals("version"))
                    {
                        outs.WriteLine("SFTP protocol version " + c.version());
                        continue;
                    }
                    if (cmd.Equals("help") || cmd.Equals("?"))
                    {
                        outs.WriteLine(help);
                        continue;
                    }
                    outs.WriteLine("unimplemented command: " + cmd);
                }
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #19
0
        public static void Main(String[] arg)
        {
            try
            {
                //Get the "known hosts" filename from the user
                Console.WriteLine("Please select your 'known_hosts' from the poup window...");
                String file = InputForm.GetFileFromUser("Choose your known_hosts(ex. ~/.ssh/known_hosts)");
                Console.WriteLine("You chose " + file + ".");
                //Create a new JSch instance
                JSch jsch = new JSch();
                //Set the known hosts file
                jsch.setKnownHosts(file);

                //Get the KnownHosts repository from JSchs
                HostKeyRepository hkr = jsch.getHostKeyRepository();

                //Print all known hosts and keys
                HostKey[] hks = hkr.getHostKey();
                HostKey   hk;
                if (hks != null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Host keys in " + hkr.getKnownHostsRepositoryID() + ":");
                    for (int i = 0; i < hks.Length; i++)
                    {
                        hk = hks[i];
                        Console.WriteLine(hk.getHost() + " " +
                                          hk.getType() + " " +
                                          hk.getFingerPrint(jsch));
                    }
                    Console.WriteLine("");
                }

                //Now connect to the remote server...

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  Environment.UserName + "@localhost");
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                //Create a new SSH session
                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);

                //Connect to remote SSH server
                session.connect();

                //Print the host key info
                //of the connected server:
                hk = session.getHostKey();
                Console.WriteLine("HostKey: " +
                                  hk.getHost() + " " +
                                  hk.getType() + " " +
                                  hk.getFingerPrint(jsch));

                //Open a new Shell channel on the SSH session
                Channel channel = session.openChannel("shell");

                //Redirect standard I/O to the SSH channel
                channel.setInputStream(Console.OpenStandardInput());
                channel.setOutputStream(Console.OpenStandardOutput());

                //Connect the channel
                channel.connect();

                Console.WriteLine("-- Shell channel is connected using the {0} cipher",
                                  session.getCipher());

                //Wait till channel is closed
                while (!channel.isClosed())
                {
                    System.Threading.Thread.Sleep(500);
                }

                //Disconnect from remote server
                channel.disconnect();
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #20
0
 /// <summary>
 /// Prompt the user for a passphrase (passwd for the private key file)
 /// </summary>
 public bool promptPassphrase(String message)
 {
     passphrase = InputForm.GetUserInput(message, true);
     return(true);
 }
            public bool promptPassphrase(String message)
            {
                InputForm inForm = new InputForm();
                inForm.Text = message;
                inForm.PasswordField = true;

                if ( inForm.PromptForInput() )
                {
                    passphrase=inForm.textBox1.Text;
                    inForm.Close();
                    return true;
                }
                else
                {
                    inForm.Close();
                    return false;
                }
            }