module Network.SSH.Client.LibSSH2
(
Session, Channel, KnownHosts,
withSSH2,
withSession,
withSessionBlocking,
withChannel,
checkHost,
readAllChannel,
retryIfNeeded,
scpSendFile,
scpReceiveFile,
runShellCommands,
execCommands,
socketConnect
) where
import Control.Monad
import Control.Exception as E
import Network
import Network.BSD
import Network.Socket
import System.IO
import Network.SSH.Client.LibSSH2.Types
import Network.SSH.Client.LibSSH2.Errors
import Network.SSH.Client.LibSSH2.Foreign
waitSocket :: Handle -> Session -> IO Bool
waitSocket h s = do
dirs <- blockedDirections s
if INBOUND `elem` dirs
then hWaitForInput h (10*1000)
else return True
socketConnect :: String -> Int -> IO Socket
socketConnect hostname port = do
proto <- getProtocolNumber "tcp"
bracketOnError (socket AF_INET Stream proto) (sClose)
(\sock -> do
he <- getHostByName hostname
connect sock (SockAddrInet (fromIntegral port) (hostAddress he))
return sock)
withSSH2 :: FilePath
-> FilePath
-> FilePath
-> String
-> String
-> Int
-> (Channel -> IO a)
-> IO (Int, a)
withSSH2 known_hosts public private login hostname port fn =
withSessionBlocking hostname port $ \s -> do
r <- checkHost s hostname port known_hosts
publicKeyAuthFile s login public private ""
withChannel s $ fn
withSession :: String
-> Int
-> (Handle -> Session -> IO a)
-> IO a
withSession hostname port fn = do
sock <- socketConnect hostname port
handle <- socketToHandle sock ReadWriteMode
session <- initSession
handshake session sock
result <- fn handle session
disconnectSession session "Done."
freeSession session
hClose handle
return result
withSessionBlocking :: String
-> Int
-> (Session -> IO a)
-> IO a
withSessionBlocking hostname port fn = do
sock <- socketConnect hostname port
session <- initSession
handshake session sock
result <- fn session
disconnectSession session "Done."
freeSession session
return result
checkHost :: Session
-> String
-> Int
-> FilePath
-> IO KnownHostResult
checkHost s host port path = do
kh <- initKnownHosts s
knownHostsReadFile kh path
(hostkey, keylen, keytype) <- getHostKey s
result <- checkKnownHost kh host port hostkey [TYPE_PLAIN, KEYENC_RAW]
freeKnownHosts kh
return result
withChannel :: Session -> (Channel -> IO a) -> IO (Int, a)
withChannel s fn = do
ch <- openChannelSession s
result <- fn ch
closeChannel ch
exitStatus <- channelExitStatus ch
freeChannel ch
return (exitStatus, result)
readAllChannel :: Channel -> IO String
readAllChannel ch = do
(sz, res) <- readChannel ch 0x400
if sz > 0
then do
rest <- readAllChannel ch
return $ res ++ rest
else return ""
runShellCommands :: Session -> [String] -> IO (Int, [String])
runShellCommands s commands = do
ch <- openChannelSession s
requestPTY ch "linux"
channelShell ch
hello <- readAllChannel ch
out <- forM commands $ \cmd -> do
writeChannel ch (cmd ++ "\n")
r <- readAllChannel ch
return r
channelSendEOF ch
closeChannel ch
exitStatus <- channelExitStatus ch
freeChannel ch
return (exitStatus, out)
execCommands :: Session -> [String] -> IO (Int, [String])
execCommands s commands = do
ch <- openChannelSession s
out <- forM commands $ \cmd -> do
channelExecute ch cmd
readAllChannel ch
closeChannel ch
exitStatus <- channelExitStatus ch
freeChannel ch
return (exitStatus, out)
scpSendFile :: Session
-> Int
-> FilePath
-> FilePath
-> IO Integer
scpSendFile s mode local remote = do
h <- openFile local ReadMode
size <- hFileSize h
ch <- scpSendChannel s remote mode (fromIntegral size) 0 0
result <- writeChannelFromHandle s ch h
hClose h
closeChannel ch
freeChannel ch
return result
scpReceiveFile :: Session
-> FilePath
-> FilePath
-> IO Integer
scpReceiveFile s remote local = do
h <- openFile local WriteMode
(ch, fileSize) <- scpReceiveChannel s remote
result <- readChannelToHandle ch h fileSize
hClose h
closeChannel ch
freeChannel ch
return result
retryIfNeeded :: Handle -> Session -> IO a -> IO a
retryIfNeeded handle session action =
action `E.catch` (\(e :: ErrorCode) ->
if e == EAGAIN
then do
waitSocket handle session
retryIfNeeded handle session action
else throw e )