script for send a file to linux

This topic contains 9 replies, has 2 voices, and was last updated by Profile photo of XONE3D . XONE3D . 2 years, 2 months ago.

  • Author
    Posts
  • #18583
    Profile photo of XONE3D .
    XONE3D .
    Participant

    Hello,

    I try to send a file from a Windows 2012 server to a Linux server using a script.

    For that I have tested the following module: SSH-NET Librairy because if it is true ssh is not implemented natively in powershell. The trouble is that I manage to connect in my linux server has powershelle entering my login and password. But whenever I généère a public private key nothing works.

    Can you tell me what is the method to do this kind of script?

    thank you

  • #18584
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Have you added your public key to the .ssh/authorized_keys file of the user on the Linux machine you're trying to log into?

    Please share what you already have as PowerShell script (attach as .txt file or use the code block). It is kind of difficult to help you without you sharing what you already have.

    Thanks
    Daniel

  • #18587
    Profile photo of XONE3D .
    XONE3D .
    Participant

    Thanks for the reply

    The connection with linux is ok , i have all desinstalled adn installed and it's work. Just the scp will not work with my script :
    (pre){New-SshSession -Computer 192.168.0.1 -Username test -Keyfile C:\Users\Administrateur\Documents\ssh\id_private -Scp -Sftp -Reconnect
    Get-SshSession | ft -AutoSize

    Get-DhcpServerv4Lease -ScopeId 192.168.0.0 -ComputerName winserver |
    Select-Object -Property ClientId, IPAddress, HostName |
    Export-Csv -NoTypeInformation -Path C:\Users\Administrateur\mysql.csv

    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath \home\ubuntu\
    }(/pre)

    i have read this tutorial

    Thanks for help

  • #18588
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    I don't have a Linux machine installed to test myself at the moment but I've spotted three possible issues in your Send-ScpItem command.

    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath \home\ubuntu\

    1. You've specified the destination path with backslash's instead of forward-slash's which Linux expects.
    2. The destination path you've specified is not the "test" users home directory. You need to you use /home/test or give the test user access to /home/Ubuntu.
    3. I believe SCP expects the file in the destination path like below:

    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv

  • #18589
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Examples that work for me:

    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath mysql.csv
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath ./mysql.csv
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv
    

    Cheers
    Daniel

  • #18593
    Profile photo of XONE3D .
    XONE3D .
    Participant

    Have you mount a linux server ?

    That work not by me , i send you the error ( sorry for the language, im french)

    can you send me a example with the copy-item command other know you a another methode to send a file ?

    Thanks

  • #18596
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    I didn't mount the Linux machine. I've used the New-SshSession command as you did to establish the connection with the private/public key and used any of the example commands below to send the file successfully to the Linux machine.

    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath mysql.csv
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath ./mysql.csv
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test
    Send-ScpItem -ComputerName 192.168.0.1 -Path C:\Users\Administrateur\mysql.csv -DestinationPath /home/test/mysql.csv
    
  • #18633
    Profile photo of XONE3D .
    XONE3D .
    Participant

    I think it comes from that I not made ​​the following module:
    [url]http://www.powershelladmin.com/wiki/SSH-Sessions_Add-on_with_SCP/SFTP_Support[/url].

    But i try with a copy-item cmdl

    Copy-item -v C:\Users\administrateur\mysql.csv \\192.168.0.1\home\test\mysql.txt

    and nothing work the message error indicated that the network way is not found ....

    Have you a idéee from what that comming ?

    Thanks

  • #18643
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    The Copy-Item does not work out of the box with Linux machines. The Linux machine would need to have the Samba server installed and configured correctly to allow the access via UNC path (\\192.168.0.1\home\test).

    You were on the right track with the SSH-Sessions module. It would be better if you would continue with getting the SSH-Sessions PowerShell module work for you.

    Cheers
    Daniel

  • #18672
    Profile photo of XONE3D .
    XONE3D .
    Participant

    I will try to install winscp.exe then...

    Thanks for the help !

You must be logged in to reply to this topic.