0

Hey I have following python code:

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # Connect to the Linux server
    ssh.connect(hostname="name", username="root", password="password", port=22)
#if file exists it should be removed first
    ssh.exec_command("rm name")
    sftp = ssh.open_sftp()
#create file & add minecraft username to file
    f = sftp.open("name", "w")
    f.write(user)
    f.close()
#run bash file, which will whitelist user
    stdin, stdout, stderr = ssh.exec_command('./whitelist.sh')
    sftp.close()
    ssh.close()

This code is working on my home pc, but on my windows server the code dont execute "./whitelist.sh". I dont get a error message or something like that What is the reason for that and how can i fix it?

redseven
  • 849
  • 6
  • 11
merfy
  • 1
  • 2
  • You need to set the file to have execute permissions. BTW, there's no point to the `rm` first. – Charles Duffy Dec 18 '22 at 16:08
  • But why does it work if i execute this on my pc? – merfy Dec 18 '22 at 16:09
  • 2
    Are you trying to run a Bash/Sh script on a WIndows server? Is your WIndows server running WSL? – Zac Anger Dec 18 '22 at 16:09
  • And there _is_ an error, you're just not checking exit status on the channel so you don't see it – Charles Duffy Dec 18 '22 at 16:09
  • yes thats with rm is stupid you are right – merfy Dec 18 '22 at 16:09
  • *But why does it work if i execute this on my pc?* Presumably it _does_ have execute permissions on your PC, but you're only sending the contents of the file and not copying its permissions. – Charles Duffy Dec 18 '22 at 16:10
  • Also, make sure you're not doing something silly like starting a background process attached to stdin, stdout and stderr channels that go away as soon as the client disconnects, causing the process to be killed even though it was successfully started. (There's a reason the right way to start and manage background processes is with systemd or another process supervisor!) – Charles Duffy Dec 18 '22 at 16:11
  • so do I have to change this execute permissions on my windows or linux server? – merfy Dec 18 '22 at 16:13
  • It's the new file you create on the remote end that isn't executable by default. – Charles Duffy Dec 18 '22 at 16:13
  • Also, have you thought about not creating a script at all and passing the code to execute as an argument to the shell, or as its stdin? No reason to fix file permissions when you could just stop using a file altogether. – Charles Duffy Dec 18 '22 at 16:15
  • I dont get it. In the file which i am creating whitelist.sh is read out the name. whitelist.sh isnt created by the script. – merfy Dec 18 '22 at 16:16
  • Sorry i am a little bit new – merfy Dec 18 '22 at 16:17
  • I tried, but that dont worked with screen. So i created a workaround – merfy Dec 18 '22 at 16:17
  • My fault, I misread what you were doing (on a phone here). That said, which thing are you saying "don't worked with screen"? How did you determine that it didn't work? What did it do instead of working? If you're using `screen` as a process supervisor... don't; that's systemd's job. – Charles Duffy Dec 18 '22 at 16:17
  • I determine it on that way. The script is creating the name file (so i can see the file if i am typing ls in the linux console) but it dont run the command on my minecraft screen (i am running the server with screen). But if i execute whitelist.sh by console I see the command in screen, also when i execute the script on my pc. – merfy Dec 18 '22 at 16:23
  • Thanks. The message is "File does not exist". Maybe I have to write the complete path – merfy Dec 18 '22 at 16:34
  • but if i am running "ls", there is whitelist.sh in the dic – merfy Dec 18 '22 at 16:47
  • I get files does not exit because i wrote an echo with this... sorry forget this haha. If i try this in console it works – merfy Dec 18 '22 at 21:23
  • Sorry to dont describe it clearly. I checked it some minutes ago and tried this code: ``` ssh.exec_command("rm name") sftp = ssh.open_sftp() f = sftp.open("name", "w") f.write(user) f.close() stdin, stdout, stderr = ssh.exec_command('./whitelist.sh') for line in stdout: print(line.strip()) sftp.close() ssh.close() ``` and it works now. Thanks for your help! – merfy Dec 19 '22 at 09:32

0 Answers0