|
Hey kiglid,
Were you able to get around the error you mentioned.
ex {"Session.connect: System.NullReferenceException: Object reference not set to an instance of an object.
I am getting a similar one for a openssh DSA key
e System.NullReferenceException: Object reference not set to an instance of an o
bject.
at Tamir.SharpSsh.jsch.Util.stripLeadingZeros(Byte[] a)
at Tamir.SharpSsh.jsch.jce.SignatureDSA.setPrvKey(Byte[] x, Byte[] p, Byte[]
q, Byte[] g)
at Tamir.SharpSsh.jsch.IdentityFile.getSignature_dss(Session session, Byte[]
data)
Tamir,
Any ideas??
Raj
|
|
|
|
|
Hi Tamir,
I have one query.. just to have clear idea about Expect function.
If I set
shell.ExpectPattern = userName + ">";
then can I use this
string returnedString = shell.Expect();
or I must use
string returnedString = shell.Expect(userName + ">");
again and again???
Somehow, when I am trying to execute a batch of commands, the process gets stuck.
I have seen the strings returned are sometimes not proper text messages (as you might see them on an tty client) and I see blocks of characters, and so it might be possible that the pattern use to expect is not found (due to garbled string).
Any idea why the string is corrupted?
|
|
|
|
|
If you set the ExpectPattern property it should be safe to ise the Expect() function.
Nayan Choudhary wrote: and so it might be possible that the pattern use to expect is not found (due to garbled string).
Does the garbled string have obvious pattern? Maybe you can add it to the Expect pattern in a way that it will match both garbelled and non garbelled responses.
|
|
|
|
|
I don't know if the garbled string has the pattern, the way you mentioned (I'm sorry, I couldn't understand your answer clearly).
The string gets corrupted randomly with blocks of special characters. I don't know what should be the pattern to avoid/remove it. Anyways, let me experiment for some time to find when does it happen.
Thanks a lot!
Nayan
|
|
|
|
|
Hi Tamir,
I am finding serious issues of string returned from Expect function.
I very often see the pattern - multiple spaces followed by multiple backspace char, same in number - in between the strings returned. Also, sometimes the prompt is breaking the command in the string returned.
I cannot show the pattern here because the editor takes it normal keystrokes. That means, visually the data is correct, but the binary data in the string has these chars.
Example:
I want to run command
\ls -lt /myFolder/file_* | tr -s " " " " | cut -d " " -f 5,6,7,8,9
multiple times (actually polling the files - any better idea on how to poll unix files?).
What I see on the screen is
\ls -lt /myFolder/file_* | tr -s <br />
cbwrk11@hpxc7601!M3G:cbwrk11> /file_* | tr -s " <" " " | cut -d " " -<br />
cbwrk11@hpxc7601!M3G:cbwrk11> " " " " | cut -d " " -f 5,6,7,,9<br />
myFolder/file_* not found<br />
cbwrk11@hpxc7601!M3G:cbwrk11>\ls -lt /myFolder/file_* | tr -s <br />
cbwrk11@hpxc7601!M3G:cbwrk11> /file_* | tr -s " < " " " | cut -d " " -<br />
cbwrk11@hpxc7601!M3G:cbwrk11> " " " " | cut -d " " -f 5,6,7,,9<br />
/myFolder/file_* not found<br />
cbwrk11@hpxc7601!M3G:cbwrk11>
[Here you cannot see the backspace chars. But as I pasted them in this editor, I can see them here. Also note that there is a '<' char in between the strings which is unexpected.]
The execution is working fine. But the string returned isn't correct, thus not useful for logging what happened.
Please, can you suggest something?
Thanks a million!
Nayan
|
|
|
|
|
Hi Nayan,
May I suggest taking different approach than using Expect?
Can you please try using Exec channel instead of Shell channel (check out my samples for more info)?
When running a remote program using exec, the output comes with no terminal escape characters.
However exec channles are not always supported by servers and you get less script wise capabilities compared to Expect.
Please have it ago.
Thanks,
Tamir
|
|
|
|
|
Hi Tamir,
As I see your examples, I understand the idea of using Exec channel.
But I see very less documentation (for most of the things) in order to experiment freely. Can you provide any link or write about the conditions when servers don't support exec channel and what exactly are the reduced scripting capabilities?
Thanks for the help! I really appreciate it.
Nayan
|
|
|
|
|
ssh exec channels are usually used for executing a single remote progrem and interacting with its stdout, stdin, stderr, etc.
For example the SCP implementation is using an exec channel to execute a remote "scp -t" command. You may take a look at my Scp class for more info.
However when using ssh exec you lose the context of a Shell. Each exec command is run independently of other commands. This means (for example) that if you set env variables in a command they will be lost upon the next exec command.
OpenSSH supports exec channels, but some other servers doesn't (can't really remember any names at the moment, sorry).
|
|
|
|
|
Can you tell me what is wrong?
Trying SharpSSH binaries got this error
Connecting...Auth fail
SharpSSH-1.1.1.11
User and password works in putty
Regards
Ezequiel
|
|
|
|
|
Is password authentication enabled on the server? For example, on OpenSSH server you should have the following line in the ssh_config file:
PasswordAuthentication yes
If password authentication is enabled, it will be useful if you could post the output of the following OpenSSH command:
ssh -vv [serverHost]
|
|
|
|
|
ssh -vv linuxba01
OpenSSH_3.8.1p1 FreeBSD-20060123, OpenSSL 0.9.7e-p1 25 Oct 2004
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Connecting to linuxba01.ar.lumina.net [10.0.0.43] port 22.
debug1: Connection established.
debug1: identity file /root/.ssh/identity type -1
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug2: key_type_from_name: unknown key type '-----END'
debug1: identity file /root/.ssh/id_rsa type 1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: Remote protocol version 1.99, remote software version OpenSSH_3.4p1
debug1: match: OpenSSH_3.4p1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_3.8.1p1 FreeBSD-20060123
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-dss,ssh-rsa
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib
debug2: kex_parse_kexinit: none,zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib
debug2: kex_parse_kexinit: none,zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_init: found hmac-md5
debug1: kex: server->client aes128-cbc hmac-md5 none
debug2: mac_init: found hmac-md5
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug2: dh_gen_key: priv key bits set: 119/256
debug2: bits set: 503/1024
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'linuxba01.ar.lumina.net' is known and matches the DSA host key.
debug1: Found key in /root/.ssh/known_hosts:7
debug2: bits set: 505/1024
debug1: ssh_dss_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /root/.ssh/identity (0x0)
debug2: key: /root/.ssh/id_rsa (0x806e4c0)
debug2: key: /root/.ssh/id_dsa (0x0)
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /root/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug1: Next authentication method: keyboard-interactive
debug2: userauth_kbdint
debug2: we sent a keyboard-interactive packet, wait for reply
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
root@linuxba01.ar.lumina.net's password:
debug2: we sent a password packet, wait for reply
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Entering interactive session.
debug2: callback start
debug2: ssh_session2_setup: id 0
debug2: channel 0: request pty-req
debug2: channel 0: request shell
debug2: fd 3 setting TCP_NODELAY
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 131072
Last login: Mon Oct 30 11:25:39 2006 from nop3
[root@linuxba01 root]# exit
logout
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug2: channel 0: close_read
debug2: channel 0: input open -> closed
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
Connection to linuxba01.ar.lumina.net closed.
debug1: Transferred: stdin 0, stdout 0, stderr 47 bytes in 9.7 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 4.9
debug1: Exit status 0
|
|
|
|
|
I don't see any issues with this output, which may indicate a SharpSSH bug.
May I send you a debug version of SharpSSH, which will add some prints to see what's going on?
If so, please drop me a line at [SharpSSH at tamirgal.com] and we'll take it from there...
Cheers,
Tamir
-- modified at 10:55 Monday 30th October, 2006
|
|
|
|
|
Did you get this fixed...I am having a simular problem.
|
|
|
|
|
Hi Tamir, is there any progress yet? I am having a similar problem. Also how come examples don't even prompt for password? I am trying to test reverse port forwarding.
|
|
|
|
|
Hi,
Any chance the server doesn't have password authentication enabled?
Did you manage to have port forwarding with another ssh client (e.g. OpenSSH)?
Please issue the command "ssh -vv [server_host]" from an OpenSSH machine and send the output to "SharpSSH at tamirgal.com", I'll take a look.
Tamir
|
|
|
|
|
Thank you so much for your help via email! Everything started to work once I enabled password authentication on the server. You are the best
|
|
|
|
|
Hi Tamir,
hope you can help here. I have been using both Visual Studio and C# for about 2 hours, and prior to this have very little experience programming, so am definatly still in the 'no idea what I am doing' category.
What I am trying to achieve is to pull information off several devices across our network (basically the device name, and the revision of operating system running on it), however to access these devices we first have to SSH into a FreeBSD server located in a central location, then telnet from here into the approriate device.
Firstly, can I use sharpSSH to perform all of this? I am unsure how a telnet session from within an SSH session is handled?
Secondly, I seem to have a problem with the ExpectPattern and the prompt from our server. The prompt is 'bash-2.05$' however I cannot just use the '$' character, as this character is also used in the welcome banner, so the subsequent ssh.writeline starts before the server is ready. I have tried using the whole 'bash-2.05$' as the ExpectPattern, but this just seems to cause the application to hang.
Thanks in advance,
Simon
|
|
|
|
|
SimBy wrote: I am unsure how a telnet session from within an SSH session is handled?
You should do it similarly to a real user using telnet from the FreeBSD shell. You will probably need to change the pattern to expect at each call. For example the telnet login sequence will look something like:
ssh.WriteLine("telnet hostname");
ssh.Expect("Password: ");
ssh.WriteLine(password);
ssh.Expect("prompt>");
SimBy wrote: I have tried using the whole 'bash-2.05$' as the ExpectPattern, but this just seems to cause the application to hang.
This is probably because the dollar sign ($) is a special character in regular expression syntax. If you want to match a dollar sign you should prefix it with a back slash "\". So matching the whole prompt will look something like:
ssh.Expect("bash-2.05\\$");
Cheers,
Tamir
|
|
|
|
|
Thanks Tamir,
worked perfectly, now to do all kinds of fancy stuff with this!
|
|
|
|
|
I'm trying to use sharpSsh with a custom SSIS control to do SFTP. This requires that my assembly be signed and inserted into the GAC. I can sign my assembly, and sharpSsh, but I can't do anything about the DiffiHellman or Mentalis libs that are packaged as .DLLs, not source.
Is there a way around this? I apologize if the answer is obvious - I'm new to signed assemblies and the GAC.
Thanks,
HE
|
|
|
|
|
You may sign those libs as well, their source code is freely available from mentalis.org
|
|
|
|
|
Thanks for the pointer, and thanks for providing this useful code.
|
|
|
|
|
In your Expect function of SshShell class,
public string Expect(Regex pattern)
if you modify the code like this, won't the matching be faster in case of long response?
...<br />
string tmp = System.Text.Encoding.Default.GetString(buff, 0, readCount);<br />
resp.Append( tmp, 0, readCount);<br />
string s = resp.ToString();<br />
match = pattern.Match( <big>tmp</big> );<br />
...
Just an idea.
-- modified at 11:54 Tuesday 17th October, 2006
|
|
|
|
|
Yes, but unfortunatly this won't work in some cases.
The Expect() function uses a Stream to fill the tmp string. The Stream's Read() function does not guaranty to fetch *all* available data on a single read operation, but only some.
So if for example we want our regex to match the "SomePromptString>" string, the Read() function may first return "SomePro" and then "mptString>" .
In this case your Expect() function will not match the pattern, and thus will never return. This is why we must perform the match operation on the whole aggregated response string.
|
|
|
|
|
Yes, you are correct! I didn't think about that. Case closed!
|
|
|
|
|