I'm starting with python3 and stuck with following problem. I will be thankful for any help to unblock me.
Requirement:
- run python script on host server to do following:
- from host server, ssh to a remote server.
- search for a file if its present on the remote server.
- if present, send its name to the host server.
- else, print following on host server
"file not found on remote server".
Sample code:
import sys
import getopt
import os
from os import path
import re
import copy
import getpass
import time
import socket
import re
bin_ver = "1.99"
chip = "6233"
remote_bin_path = "/path/of/the/binary/release/v"+bin_ver+"/bin/"
print("path of binary on remote server is " + remote_bin_path)
os.getcwd()
os.chdir(remote_bin_path)
os.getcwd()
if (chip == "6233" or chip == "62334"):
sub_ver = "ab"
else:
sub_ver = "xy"
print("sub_ver is: " + sub_ver)
search_string = "*" + chip + "*" + sub_ver + "*"
print("search_string is " + search_string)
cmd = "ls -1 " + search_string
print("cmd is " + cmd)
required_file = os.popen(cmd).read()
print("INFO: required file is \n"+required_file)
Input dataset: sample file names present on remote server:
binary-v1.99-6233b0-ab-cde-1.2.3.bin
binary-v1.99-62334a0-xy-cde-1.2.3.bin
binary-v1.99-62334b0-ab-cde-1.2.3.bin
binary-v1.99-62334a0-xy-cde-1.2.3.bin
binary-v1.99-6278b0-ab-cde-1.2.3.bin
binary-v1.99-6277a0-ab-cde-1.2.3.bin
Problem with above implementation:
This code runs fine on the host server, but the issue is it returns 2 file names:
binary-v1.99-6233b0-ab-cde-1.2.3.bin
binary-v1.99-62334b0-ab-cde-1.2.3.bin
Since the input variable chip = "6233", i do not want the output corresponding to 62334. How to set the correct word boundary for 6233 in search_string variable?
Next Setps:
How can I run this script on the remote server and return the filename to the host server.