判斷遠端主機是否存在某個檔案

2022-06-02 10:27:14 字數 3301 閱讀 2843

1.

1

#-*- coding:utf-8 -*-23

import

os,sys,paramiko,time4#

*******判斷所有測試環境是否有某個檔案,***********

5 s7 = "

10.10.123.96

"6 s3 = "

10.10.39.70

"7 host_dic =

20 exist ={}

21 not_exist ={}

22 port =22

23 user = "

root

"24 passwd = "

b^dc%4lsbvhzzk3b"25

26def

ssh_cmd(ip,port,cmd,user,passwd):

27 result = ""

28try

:29 ssh =paramiko.sshclient()

30ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

31ssh.connect(ip,port,user,passwd)

32 stdin, stdout, stderr =ssh.exec_command(cmd)

33 result =stdout.read()

34print

result

35ssh.close()

36except:37

print

"ssh_cmd err."38

return

result

39while

true:

40 raw = raw_input("

按a批量執行命令: 按b批量判斷檔案是否存在: 按q退出:")

41if raw =="a"

:42 cmd = raw_input("

請輸入要執行的命令:")

43for i in

host_dic:

44print

host_dic.get(i)

45ssh_cmd(i,port,cmd, user,passwd)

46elif raw =="b"

:47while

true:

48print

"\033[32;1m****使用說明:該指令碼可以判斷檔案,不能判斷目錄。****\033[0m

"49 choice = raw_input("

請輸入要查詢遠端伺服器上檔案的絕對路徑: 按q退出")

50if choice =="q"

:51print

"感謝使用,再見"52

exit()

53 v = "ls"

54 c = v + "

" +choice55#

print c

56for i in

host_dic:

57 ssh =paramiko.sshclient()

58ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

59ssh.connect(i,port,user,passwd)

60 stdin, stdout, stderr =ssh.exec_command(c)

61 result =stdout.read()62#

print result

63if result != "":64

print host_dic.get(i),"

\033[31;1m上存在該檔案\033[0m

"65 exist[i]=host_dic.get(i)66#

print host_dic.get(i),"\033[33;1m上存在該檔案\033[0m"

67ssh.close()68#

print exist

69for i in

host_dic:

70if i not

inexist:

71 not_exist[i]=host_dic.get(i)

72print host_dic.get(i),"

\033[33;1m上不存在該檔案\033[0m

"73 exist ={}74#

print not_exist

75 choice1 = raw_input("

按任意鍵繼續查詢,按t拷貝本地乙個檔案到不存在該檔案的機器上,按q退出:")

76if choice1 =="q"

:77print

"感謝使用,再見"78

print

exit()

79elif choice1 == "t"

:80 local_file = raw_input("

請輸入本地檔案的絕對路徑:")

81 remote_file = raw_input("

請輸入要複製到遠端機器的絕對路徑:")

82for j in

not_exist:

83 scp =paramiko.sshclient()

84scp.set_missing_host_key_policy(paramiko.autoaddpolicy())

85scp.connect(j,port,user,passwd)

86 sftp =paramiko.sftpclient.from_transport(scp.get_transport())

87 sftp =scp.open_sftp()

88sftp.put(local_file,remote_file)

89print

"\033[31;1m已複製到\033[0m

",host_dic.get(j)

90 not_exist ={}

91else:92

print

"感謝使用,再見

"93 exit()

curl判斷遠端檔案是否存在

在專案中遇到了要判斷乙個是否存在,遠端伺服器上 首先想到了用file get contents 這個函式,可以打到效果,但是發現,會很慢,一直在請求,產看資料發現,file get contents fopen 每次請求都會重新做dns查詢,並不對 dns資訊進行快取,curl函式則不一樣 fope...

PHP判斷遠端檔案是否存在

函式 remote file exists 功能 判斷遠端檔案是否存在 引數 url file 遠端檔案url 返回 存在返回true,不存在或者其他原因返回false function remote file exists url file url arr parse url url file i...

python判斷遠端檔案是否存在

如果列印ok,則表示存在 import paramiko client paramiko.sshclient client.load system host keys client.connect 10.10.0.0 username service password word stdout,cli...