How to connect to a PC via SSH? (Is it possible?) lazarus / 8-Other ---------------------------------------------------------------------- dotniet · 2009-10-05 11:46 Hi. I need to work (run/edit/delete files) with 12 Linux computer nodes in a cluster. Usually I use SSH to connect to those nodes. How can I do this from Lazarus? ______________________ I am new to Lazarus but I have good experience with Delphi. Thanks ---------------------------------------------------------------------- Vincent Snijders · 2009-10-05 15:10 You can use TProcess that calls plink.exe or ssh. See the input/output example of http://wiki.lazarus.freepascal.org/Executing_External_Progr ams#Using_input_and_output_of_a_TProcess ---------------------------------------------------------------------- dotniet · 2009-10-16 10:58 PLink is for Windows. I am running on Linux. I have tried to run the 'ssh' command via TProcess (see the example here: https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus- ccr/examples/process) but it is not working. I mean it launches the SSH, then it asks for password and then nothing happens. TProcess returns no output from SSH. ---------------------------------------------------------------------- Vincent Snijders · 2009-10-16 11:22 IIRC, when I wrote the example, I used passwordless connections, using a private key file. ---------------------------------------------------------------------- Marc · 2009-10-16 11:48 ssh doesn't ask for a password through a "virtual" console like a pipe, but it wants a real console. google on ssh-askpass, maybe it helps ---------------------------------------------------------------------- dotniet · 2009-10-16 13:02 Quote from: Vincent Snijders on October 16, 2009, 01:22:33 pmIIRC, when I wrote the example, I used passwordless connections, using a private key file. Can you provide some keywords to type in google? I need a point from where to start. I have never used private keys. Thanks. Quote from: Markssh doesn't ask for a password through a "virtual" console like a pipe, but it wants a real console. google on ssh-askpass, maybe it helpsSSH asked for password! It pop-up a box (GUI) and let me to type the password. But after that I got no sign of life from it.  In a different thread, I already announced that the examples for TProcess (on Lazarus web site) are not working. I used those examples to run ssh. ---------------------------------------------------------------------- Vincent Snijders · 2009-10-16 13:43 http://www.google.nl/search?q=ssh+passwordless+login ---------------------------------------------------------------------- dotniet · 2009-10-16 14:59 Thanks. I strated to follow a tutorial on passwordless SSH but I cannot do it bacause I don't have access to stuff. I wil let the network administrator do it. I will let you know how it worked. I suppose you used SSH with TProcess and sent the input commands to the TProcess. Right? Thanks again. ---------------------------------------------------------------------- Marc · 2009-10-19 09:16 Quote from: dotniet on October 16, 2009, 03:02:25 pmIn a different thread, I already announced that the examples for TProcess (on Lazarus web site) are not working. I used those examples to run ssh. This was the way I used the remote ssh-debugger option in lazarus. But... I ran gdb over ssh, so I had the output of gdb. maybe a console itself doesn't work with tprocess ---------------------------------------------------------------------- dotniet · 2009-10-19 14:49 Hi. I have tried to run SSH via TProcess but it is not working (no output is produced). Here is the code used (it is the code provided as example on Lazarus wiki page): Code: [Select] program launchprogram;  uses    Classes, SysUtils, Process;  var    AProcess: TProcess;    AStringList: TStringList;  begin    AProcess := TProcess.Create(nil);    AStringList := TStringList.Create;    AProcess.CommandLine := 'ssh';    AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];    AProcess.Execute;    AStringList.LoadFromStream(AProcess.Output);    writeln(AStringList.text);    AStringList.Free;    AProcess.Free;  end.            I am using passwordless SSH. ---------------------------------------------------------------------- dotniet · 2009-10-19 14:52 For me, SSH (via TProcess) didn't work. It may be a bug in this new FPC release? I have starteda new thread about it here: http://forum.lazarus.freepascal.org/index.php/topic,7720.0.html There is a special place where I can submit bug reports? As I told on this forum, TProcess is not running with many other programs!! ---------------------------------------------------------------------- Marc · 2009-10-19 22:52 did you read the wiki about large output ? Besides that, when running ssh, it will not exit unless you tell it to, so your program will wait forever on the line AProcess.Execute; ---------------------------------------------------------------------- Marc · 2009-10-19 22:53 this is not a bug of tproccess, but a flaw in your code logic. PS. There is no need to create a separate topic for this, so I merged them. Also when reporting bugs, use our bugtracker, see 4th link on the left side ---------------------------------------------------------------------- dotniet · 2009-10-20 09:57 SSH and TProcess is not working with ANY of the "official" examples provided with Lazarus. There are 3 "simple" examples and one for large output. Again - the examples are not working ALSO with basic commands like ls. I don't know how much can be my logic's fault when all I have done was to use the "official" lazarus documentation. ---------------------------------------------------------------------- Marc · 2009-10-20 13:50 That's strange, since when I wrote the large output example it worked. I recompiled my example with a 2.2.4 version of fpc and still works. However when trying with ssh, I didn't get output, unless I used the poStderrToOutPut option. It seems that ssh outputs to stderr. Code: [Select][marc@giant procout]$ ./procoutlarge "ls -al" -- executing -- . -- executed -- -- linecount = 10 -- | total 868 | drwxrwxr-x   2 marc marc   4096 2009-10-20 15:41 . | drwxr-xr-x 142 marc marc  20480 2009-10-12 23:16 .. | -rw-rw-r--   1 marc marc     16 2004-09-30 19:49 out.txt | -rw-rwxr-x   1 marc marc 386616 2004-09-30 19:44 procout | -rwxrwxr-x   1 marc marc 441976 2009-10-20 15:41 procoutlarge | -rw-rw-r--   1 marc marc   7096 2009-10-20 15:41 procoutlarge.o | -rw-rw-r--   1 marc marc   2015 2004-12-28 22:34 procoutlarge.pp | -rw-rw-r--   1 marc marc   2016 2004-12-28 22:34 procoutlarge.pp.bak | -rw-rw-r--   1 marc marc    609 2004-09-30 19:44 procout.pp -- end -- Code: [Select][marc@giant procout]$ ./procerrlarge "ssh -V" -- executing -- . -- executed -- -- linecount = 1 -- | OpenSSH_4.7p1, OpenSSL 0.9.8b 04 May 2006 -- end -- ---------------------------------------------------------------------- dotniet · 2009-10-21 14:14 It worked also for me after I have added the poStderrToOutPut option. THANKS A LOT! ---------------------------------------------------------------------- AnthonyTekatch · 2022-01-02 02:39 Quote from: Marc on October 16, 2009, 01:48:44 pmssh doesn't ask for a password through a "virtual" console like a pipe, but it wants a real console. google on ssh-askpass, maybe it helps The Expect program does work for ssh, and I'd be interested to know if Lazarus can easily do the same thing as this example: https://www.thegeekstuff.com/2010/10/expect-examples/ 6. SSH Login into Another Machine The example expect program shown below automates the ssh login from one machine to another machine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/usr/bin/expectset timeout 20set ip [lindex $argv 0]set user [lindex $argv 1]set password [lindex $argv 2]spawn ssh "$user\@$ip"expect "Password:"send "$password\r";interact ---------------------------------------------------------------------- trev · 2022-01-02 04:16 Do you really need to use a password? All my local and remote SSH logins are done with public keys and with passwords disabled for better security. ---------------------------------------------------------------------- dbannon · 2022-01-02 08:55 I would look at the ssh-askpass model if you really must. I have used ssh-askpass with a lazarus app (using scp but no real diff I expect). Can find the code if you need it. Honestly its not really secure but OK in a private, non-shared access situation. Just. Similarly, password-less keys are again, OK, if its a private and non-shared. But again, only just. If you have to get what you are doing past a well informed sys-admin, my guess is they will say "NO" ! Is it file access you need ?  sshfs may be an acceptable, you establish a sshfs mount using a real password and then Lazarus app can access files on that mountpoint as if they are local. All you need is sshfs on your desktop, and nothing special remotely. Davo Quote from: AnthonyTekatch on January 02, 2022, 03:39:06 amQuote from: Marc on October 16, 2009, 01:48:44 pmssh doesn't ask for a password through a "virtual" console like a pipe, but it wants a real console. google on ssh-askpass, maybe it helps The Expect program does work for ssh, and I'd be interested to know if Lazarus can easily do the same thing as this example: https://www.thegeekstuff.com/2010/10/expect-examples/ 6. SSH Login into Another Machine The example expect program shown below automates the ssh login from one machine to another machine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/usr/bin/expectset timeout 20set ip [lindex $argv 0]set user [lindex $argv 1]set password [lindex $argv 2]spawn ssh "$user\@$ip"expect "Password:"send "$password\r";interact ---------------------------------------------------------------------- AnthonyTekatch · 2022-01-02 10:26 Thank you for your answers. I am making a small program that Mac users will send its log files to a remote server that only allows sftp. Getting them to also install ssh-askpass or sshfs is likely too difficult. They would like a program with the simplicity of captainftp (or filezilla) where their password is stored in a configuration file, and that program alone sends the file. That is why I was looking for something like TProcess to act like "expect" and send sftp the password. Quote from: dbannon on January 02, 2022, 09:55:29 amI would look at the ssh-askpass model if you really must. I have used ssh-askpass with a lazarus app (using scp but no real diff I expect). Can find the code if you need it. Honestly its not really secure but OK in a private, non-shared access situation. Just. Similarly, password-less keys are again, OK, if its a private and non-shared. But again, only just. If you have to get what you are doing past a well informed sys-admin, my guess is they will say "NO" ! Is it file access you need ?  sshfs may be an acceptable, you establish a sshfs mount using a real password and then Lazarus app can access files on that mountpoint as if they are local. All you need is sshfs on your desktop, and nothing special remotely. Davo ---------------------------------------------------------------------- dbannon · 2022-01-02 22:00 Just to be clear Anthony, both ssh-askpass and sshfs are only required on the user's machine, not the remote server. I was not sure which machine you mean when you say "Getting them to also install...". Davo ---------------------------------------------------------------------- AnthonyTekatch · 2022-01-02 22:23 Quote from: dbannon on January 02, 2022, 11:00:35 pmJust to be clear Anthony, both ssh-askpass and sshfs are only required on the user's machine, not the remote server. I was not sure which machine you mean when you say "Getting them to also install...". Davo Thanks Davo, I should have been more clear. I meant that the user would be tasked with installing ssh-askpass or sshfs, and neither seem to be super- simple for a non-technical Mac user. For now, it seems that the expect program is already installed on some Macs, so I will proceed with using that by getting Lazarus to run expect with the ssh password in that command line. ---------------------------------------------------------------------- dbannon · 2022-01-02 23:54 Hmm, says https://www.expandrive.com/sshfs-mac/ > The go-to opensource option is the port of SSHFS from Linux by macFUSE. It is a relatively straightforward no-frills command-line appliation without any user interface,... Without any user interface ??  Its got a perfectly good command line interface ! > One caveat is that like many solutions, it relies on a kernel extension which has been deprecated by macOS and requires a reboot into recovery mode to reduce security level on newer M1 based macs. Ahh, run away, run away !  You sure don't want to be asking users to do things like that !  So, yep, I agree, thats not going to work. I can remember days spent trying to get expect scripts to work reliably, so I sympathize .... Good luck. Davo ---------------------------------------------------------------------- AnthonyTekatch · 2022-01-03 00:32 Thanks for digging that up about SSHFS. I will update here if I get an expect solution working. ---------------------------------------------------------------------- AnthonyTekatch · 2022-01-11 15:58 I managed to get expect to work on Linux and a Mac. The expect script looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i set timeout 20set sftparameters [lindex $argv 0]set password [lindex $argv 1]set localdir [lindex $argv 2]set remotedir [lindex $argv 3]set filename [lindex $argv 4]spawn sftp "$sftparameters"expect "assword:"send "$password\r"expect "sftp>"send "cd ${remotedir}\r"expect "sftp>"send "put ${localdir}${filename}\r"expect "sftp>"send "quit\r"exit  I have also attached an example project that runs the script.