/// <summary> /// Constructs a new SSH instance /// </summary> /// <param name="host">The remote SSH host</param> /// <param name="user">The login username</param> /// <param name="password">The login password</param> public SshBase(string host, string user, string password) { Host = host; Username = user; Password = password; m_jsch = new JSch(); }
public static void RunExample(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, SharpSsh.SshBase.SSH_TCP_PORT); string proxy = InputForm.GetUserInput("Enter proxy server", "hostname:port"); string proxy_host = proxy.Substring(0, proxy.IndexOf(':')); int proxy_port = int.Parse(proxy.Substring(proxy.IndexOf(':') + 1)); session.setProxy(new ProxyHTTP(proxy_host, proxy_port)); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoViaHTTP(); 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(); Console.WriteLine("-- Shell channel is connected using the {0} cipher", session.Cipher); // 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(params string[] arg) { if (arg.Length < 3) { Console.Error.WriteLine( "usage: java KeyGen rsa output_keyfile comment\n" + " java KeyGen dsa output_keyfile comment"); return; } try { // 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"); return; } // 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); // Generate the new key pair KeyPair key_pair = KeyPair.genKeyPair(jsch, type); // Set a passphrase key_pair.setPassphrase(passphrase); // Write the private key to "filename" key_pair.writePrivateKey(filename); // Write the public key to "filename.pub" key_pair.writePublicKey(filename + ".pub", comment); // Print the key fingerprint Console.WriteLine("Finger print: " + key_pair.getFingerPrint()); // Free resources key_pair.dispose(); } catch (Exception e) { Console.WriteLine(e); } return; }
public string getFingerPrint(JSch jsch) { HASH hash = null; try { hash = (HASH)Activator.CreateInstance(Type.GetType(jsch.getConfig("md5"))); } catch (Exception e) { Console.Error.WriteLine("getFingerPrint: " + e); } return Util.getFingerPrint(hash, m_key); }
public static KeyPair load(JSch jsch, string prvkey) { string pubkey = prvkey + ".pub"; // if(!new File(pubkey).exists()) if (!File.Exists(pubkey)) { pubkey = null; } return load(jsch, prvkey, pubkey); }
public static void RunExample(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, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoAES(); session.setUserInfo(ui); //Add AES128 as default cipher in the session config store StringDictionary config = new StringDictionary(2); 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.Cipher); //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.Message); } }
public static void RunExample(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, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoSubsystem(); session.setUserInfo(ui); // Connect to remote SSH server session.Connect(); // Get subsystem name from user string subsystem = InputForm.GetUserInput("Enter subsystem name", ""); // Open a new Subsystem channel on the SSH session Channel channel = session.openChannel("subsystem"); ((ChannelSubsystem)channel).setSubsystem(subsystem); ((ChannelSubsystem)channel).setPty(true); // Redirect standard I/O to the SSH channel channel.setInputStream(Console.OpenStandardInput()); channel.setOutputStream(Console.OpenStandardOutput()); ((ChannelSubsystem)channel).setErrStream(Console.OpenStandardError()); // Connect the channel channel.connect(); Console.WriteLine("-- Subsystem '" + subsystem + "' is connected using the {0} cipher", session.Cipher); // 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(); //Get the "known hosts" 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, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoPubKey(); 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 KeyPair genKeyPair(JSch jsch, int type, int key_size) { KeyPair kpair = null; if (type == DSA) kpair = new KeyPairDSA(jsch); else if (type == RSA) kpair = new KeyPairRSA(jsch); if (kpair != null) kpair.generate(key_size); return kpair; }
public static void RunExample(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, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoStreamForwarding(); 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).Host = host; ((ChannelDirectTCPIP)channel).Port = port; channel.connect(); while (!channel.IsClosed) System.Threading.Thread.Sleep(500); channel.disconnect(); session.disconnect(); } catch (Exception e) { Console.WriteLine(e); } }
public static void RunExample(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, SharpSsh.SshBase.SSH_TCP_PORT); // 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 UserInfoPortForwardingR(); 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.Message); } }
internal Session(JSch jsch) { m_jsch = jsch; m_buf = new Buffer(); m_packet = new Packet(m_buf); }
private byte[] m_q_array; // prime q #endregion Fields #region Constructors public KeyPairRSA(JSch jsch) : base(jsch) { }
public static KeyPair load(JSch jsch, string prvkey, string pubkey) { byte[] iv = new byte[8]; // 8 bool encrypted = true; byte[] data = null; byte[] publickeyblob = null; int type = ERROR; int vendor = VENDOR_OPENSSH; try { //File file=new File(prvkey); FileStream fis = File.OpenRead(prvkey); byte[] buf = new byte[(int)(fis.Length)]; int len = fis.Read(buf, 0, buf.Length); fis.Close(); int i = 0; while (i < len) { if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSA; } else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; vendor = VENDOR_FSECURE; } else { //System.outs.println("invalid format: "+identity); throw new JSchException("invaid privatekey: " + prvkey); } i += 3; continue; } if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) + (a2b(buf[i++]) & 0xf)); } continue; } if (buf[i] == 0x0d && i + 1 < buf.Length && buf[i + 1] == 0x0a) { i++; continue; } if (buf[i] == 0x0a && i + 1 < buf.Length) { if (buf[i + 1] == 0x0a) { i += 2; break; } if (buf[i + 1] == 0x0d && i + 2 < buf.Length && buf[i + 2] == 0x0a) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < buf.Length; j++) { if (buf[j] == 0x0a) break; //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invaid privatekey: " + prvkey); } int start = i; while (i < len) { if (buf[i] == 0x0a) { bool xd = (buf[i - 1] == 0x0d); Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0) ); if (xd) len--; len--; continue; } if (buf[i] == '-') { break; } i++; } data = Util.fromBase64(buf, start, i - start); if (data.Length > 4 && // FSecure data[0] == (byte)0x3f && data[1] == (byte)0x6f && data[2] == (byte)0xf9 && data[3] == (byte)0xeb) { Buffer _buf = new Buffer(data); _buf.getInt(); // 0x3f6ff9be _buf.getInt(); byte[] _type = _buf.getString(); byte[] _cipher = _buf.getString(); string cipher = Util.getString(_cipher); if (cipher.Equals("3des-cbc")) { _buf.getInt(); byte[] foo = new byte[data.Length - _buf.OffSet]; _buf.getByte(foo); data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + prvkey); } else if (cipher.Equals("none")) { _buf.getInt(); _buf.getInt(); encrypted = false; byte[] foo = new byte[data.Length - _buf.OffSet]; _buf.getByte(foo); data = foo; } } if (pubkey != null) { try { fis = File.OpenRead(pubkey); buf = new byte[(int)(fis.Length)]; len = fis.Read(buf, 0, buf.Length); fis.Close(); if (buf.Length > 4 && // FSecure's public key buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { bool valid = true; i = 0; do { i++; } while (buf.Length > i && buf[i] != 0x0a); if (buf.Length <= i) { valid = false; } while (valid) { if (buf[i] == 0x0a) { bool inheader = false; for (int j = i + 1; j < buf.Length; j++) { if (buf[j] == 0x0a) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (buf.Length <= i) { valid = false; } start = i; while (valid && i < len) { if (buf[i] == 0x0a) { Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } if (valid) { publickeyblob = Util.fromBase64(buf, start, i - start); if (type == UNKNOWN) { if (publickeyblob[8] == 'd') { type = DSA; } else if (publickeyblob[8] == 'r') { type = RSA; } } } } else { if (buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-') { i = 0; while (i < len) { if (buf[i] == ' ')break; i++; } i++; if (i < len) { start = i; while (i < len) { if (buf[i] == ' ')break; i++; } publickeyblob = Util.fromBase64(buf, start, i - start); } } } } catch { } } } catch (Exception e) { if (e is JSchException) throw (JSchException)e; throw new JSchException(e.ToString()); } KeyPair kpair = null; if (type == DSA) { kpair = new KeyPairDSA(jsch); } else if (type == RSA) { kpair = new KeyPairRSA(jsch); } if (kpair != null) { kpair.encrypted = encrypted; kpair.publickeyblob = publickeyblob; kpair.vendor = vendor; if (encrypted) { kpair.iv = iv; kpair.data = data; } else { if (kpair.parse(data)) { return kpair; } else { throw new JSchException("invaid privatekey: " + prvkey); } } } return kpair; }
internal KnownHosts(JSch jsch) : base() { m_jsch = jsch; m_pool = new List<HostKey>(); }
public static KeyPair genKeyPair(JSch jsch, int type) { return genKeyPair(jsch, type, 1024); }
public KeyPair(JSch jsch) { m_jsch = jsch; }
public static void RunExample(string[] arg) { if (arg.Length != 2) { Console.WriteLine("usage: java ScpTo file1 user@remotehost:file2"); Environment.Exit(-1); } try { string lfile = arg[0]; string user = arg[1].Substring(0, arg[1].IndexOf('@')); arg[1] = arg[1].Substring(arg[1].IndexOf('@') + 1); string host = arg[1].Substring(0, arg[1].IndexOf(':')); string rfile = arg[1].Substring(arg[1].IndexOf(':') + 1); JSch jsch = new JSch(); Session session = jsch.getSession(user, host, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UseScpTorInfo(); session.setUserInfo(ui); session.Connect(); // exec 'scp -t rfile' remotely string command = "scp -p -t " + rfile; Channel channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); // get I/O streams for remote scp Stream outs = channel.getOutputStream(); Stream ins = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[1]; if (checkAck(ins) != 0) Environment.Exit(0); // send "C0644 filesize filename", where filename should not include '/' int filesize = (int)(new FileInfo(lfile)).Length; command = "C0644 " + filesize + " "; if (lfile.LastIndexOf('/') > 0) command += lfile.Substring(lfile.LastIndexOf('/') + 1); else command += lfile; command += "\n"; byte[] buff = Util.getBytes(command); outs.Write(buff, 0, buff.Length); outs.Flush(); if (checkAck(ins) != 0) Environment.Exit(0); // send a content of lfile FileStream fis = File.OpenRead(lfile); byte[] buf = new byte[1024]; while (true) { int len = fis.Read(buf, 0, buf.Length); if (len <= 0) break; outs.Write(buf, 0, len); outs.Flush(); Console.Write("#"); } // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); Console.Write("."); if (checkAck(ins) != 0) Environment.Exit(0); Console.WriteLine("OK"); Environment.Exit(0); } catch (Exception e) { Console.WriteLine(e); } }
public static void RunExample(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.Host + " " + 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, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoKnownHosts(); session.setUserInfo(ui); // Connect to remote SSH server session.Connect(); // Print the host key info // of the connected server: hk = session.HostKey; Console.WriteLine("HostKey: " + hk.Host + " " + 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.Cipher); // 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.Message); } }
internal IdentityFile(string identity, JSch jsch) { m_identity = identity; m_jsch = jsch; try { Type c = Type.GetType(jsch.getConfig("3des-cbc")); m_cipher = (Cipher)Activator.CreateInstance(c); m_key = new byte[m_cipher.BlockSize]; // 24 m_iv = new byte[m_cipher.IVSize]; // 8 c = Type.GetType(jsch.getConfig("md5")); m_hash = (HASH)(Activator.CreateInstance(c)); m_hash.init(); FileInfo file = new FileInfo(identity); FileStream fis = File.OpenRead(identity); byte[] buf = new byte[(int)(file.Length)]; int len = fis.Read(buf, 0, buf.Length); fis.Close(); int i = 0; while (i < len) { if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') m_type = DSS; else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') m_type = RSA; else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure m_type = UNKNOWN; m_keytype = FSECURE; } else throw new JSchException("invaid privatekey: " + identity); i += 3; continue; } if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',') { i += 4; for (int ii = 0; ii < m_iv.Length; ii++) m_iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) + (a2b(buf[i++]) & 0xf)); continue; } if (buf[i] == '\r' && i + 1 < buf.Length && buf[i + 1] == '\n' ) { i++; continue; } if (buf[i] == 0x0a && i + 1 < buf.Length) { if (buf[i + 1] == '\n') { i += 2; break; } if (buf[i + 1] == '\r' && i + 2 < buf.Length && buf[i + 2] == '\n' ) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < buf.Length; j++) { if (buf[j] == 0x0a) break; //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; m_encrypted = false; // no passphrase break; } } i++; } if (m_type == ERROR) throw new JSchException("invaid privatekey: " + identity); int start = i; while (i < len) { if (buf[i] == 0x0a) { bool xd = (buf[i - 1] == 0x0d); Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0) ); if (xd) len--; len--; continue; } if (buf[i] == '-') break; i++; } m_encoded_data = Util.fromBase64(buf, start, i - start); if (m_encoded_data.Length > 4 && // FSecure m_encoded_data[0] == (byte)0x3f && m_encoded_data[1] == (byte)0x6f && m_encoded_data[2] == (byte)0xf9 && m_encoded_data[3] == (byte)0xeb) { Buffer _buf = new Buffer(m_encoded_data); _buf.getInt(); // 0x3f6ff9be _buf.getInt(); byte[] _type = _buf.getString(); byte[] _cipher = _buf.getString(); string s_cipher = System.Text.Encoding.Default.GetString(_cipher); if (s_cipher.Equals("3des-cbc")) { _buf.getInt(); byte[] foo = new byte[m_encoded_data.Length - _buf.OffSet]; _buf.getByte(foo); m_encoded_data = foo; m_encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else if (s_cipher.Equals("none")) { _buf.getInt(); m_encrypted = false; byte[] foo = new byte[m_encoded_data.Length - _buf.OffSet]; _buf.getByte(foo); m_encoded_data = foo; } } try { file = new FileInfo(identity + ".pub"); fis = File.OpenRead(identity + ".pub"); buf = new byte[(int)(file.Length)]; len = fis.Read(buf, 0, buf.Length); fis.Close(); } catch { return; } if (buf.Length > 4 && // FSecure's public key buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-' ) { i = 0; do { i++; } while (buf.Length > i && buf[i] != 0x0a); if (buf.Length <= i) return; while (true) { if (buf[i] == 0x0a) { bool inheader = false; for (int j = i + 1; j < buf.Length; j++) { if (buf[j] == 0x0a) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (buf.Length <= i) return; start = i; while (i < len) { if (buf[i] == 0x0a) { Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } m_publickeyblob = Util.fromBase64(buf, start, i - start); if (m_type == UNKNOWN) { if (m_publickeyblob[8] == 'd') m_type = DSS; else if (m_publickeyblob[8] == 'r') m_type = RSA; } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') return; i = 0; while (i < len) { if (buf[i] == ' ') break; i++; } i++; if (i >= len) return; start = i; while (i < len) { if (buf[i] == ' ') break; i++; } m_publickeyblob = Util.fromBase64(buf, start, i - start); } } catch (Exception e) { if (e is JSchException) throw (JSchException)e; throw new JSchException(e.ToString()); } }
public static void RunExample(string[] arg) { if (arg.Length != 2) { Console.WriteLine("usage: java ScpFrom user@remotehost:file1 file2"); return; } try { string user = arg[0].Substring(0, arg[0].IndexOf('@')); arg[0] = arg[0].Substring(arg[0].IndexOf('@') + 1); string host = arg[0].Substring(0, arg[0].IndexOf(':')); string rfile = arg[0].Substring(arg[0].IndexOf(':') + 1); string lfile = arg[1]; string prefix = null; if (Directory.Exists(lfile)) prefix = lfile + Path.DirectorySeparatorChar; JSch jsch = new JSch(); Session session = jsch.getSession(user, host, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoScpFrom(); session.setUserInfo(ui); session.Connect(); // exec 'scp -f rfile' remotely string command = "scp -f " + rfile; Channel channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); // get I/O streams for remote scp Stream outs = channel.getOutputStream(); Stream ins = channel.getInputStream(); channel.connect(); byte[] buf = new byte[1024]; // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); while (true) { int c = checkAck(ins); if (c != 'C') break; // read '0644 ' ins.Read(buf, 0, 5); int filesize = 0; while (true) { ins.Read(buf, 0, 1); if (buf[0] == ' ') break; filesize = filesize * 10 + (buf[0] - '0'); } string file = null; for (int i = 0; ; i++) { ins.Read(buf, i, 1); if (buf[i] == (byte)0x0a) { file = Util.getString(buf, 0, i); break; } } //Console.WriteLine("filesize="+filesize+", file="+file); // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); // read a content of lfile FileStream fos = File.OpenWrite(prefix == null ? lfile : prefix + file); int foo; while (true) { if (buf.Length < filesize) foo = buf.Length; else foo = filesize; ins.Read(buf, 0, foo); fos.Write(buf, 0, foo); filesize -= foo; if (filesize == 0) break; } fos.Close(); byte[] tmp = new byte[1]; if (checkAck(ins) != 0) Environment.Exit(0); // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); } Environment.Exit(0); } catch (Exception e) { Console.WriteLine(e); } }
public static void RunExample(string[] arg) { try { JSch jsch = new JSch(); InputForm inForm = new InputForm(); inForm.Text = "Enter username@hostname"; string host = null, user; while(true) { inForm.SetText(""); if (!inForm.PromptForInput()) { Console.WriteLine("Cancelled"); return; } user = inForm.GetText(); if (!string.IsNullOrEmpty(user) && user.IndexOf('@') >= 0 && user.IndexOf('@') < user.Length - 1) { host = user.Substring(user.IndexOf('@') + 1); break; } } Session session = jsch.getSession(user, host, SharpSsh.SshBase.SSH_TCP_PORT); // username and password will be given via UserInfo interface. UserInfo ui = new UserInfoSftp(); session.setUserInfo(ui); session.Connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp c = (ChannelSftp)channel; Stream ins = Console.OpenStandardInput(); TextWriter outs = Console.Out; List<string> cmds = new List<string>(); 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--; 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 = 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]); StringDictionary config = new StringDictionary(2); 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 { } 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.ToString()); } 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.ToString()); } 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.ToString()); } 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.ToString()); } continue; } if (cmd.Equals("lls") || cmd.Equals("ldir")) { string path = "."; if (cmds.Count == 2) path = (String)cmds[1]; try { 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 = "."; if (cmds.Count == 3) p2 = (String)cmds[2]; try { SftpProgressMonitor monitor = new MyProgressMonitor(); if (cmd.StartsWith("get")) { ChannelSftp.ChannelSftpModes mode = ChannelSftp.ChannelSftpModes.OVERWRITE; if (cmd.Equals("get-resume")) mode = ChannelSftp.ChannelSftpModes.RESUME; else if (cmd.Equals("get-append")) mode = ChannelSftp.ChannelSftpModes.APPEND; c.get(p1, p2, monitor, mode); } else { ChannelSftp.ChannelSftpModes mode = ChannelSftp.ChannelSftpModes.OVERWRITE; if (cmd.Equals("put-resume")) mode = ChannelSftp.ChannelSftpModes.RESUME; else if (cmd.Equals("put-append")) mode = ChannelSftp.ChannelSftpModes.APPEND; c.put(p1, p2, monitor, mode); } } catch (SftpException e) { Console.WriteLine(e.ToString()); } 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.ToString()); } 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.ToString()); } if (attrs != null) outs.WriteLine(attrs); 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); } }