2015-07-28 129 views
4

我想通過SSH連接到遠程的GNU + Linux機器並執行命令,但使用IPv6地址。使用IPv4地址,它工作得很好。如何使用C#中的SSH.NET庫連接IPv6地址

我在Ubuntu 15.04運行和使用的MonoDevelop(單聲道4.0)

下面是使用Renci SSH.NET庫我的示例代碼:

SshClient sshClient = new SshClient (ipV6Address, user, pass); 
sshClient.Connect(); 
SshCommand sshCmd = sshClient.RunCommand (command); 
sshClient.Disconnect(); 

我還試圖用Tamirs SshSharp和終端控制。

[ ]添加到IPv6地址也不起作用。

我得到以下異常:

System.Net.Sockets.SocketException: Invalid arguments 
at System.Net.Sockets.Socket+SocketAsyncResult.CheckIfThrowDelayedException() [0x00000] in <filename unknown>:0 
at System.Net.Sockets.Socket.EndConnect (IAsyncResult result) [0x00000] in <filename unknown>:0 
at Renci.SshNet.Session.SocketConnect (System.String host, Int32 port) [0x00000] in <filename unknown>:0 
at Renci.SshNet.Session.Connect() [0x00000] in <filename unknown>:0 
at Renci.SshNet.BaseClient.Connect() [0x00000] in <filename unknown>:0 
at main program... 

也許有人能提供給我一個代碼示例(在C#)或告訴我,明確支持IPv6地址庫。圖書館應該是免費的(最好使用Apache許可證)並可用於商業用途。

問候威馬

編輯:

來,我ipAddress缺乏網絡接口,我改變了結論後,我ipAddress"fe80::xxxx:xxxx:xxxx:xxxx%2"(是2是正確的,我用ping6測試)。

這似乎是工作有點「多」,因爲異常改爲:

System.ArgumentException: host 
at Renci.SshNet.ConnectionInfo..ctor (System.String host, Int32 port, System.String username, ProxyTypes proxyType, System.String proxyHost, Int32 proxyPort, System.String proxyUsername, System.String proxyPassword, Renci.SshNet.AuthenticationMethod[] authenticationMethods) [0x00000] in <filename unknown>:0 
at Renci.SshNet.PasswordConnectionInfo..ctor (System.String host, Int32 port, System.String username, System.Byte[] password, ProxyTypes proxyType, System.String proxyHost, Int32 proxyPort, System.String proxyUsername, System.String proxyPassword) [0x00000] in <filename unknown>:0 
at Renci.SshNet.PasswordConnectionInfo..ctor (System.String host, Int32 port, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at Renci.SshNet.SshClient..ctor (System.String host, Int32 port, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at Renci.SshNet.SshClient..ctor (System.String host, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at main program... 

解決

使用SSH.NET的預發佈包解決我的問題!

感謝@MartinPrikryl等;)

+1

當您嘗試使用IPv6地址時會出現什麼錯誤?原諒我的玩世不恭,但你確定你的網絡支持ipv6嗎? – pcwizz

+0

您是否嘗試首先使用GUI SSH客戶端連接到使用IPv6的服務器?像來自PuTTY? –

+0

是的,我的網絡支持IPv6。 通過ssh從shell進行連接正在運行。 Sry我忘了添加錯誤信息^^,但我現在添加了它。 – Wima

回答

2

如果您連接到一個鏈路本地ADRESS(FE80::)那麼你需要指定一個%,如FE80 ::%的eth0

接口名稱因爲你可以有多個鏈接本地地址,你需要告訴你的操作系統使用哪個接口。使用單播地址,操作系統知道由於路由表使用了什麼接口。

+0

謝謝,這對我有所幫助。我現在使用「fe80 :: .......%2」,這似乎更好,因爲異常更改爲「System.ArgumentException:host」。我將在我的主要文章中描述完整的錯誤消息 – Wima

+2

@Wima它甚至更糟,因爲IP甚至沒有在SSH.NET'ConnectionInfo'中獲得最後的驗證。無論如何,當'fe80 :: 2e0:dff:fe9d:307%2'傳遞給'SshClient'構造函數的'host'參數時,我沒有任何異常。你使用的是什麼版本的SSH.NET?在以前版本的SSH.NET中確實存在某種類型的IP地址正則表達式驗證,它們不接受接口名稱。但它不再存在(最新版本2014.4.6-beta2)。 –

+0

我使用SSH.NET版本2013.4.7 – Wima